I have been trying to create a linked list program that takes in a word and its definition and adds the word to a link list followed by the definition. i.e. java - a simple platform-independent object-oriented programming language.
the thing is that of course words must be added and removed from the list. so i am going to use the compareTo method. Basically there a 2 problems
1: some syntax problem which is causing my add and remove method not to be seen from the WordList class
2: Do I HAVE to use the iterator class to go thru the list? I understand how the iterator works but i see no need for it.
I just need to be pointed in the right direction im a little lost.................
your help would be greatly appreciated i've been working on this over a week i dont like linked list..........
Here are my 4 classes of code........
/*
* Dictionary.java
* Created on November 4, 2007, 10:53 PM
* A class Dictionary that implements the other classes.
* @author Denis
*/
import javax.swing.JOptionPane;
/* testWordList.java
* Created on November 10, 2007, 11:50 AM
* @author Denis
*/
public class Dictionary {
/** Creates a new instance of testWordList */
public Dictionary() {
boolean done=false;
String in="";
while(done!=true)
{
in = JOptionPane.showInputDialog("Type 1 to add to Dictionary, 2 to remove from Dictionary, 3 to Print Dictionary, and 4 to Quit");
int num = Integer.parseInt(in);
switch (num){
case 1:
String toAdd = JOptionPane.showInputDialog("Please Key in the Word a dash and the definition.");
add(toAdd);
break;
case 2:
String toDelete = JOptionPane.showInputDialog("What would you like to remove?");
remove(toDelete);
break;
case 3:
for(Word e: list)
System.out.println(e);
break;
case 4:
System.out.println("Transaction complete.");
done = true;
break;
default:
done = true;
break;
}//end switch
}//end while
}//end Dictionary
}//end main
import java.util.Iterator;
/* WordList.java
* Created on November 4, 2007, 10:40 PM
*A class WordList that creates and maintains a linked list of words and their meanings in lexicographical order.
* @author Denis*/
public class WordList{
WordNode list;
//Iterator<WordList> i = list.iterator();
/*public void main(String [] args)
{
while(i.hasNext())
{
WordNode w = i.next();
String s = w.word.getWord();
}*/
void WordList() {
list = null;
}
void add(Word b)
{
WordNode temp = new WordNode(b);
WordNode current,previous = null;
boolean found = false;
try{
if(list == null)
list=temp;
else{
current = list;
while(current != null && !found)
if(temp.object.getWord().compareTo(current.object.getWord())<0)
found = true;
else{
previous=current;
current=current.next;
}
temp.next=current;
if(previous==null)
list=temp;
else
previous.next=temp;
}//end else
}//end try
catch (NullPointerException e)
{
System.out.println("Catch at line 46");
}
}//end add
}
/*WordNode.java
* Created on November 4, 2007, 10:40 PM
*A class WordNode that contains a Word object of information and a link field that will contain the address of the next WordNode.
* @author Denis
*/
public class WordNode {
Word object;//Word object of information
WordNode next;//link field that will contain the address of the next WordNode.
WordNode object2;
public WordNode (WordNode wrd)
{
object2 = wrd;
next = null;
}
WordNode(Word x)
{
object = x;
next = null;
}
WordNode list = null;
//WordNode list = new WordNode("z");
Word getWord()
{
return object;
}
WordNode getNode()
{
return next;
}
}
import javax.swing.JOptionPane;
/* Word.java
* Created on November 4, 2007, 10:39 PM
* A class Word that holds the name of a word and its meaning.
* @author Denis
*/
public class Word {
private String word = " ";
/** Creates a new instance of Word with the definition*/
public Word(String w) {
word = w;
}
String getWord(){
return word;
}
}