简体   繁体   English

remove()方法的链表

[英]remove() method of a linked list

I am taking a programming class I have the following assignment. 我正在上一个编程课,我有以下任务。

Write a menu driven program that either accepts words and their meanings, or displays the list of words in lexicographical order (ie as in a dictionary). 编写一个菜单驱动的程序,它接受单词及其含义,或以字典顺序显示单词列表(即在字典中)。 When an entry is to be added to the dictionary you must first enter the word as one string, and then enter the meaning as separate string. 当要将条目添加到字典时,必须先将该单词作为一个字符串输入,然后将该含义输入为单独的字符串。 Another requirement - from time to time words become obsolete. 另一个要求 - 不时的话语已经过时了。 When this happens, such word must be removed from the dictionary. 发生这种情况时,必须从字典中删除这样的单词。

Use the JOptionPane class to enter the information. 使用JOptionPane类输入信息。

Use the concept of linked list to carryout this exercise. 使用链表的概念来执行此练习。 You will need at minimum the following classes: 您至少需要以下课程:

  • A WordMeaning class that hold the name of a word and its meaning. 一个WordMeaning类,它包含一个单词的名称及其含义。
  • A WordMeaningNode class that creates the node of information and its link field. 一个WordMeaningNode类,用于创建信息节点及其链接字段。
  • A WordList class that creates and maintain a linked list of words and their meanings. 一个WordList类,用于创建和维护单词及其含义的链接列表。
  • A Dictionary class that test your classes. 用于测试类的Dictionary类。

For the output, the program should produce two scrollable lists: 对于输出,程序应该生成两个可滚动列表:

  • The current list of words and their meanings. 当前的单词列表及其含义。
  • The list of the deleted words. 已删除单词的列表。 You need not list the meanings, just the words. 你不需要列出含义,只需单词。

So far, I have everything coded except for the remove method, and I am not sure how to code that, so could anyone help me please. 到目前为止,除了删除方法之外,我编写了所有代码,而且我不知道如何编写代码,所以任何人都可以帮助我。 I coded the add method already, but now I don't know where to begin with the remove method in my WordList class. 我已经编写了add方法,但现在我不知道从哪里开始使用我的WordList类中的remove方法。 My classes are below. 我的课程如下。

WordList Class: WordList类:

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 to use the same method format for the remove method as I did for the add method, but didn't really work, or I did it wrong. 我尝试使用与add方法相同的方法格式,但是没有真正起作用,或者我做错了。

To remove an item from LinkedList, you should iterate over its nodes. 要从LinkedList中删除项目,您应该遍历其节点。 Then, if occurence found, connect previous and next node, setting previous.next = next : 然后,如果发现了occurence,则连接上一个和下一个节点,设置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;
}

In main code, change the piece of case 2 : 在主代码中,更改case 2

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

because you really don't need NullPointerException here. 因为你真的不需要NullPointerException

To remove an element from your list, you need to find the element just before the one to remove, and set its next reference to the element after the one to remove. 要从列表中删除元素,您需要在要删除的元素之前找到该元素,并在要删除的元素之后设置其元素的next引用。

You will have some (not mutually exclusive) corner cases to pay attention to : 你会有一些(不是相互排斥的)角落案例需要注意:

  • If the element to remove is the first (then, the first node of WordList should be set to the element after the one to remove) 如果要删除的元素是第一个(那么,WordList的第一个节点应该被设置为要移除的元素之后的元素)
  • If the element to remove if the last one in the list (then you will have to set the previous element's next reference to null ) 如果要删除的元素如果列表中的最后一个(那么你将必须将前一个元素的next引用设置为null

Also, I see you need to keep a list of removed items, so don't forget to keep a reference on the removed item during the process, and to add it to your list of obsolete words. 此外,我发现您需要保留已删除项目的列表,因此请不要忘记在此过程中保留对已删除项目的引用,并将其添加到过时单词列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM