double link list project...
ok... this will be fairly lengthy because I need to put the code in here so that maybe you'll understand...so please bear with me :)
I started out with a project that we did in class (which is the code I'm posting at the end of this) and it read in a file that had a poem in it. Each word of the poem has a two-digit number in front of it. it is broken up in the program to where the variable sequence is the two-digit number and the word variable is the rest of the word that was read in. The program takes all of the poem and prints it out by adding a word everytime:
Mary
Mary had
Mary had a...and so on.
Now we must make changes to the program so that it is a double link list so that the poem being read in can be out of order (where the original program had the text file in order). WE have to make it so that each time the next word is read in...the sequence numbers are compared and then either the one read in goes to the left or right of the current word. And eventually it will have to be printed out backwards too. Sorry if this is confusing. We've had a couple of people working on this and none of us have gotten anywhere (needless to say we are all beginners!)
Well, here's the code I have and the instrustors notes. If anyone can give us any direction, it would be great. Thanks!!
publicclass Poem
{
private WordNode list;
publicvoid add(Word word)
{
WordNode node =new WordNode(word);
WordNode current;
if(list ==null)
list = node;
else
{
current = list;
while(current.next !=null)
current = current.next;
current.next = node;
}
}
public String toString()
{
String result ="";
WordNode current = list;
while(current.next !=null)
{
result += current.word +" ";
current = current.next;
}
result += current.word +" ";
return result;
}
privateclass WordNode
{
public Word word;
public WordNode next;
public WordNode (Word word)
{
this.word = word;
next =null;
}
}
}
publicclass Word
{
private String word;
privateint sequence;
public Word(String newWord)
{
sequence = Integer.parseInt(newWord.substring(0, 2));
word = newWord.substring(2);
}
publicint getSequence()
{
return sequence;
}
public String toString()
{
return word;
}
}
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
publicclass PoemTest2
{
publicstaticvoid main (String[] args)throws IOException
{
Scanner scan =new Scanner(new File("numbers.txt"));
Poem poem =new Poem();
while(scan.hasNext())
{
poem.add (new Word(scan.next()));
System.out.println ("\n\nForward" + poem );
System.out.println ("\nBackwards: " + poem.toStringBackwards());
}
System.out.println ("\n\nForward" + poem);
System.out.println ("\nBackwards: " + poem.toStringBackwards());
}
}
Limerick.txt(Original file...all in order)
01A 02mathematician 03named 04Bath
05Let 06x 07equal 08half 09that 10he 11hath.
12He 13gave 14away 15y
16Then 17sat 18down 19to 20pi
Numbers.txt(new one that's out of order)
21And 22choked. 23What 24a 25sad 26aftermath
INSTRUCTOR'S NOTES
Specifications:These specifications are based on the classes developed in the Poem lab.
The Word class should have an accessor method added for the instance variable sequence since that variable抯 value will be needed in the Poem class.
The Poem class should be modified so that the list of Word objects can be traversed from either direction: first-to-last or last-to-first. This will require an additional WordNode variable to contain the address of the last WordNode in the list.
The add method should be modified so that a WordNode is placed in the list in such a way that the sequence of its associated Word is not less than that of the previous WordNode抯 Word (if any) and not greater than that of the next WordNode抯 Word (if any). In other words, the Words associated with the list of WordNodes should be in ascending sequence order when the list is traversed first-to-last and in descending sequence order when the list is traversed last-to-first. A WordNode being placed in the list might replace the first node, replace the last node, or be inserted between 2 nodes depending on where its Word should go in the list. Each of these alternatives requires updating the value of the next and previous variables in the node being added as well as those in its neighbor node(s). In addition the variables in the Poem class that point to the first and last WordNodes will need to be updated whenever the first or last node is replaced.
A method named toStringBackwards should be added to return a String consisting of the list of Words in last-to-first order. This method can be created by copying toString and making a couple of minor changes.
The inner class WordNode should have an additional WordNode variable added to contain the address of the previous WordNode in the list.

