StackOverflowers。链接列表输出没有正确生成
我对这个网站相当陌生,并认为明智的来到这里寻求帮助,解决一个特殊问题,包括从链表打印正确的输出。 这个想法是采用一个程序,使用一个数组来生成一个列表并将其转换成一个链表,同时达到相同的结果。现在,我已经持续了好几个小时,即使达到了输出实际打印的地步(最后),也没有获得所需的结果。
需要的结果:
注意:该结果产生与使用阵列
词语:该计数:2
字:当然数:2
词语:检数:1个
词语:计数:7
词语:施工数:1个
词语:9
字:计数算法数:4
词语:和计数:9
结果我获得:
注:信仰数:2
字转换阵列的方法为链表
字时,这个结果产生:我的计数:2
Word:of count:2
Word:best count:2
Word:伯爵:2
字:数:2
字:是计数:2
字:事实数:2
词:计:2
我不知道是什么原因,这是案件和我似乎无法跟踪它。我尝试阅读我的笔记和搜索,但无济于事。我不确定它是否与setNext(...)[Node类的一部分]有关,或者我正在调用incrementCount()方法[Word类的一部分]。我不相信setNext(...)甚至有目的,但只是代码的一部分,在这一点上什么也不做。
我希望我的交付不会偏离轨道,并且可以为我的尝试提供解决方案。我知道我已经达到了我的极限,因为我无法想到与此有关的其他任何事情。
期待您的建议。
谢谢。 T3:
T3。
private Node top;
public WordList()
{
top = null;
}
// This method adds words to the linked list.
public void addWord(String w)
{
Word word = new Word(w);
top = new Node(word,top);
// Checks to see if a particular word is present more than once.
// If the particular word is encountered more than once, it
// increments the word count for that particular word, else
// a new node is created to store another word. The word check
// process is repeated once more.
// Note: getWord() is part of the Node class that attempts to retrieve
// a word that is held in a particular node.
// matchesWord(...) determines whether a particular word (string) has been
// encountered. If result is true, the number of times
// the word is encountered should be incremented.
if(top.getWord().matchesWord(w))
{
top.getWord().incrementCount();
}
else
{
Word newWord = new Word (w);
Node newNode = new Node(newWord, top);
//top = newNode;
top.setNext(newNode);
}
} // end addWord
// This method prints out the linked list.
public void printList()
{
Node currentNode;
currentNode = top.getNext();
while(currentNode != null)
{
System.out.println(currentNode.getWord());
currentNode = currentNode.getNext();
}
}
屏幕取词()只返回一个储存在节点A字对象。就是这样。我试图修复printList()方法,但仍然无法解决错误。 – TicklyTurtle
您是否理解我说过每次调用addWord()时总是添加一个'Node'?拿出这条线,并尝试迭代你的名单来寻找匹配,而不是仅仅检查顶部的什么' – millhouse
我很感谢你的帮助,但是看到我没有进步,我只会向你表示感谢,并且保持原样。 :-)我尽我所能。很荣幸能收到你的回答。 – TicklyTurtle