Creating a program to read a text file full of words in alphabetical order. I then have to read it into a Linked List. The linked list should output total number of words, which I have managed to do. After this it needs to calculate the min and max depth and average look up, which are the parts i'm getting completely lost one, have been reading tutorials for hours. I need to some how add the count to the node each time it is called?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Main;

//import InputWindow.InputWindow;
import EasyIn.*;
import EasyIn.EasyIn;
import java.io.*;
import java.util.*;
/**
 *
 * @author itmpm
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
  //  int Row;
    int Count;
    int PrintCount;

 //  public list ()  {


    
    StreamTokenizer st = new StreamTokenizer(new FileReader("filename"));

    LinkedList linked = new LinkedList();
    Iterator iterator;

    while (st.ttype !=st.TT_EOF) {
     st.nextToken();
     if (st.ttype==st.TT_WORD)

   //   System.out.println(st.sval);
     {int Row = 0;
            if (Row == 0){
         linked.addFirst(st.sval);
          }
        else
          {
          for(Count=0; ((Count<Row) && (st.sval == linked.get(Count))); Count++)
            {
            }
          linked.add(Count, st.sval);
          }
        Row++;

            iterator = linked.iterator();

    PrintCount=0;
    while (iterator.hasNext())
    {
      System.out.println(iterator.next());
      PrintCount++;
    }
      
    }
     iterator = linked.iterator();
    PrintCount=0;
    while (iterator.hasNext())
    {
      System.out.println(iterator.next());
      PrintCount++;
    }
   System.out.println("End of Sorted Linked List!");
   System.out.println("Size = " + linked.size());
 //   System.out.println(iterator.toString());

    }}
    
}

Recommended Answers

All 7 Replies

calculate the min and max depth and average look up

What does this mean? Can you Explain what happens that has a depth? And what a lookup is?

What does this mean? Can you Explain what happens that has a depth? And what a lookup is?

Well its a linked list, which I'm sure i have working as when I run the code it runs through a heap of words that are a lot longer than the total number of words. I can get an output from the total number of words.
The max depth would calculate how many times it would have to run through the list to find a word (from the textfile input). I'm thinking that I need to put a count on the 'hasNext' part or something. I don't think I would have to actually have to specify the word I want, It should just add some count and find the 'max'. After I can find this I'm confident I would be able to figure out the average and other stuff, which seems easy enough.

how many times it would have to run through the list to find a word

If the list is ordered, then it will find "AAAA" at the first, but "ZZZ" would require it to look to the end.
A linked list is not good for lookups. You always have to start at the beginning.
There must be some other meaning to max depth.

If the list is ordered, then it will find "AAAA" at the first, but "ZZZ" would require it to look to the end.
A linked list is not good for lookups. You always have to start at the beginning.
There must be some other meaning to max depth.

Well yes It is sorted. Once I can figure out how to find the depth then I will implement a balanced binary tree. Then compare them in terms of lookup time (Average lookup).

Sounds like a classical programming exercise.

Yes it probably is. This IS homework, and I'm not asking anyone to do it for me, just asking for help with which step I need to take to resolve this small problem i'm having after spending ages trying to figure it out. The class I'm taking is about Algorithms, and we aren't really taught how to program in Java, just told to figure it out.

Ok. Come and ask questions any time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.