我的链表正在返回最后一个输入的节点



我正在为我的学校作业做一个字典系统。我试图做的想法是将每个单词及其定义存储在一个节点中,并使用二进制搜索树进行搜索。

但是出现了一个问题。当我在链表中的节点中添加单词和定义时,wordList((方法会返回我输入的最后一个节点。我想我在实现和链接节点时遇到了问题。你能帮帮我吗?(注:Vocab是我的测试课程(

public class Node {
private static String word;
private static String definition;
private Node link;

public Node() {
link = null;
word = null;    
}
public Node(String newWord) {
setWord(newWord);
link = null;
}
public Node(String newWord, Node linkValue) {
setWord(newWord);
link = linkValue;
}
public Node(String newWord, String newDefinition, Node linkValue) {
setWord(newWord, newDefinition);
link = linkValue;
}
public Node(String newWord, String newDefinition) {
setWord(newWord, newDefinition);

}
@Override
public String toString() {
return "Node [word=" + word + ", link=" + link + "]";
}
public static String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
public static String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public void setWord(String word, String definition) {
this.word = word;
this.definition = definition;
}
public Node getLink() {
return link;
}
public void setLink(Node link) {
this.link = link;
}
}

public class LinkedList {
protected static Node first;
private Node last;
public LinkedList() {
super();
this.first = null;
this.last = null;
}
public LinkedList(String wordName, String wordDefinition, Node first2) {
// TODO Auto-generated constructor stub
}
public boolean isEmpty() {
return(first == null);
}
public void insert(String newWord, String newDefinition) {
if(isEmpty()) {
first= last= new Node(newWord, newDefinition, first);
}
else {
first = new Node(newWord, newDefinition, first);
}
}
public void wordList() {
Node current = first;
while(current != null) {
System.out.println(current.getWord());
current = current.getLink();
}
}
public static Node getFirst() {
return first;
}
public void setFirst(Node first) {
this.first = first;
}
public Node getLast() {
return last;
}
public void setLast(Node last) {
this.last = last;
}
}
public class Vocab {
public static void main(String[] args) {
LinkedList dictionary = new LinkedList();

Node a = new Node("Assembly","A unit of fitted parts that naje yo a mechanism or machine, such as the headstock assemble of a lathe.");

dictionary.setFirst(a);
Node b = new Node("Boil","Agitation of a bath of metal caused by the liberation of a gas beneath its surface.");
a.setLink(b);
b.setLink(null);

dictionary.wordList();

}
}

如果您想使用LinkedList,请使用其方法,而不是手动操作:

LinkedList dictionary=新建LinkedList((;

// Node a = new Node("Assembly","A unit of fitted parts that naje yo a mechanism or machine, such as the headstock assemble of a lathe.");

dictionary.setFirst(a);
//Node b = new Node("Boil","Agitation of a bath of metal caused by the liberation of a gas beneath its surface.");
//a.setLink(b);
//b.setLink(null);
dictionary.wordList();

在上面的代码中,我已经注释掉了所有深入一级的代码行。在使用方面,您永远不应该关心Node类是否在内部使用。

看看适用于您的LinkedList方法。然后试着想想为什么当你不使用它们时,你永远不会改变列表的内部属性,从而使它成为一个半坏的设置。

EDIT:顺便考虑删除接受Node实例的setFirstsetLast方法。他们只会让你感到困惑,不应该出现在那里。

相关内容

  • 没有找到相关文章