Java 我的链接列表正在返回刚刚输入的节点
我正在为我的学校作业做一个字典系统。我尝试的想法是将每个单词及其定义存储在一个节点中,并使用二叉搜索树进行搜索 但出现了一个问题。当我在链表中的节点中添加单词和定义时,wordList()方法将返回我输入的最后一个节点。我想我在实现和链接节点时遇到了问题。你能帮帮我吗?(注:Vocab是我的测试类)Java 我的链接列表正在返回刚刚输入的节点,java,linked-list,binary-search-tree,Java,Linked List,Binary Search Tree,我正在为我的学校作业做一个字典系统。我尝试的想法是将每个单词及其定义存储在一个节点中,并使用二叉搜索树进行搜索 但出现了一个问题。当我在链表中的节点中添加单词和定义时,wordList()方法将返回我输入的最后一个节点。我想我在实现和链接节点时遇到了问题。你能帮帮我吗?(注:Vocab是我的测试类) 如果要使用链接列表,请使用其方法,而不是手动操作: LinkedList字典=新建LinkedList() 在上面的代码中,我注释掉了所有深入一层的代码行。在使用方面,您永远不应该关心节点类是否在内
如果要使用
链接列表
,请使用其方法,而不是手动操作:
LinkedList字典=新建LinkedList()
在上面的代码中,我注释掉了所有深入一层的代码行。在使用方面,您永远不应该关心节点
类是否在内部使用
请看一看适用于您的LinkedList方法。然后试着想想,为什么当你不使用它们时,你永远不会改变列表的内部属性,从而使它半途而废
<>编辑:顺便考虑删除<代码> StestBuffy/Cuff>和<代码> SETSTATE/<代码>接受<代码>节点< /COD>实例的方法。它们只会让你感到困惑,你本不应该出现在那里。请从问题中删除未使用的代码。事实上,这个问题包含350多行代码,其中大部分与问题无关。请提供一份报告。(特别是,看起来您根本不需要TreeNode或BinarySearchTree。)
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();
}
}
// 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();