链接列表学校作业

时间:2015-04-15 08:50:24

标签: java linked-list

我正在上编程课,我有以下任务。

编写菜单驱动的程序,该程序接受单词及其含义,或以字典顺序显示单词列表(即在字典中)。当要将条目添加到字典时,必须先将该单词作为一个字符串输入,然后将该含义输入为单独的字符串。另一个要求 - 不时的话语已经过时了。发生这种情况时,必须从字典中删除这样的单词。

使用JOptionPane类输入信息。

使用链表的概念来执行此练习。您至少需要以下课程:

  • 一个WordMeaning类,它包含单词的名称及其含义。
  • 一个WordMeaningNode类,用于创建信息节点及其信息 link field。
  • 一个WordList类,用于创建和维护单词和链接列表 他们的意思。
  • 测试课程的Dictionary类。

对于输出,程序应生成两个可滚动列表:

  • 当前的单词列表及其含义。
  • 已删除字词的列表。你不需要列出含义 单词。

到目前为止,除了删除方法之外,我已经编码了所有内容,而且我不知道如何编写代码,所以任何人都可以帮助我。我已经编写了add方法,但现在我不知道从哪里开始使用我的WordList类中的remove方法。我的课程如下。

WordMeaning Class:

public class WordMeaning {

String name;
String definition;

WordMeaning(String t, String d) {
    name = t;
    definition = d;
}

String getName() {
    return name;
}

String getDefinition() {
    return definition;}
}

WordMeaningNode类:

public class WordMeaningNode {

WordMeaning wordMeaning;
WordMeaningNode next;

WordMeaningNode(WordMeaning w) {

    wordMeaning = w;
    next = null;
}

public WordMeaning getWordMeaning()
{
    return wordMeaning;
}
}

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;
}
}

字典类:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

class Dictionary {

public static void main(String[] args) {
    WordMeaning entry;
    WordList diction = new WordList();
    WordList obsolete = new WordList();
    boolean more = true;
    int menuOption = 0;

    String menuMessage = "1. Enter a word and its definition\n2. Remove a"
            + "word\n3. Display all words and meanings\n4. Display"
            + "Removed words\n\n "
            + "Enter the Menu Option (1,2,3, or 4--Any other key"
            + "exits the program)";

    do {
        menuOption = GetData.getInt(menuMessage); //user input

        switch (menuOption) {
            case 1:
                String word = GetData.getString("Enter the word to define:"
                        + " ").toUpperCase();
                String meaning = GetData.getString("Enter the meaning of "
                        + word + " : ");
                meaning = " - " + meaning;
                entry = new WordMeaning(word, meaning);
                diction.add(entry);
                JOptionPane.showMessageDialog(null, word + " was added to"
                        + " the dictionary.", "New Entry",
                        JOptionPane.INFORMATION_MESSAGE);
                break;

            case 2:
                word = GetData.getString("Enter the obsolete word:")
                        .toUpperCase();

                try {
                    diction.remove(word);
                    obsolete.add(new WordMeaning(word, " "));
                    JOptionPane.showMessageDialog(null, word + "has been removed!", "Word Removal", JOptionPane.INFORMATION_MESSAGE);
                } catch (NullPointerException e) {
                    JOptionPane.showMessageDialog(null, word + "does not exist", "Word Removal", JOptionPane.INFORMATION_MESSAGE);
                }
                break;

            case 3:
                JTextArea text = new JTextArea(diction.toString(), 10, 40);
                JScrollPane pane = new JScrollPane(text);
                JOptionPane.showMessageDialog(null, pane, "Current"
                        + "Dictionary", JOptionPane.INFORMATION_MESSAGE);
                break;

            case 4:
                text = new JTextArea(obsolete.toString(), 10, 40);
                pane = new JScrollPane(text);
                JOptionPane.showMessageDialog(null, pane, "Obsolete Words",
                        JOptionPane.INFORMATION_MESSAGE);
                break;
            default:
                more = false;

        }
    } while (more);
}
}

GetData Class:

import javax.swing.JOptionPane;
class GetData
{
static double getDouble(String s)
{
    return Double.parseDouble(getString(s));
}

static int getInt(String s)
{
    return Integer.parseInt(getString(s));
}

static String getString(String s)
{
    return JOptionPane.showInputDialog(s);
}
}

2 个答案:

答案 0 :(得分:0)

要从链接列表中删除节点,首先搜索该节点的链接列表,然后将所有参考点替换为对下一个节点的引用。

让我们调用我们想要删除current的节点和previous之前的节点 (以便previous.next == current) 然后只需设置previous.next = current.nextcurrent就会从您的列表中消失。

特殊情况是current是第一个节点,在这种情况下没有previous
在这种情况下,请设置list = list.next

答案 1 :(得分:0)

删除节点:

  • 如果diction.list是搜索到的对象,请将diction.list替换为diction.list.next

  • 在列表中迭代current;如果current.next是搜索到的对象,请将current.next设置为current.next.next