我正在学习一门编程课,我有以下任务。链接列表的remove()方法
编写一个菜单驱动的程序,可以接受单词及其含义,也可以按词典顺序(即在字典中)显示单词列表。当要将词条添加到字典中时,必须先将该词作为一个字符串输入,然后将其含义作为单独的字符串输入。另一个要求 - 不时地语言变得过时。发生这种情况时,必须从字典中删除此类字词。
使用JOptionPane类输入信息。
使用链表的概念来执行此练习。您至少需要以下课程:
- WordMeaning类,它包含单词的名称及其含义。
- 一个WordMeaningNode类,用于创建信息节点及其 链接字段。
- 一个WordList类,用于创建和维护单词的链接列表和 其含义。
- 一个测试你的类的Dictionary类。
对于输出,程序应该产生两个滚动列表:
- 词和它们的含义的当前列表。
- 删除单词的列表。你不需要列出意义,只需要 这个词。
到目前为止,除了remove方法之外,我已经编码了一切,我不确定如何编码,所以任何人都可以帮助我。我已经编写了add方法,但是现在我不知道我的WordList类中的remove方法从哪里开始。我的课程如下。
单词表类:
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;
}
}
我想,像我一样的add方法使用相同的方法格式remove方法,但并没有真正的工作,或者我没有错。
不,他的任务是*代码* WordMeaningNodes的链表,每个节点包含一个带有单词及其定义的WordMeaning实例。在发表评论前阅读问题。 –