Remove () linked list

I take the programming class, I have the following purpose.

Write a menu-driven program that either accepts words or their meanings, or displays a list of words in lexicographical order (for example, like in a dictionary). When an entry is to be added to the dictionary, you must first enter the word as a single line, and then enter the value as a separate line. Another requirement is that words become obsolete from time to time. When this happens, such a word should be removed from the dictionary.

Use the JOptionPane class to enter information.

Use the linked list concept to complete this exercise. You will need at least the following classes:

  • The WordMeaning class that contains the name of the word and its meaning.
  • The WordMeaningNode class that creates the node information and its link.
  • The WordList class that creates and maintains a linked list of words and their meanings.
  • A dictionary class that validates your classes.

For output, the program must create two scrollable lists:

  • The current list of words and their meanings.
  • List of deleted words. You do not need to list meanings, just words.

So far, I have everything encoded except the remove method, and I'm not sure how to code it, so can someone help me. I already encoded the add method, but now I don’t know where to start with the remove method in my WordList class. My classes are below.

WordList Class:

public class WordList {

WordMeaningNode list;

WordList() {
    list = null;
}

void add(WordMeaning w)// In alphabetical order
{
    WordMeaningNode temp = new WordMeaningNode(w);

    if (list == null)
        list = temp;
    else
    {
        WordMeaningNode aux = list;
        WordMeaningNode back = null;
        boolean found = false;

        while(aux != null && !found)
            if( temp.getWordMeaning().getName().compareTo(aux.getWordMeaning().getName()) < 0 )
                found = true;
            else
            {
                back = aux;
                aux = aux.next;
            }

        temp.next = aux;
        if (back == null)
            list = temp;
        else
            back.next = temp;
    }
}

boolean listIsEmpty() {
    boolean empty;
    if (list == null) {
        empty = true;
    } else {
        empty = false;
    }

    return empty;
}

public String toString()
{
    String result = "";
    int count = 0;
    WordMeaningNode current = list;

    while (current != null)
    {
        count++;
        result += current.getWordMeaning().getName() + "\n" + "\t" + current.getWordMeaning().getDefinition();
        current = current.next;
    }

    return result + "\nThe number of words is : " + count;
}
}

I tried using the same method format for the remove method as I did for the add method, but it actually didn't work, or I did it wrong.

+4
2

LinkedList, . , , node, previous.next = next:

boolean remove(String word) {

    if (list == null)   // list is empty
        return false;

    WordMeaningNode n = list;
    WordMeaningNode prev = null;

    do {
       if (n.wordMeaning.name.equals(word)) {  // word found
           if (prev != null) {
              prev.next = n.next;   // connect previous to next
           } else {
              list = list.next;     // connect head to next
           }
           return true;
       }
       prev = n;
       n = n.next;
    } while (n != null);   // repeat till the end of a list
    return false;
}

case 2:

if (diction.remove(word)) {
    obsolete.add(new WordMeaning(word, " "));
    // notify about deletion
} else {
    // notify that word don't exist.
}

NullPointerException .

+4

, , , next .

( ) , :

  • ( node WordList )
  • , ( next null)

, , , .

0

All Articles