java 如何显示LinkedList的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16248612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display all the contents of the LinkedList?
提问by Tan
Please consider the following code for linkedlist. Basically I've created three nodes in the LinkedList class and trying to display the contents but I'm doing something wrong in the DisplayLinkedList() method. Right now I'm getting my output as follows :
请考虑以下链接列表的代码。基本上,我在 LinkedList 类中创建了三个节点并尝试显示内容,但我在 DisplayLinkedList() 方法中做错了。现在我的输出如下:
B
C
null
I wanna display it as follows: A B C
我想显示如下:A B C
respectively. Could anyone tell me where I'm wrong in the DisplayLinkedList() method ?
分别。谁能告诉我我在 DisplayLinkedList() 方法中哪里错了?
package MyPackage;
class Node {
String data;
Node next;
public Node(String data, Node next){
this.data = data;
this.next = next;
}
public String getData(){
return data;
}
public Node getNext(){
return next;
}
public void setNext(Node n){
next = n;
}
public String toString() {
return this.data;
}
}
// CREATING LINKED LIST BACKWARDS AND APPLYING SOME OPERATIONS ON IT
class LinkedList{
Node cNode = new Node("C", null);
Node bNode = new Node("B", cNode);
Node list = new Node("A", bNode);
public void DisplayLinkedList(){
Node prev = null;
Node curr = list;
while(curr != null){
prev = curr;
curr = curr.getNext();
System.out.println(curr);
}
}
public class LinkedListByME {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.DisplayLinkedList();
}
}
回答by Scott Woodward
You're checking if the curr is null, and THEN incrementing, which makes curr a null value that you try to print.
您正在检查 curr 是否为空,然后递增,这使 curr 成为您尝试打印的空值。
while(curr != null){
prev = curr;
curr = curr.getNext();
System.out.println(curr);
}
doing the print first should help.
先打印应该会有所帮助。
while(curr != null){
System.out.println(curr);
prev = curr;
curr = curr.getNext();
}
回答by Scott Woodward
Your issue is in your loop in DisplayLinkedList
. You "miss" the first node because you advance to the next node before printing it.
你的问题在你的循环中DisplayLinkedList
。您“错过”了第一个节点,因为您在打印之前前进到了下一个节点。
It should be:
它应该是:
while(curr != null) {
System.out.println(curr);
prev = curr;
curr = curr.getNext();
}
Also, it looks like you are keeping track of prev
without using it. The simplified version of the method could be:
此外,看起来您正在跟踪prev
而不使用它。该方法的简化版本可以是:
public void DisplayLinkedList() {
Node curr = list;
while(curr != null) {
System.out.println(curr);
curr = curr.getNext();
}
}
回答by Junaid Hassan
public void listTrasverse() { if(isEmpty()) { System.out.print("\nLIST IS EMPTY !!!"); } else { while(current!=null) { current.displayLink(); current=current.next; } } }
public void listTrasverse() { if(isEmpty()) { System.out.print("\nLIST IS EMPTY !!!"); } else { while(current!=null) { current.displayLink(); current=current.next; } } }
Use this code to call the displayLink()
method in you node class the displayLink()
method will be
使用此代码调用displayLink()
节点类中的displayLink()
方法,该方法将是
public void displayLink(){
System.out.print("\nDATA= "+data);
}