Java 从单向链表打印节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16565045/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:27:08  来源:igfitidea点击:

printing nodes from a singly-linked list

javasingly-linked-list

提问by user2326847

I made a node class which is a linked list class. Is there any way I can print out elements in this list ? I made my print()method but it only returns the first element which is 21. How do I iterate through that list ?

我做了一个节点类,它是一个链表类。有什么办法可以打印出这个列表中的元素吗?我创建了我的print()方法,但它只返回第一个元素,即 21。如何遍历该列表?

public class ListNode {
    private int item;
    private ListNode next;

    public ListNode(int item, ListNode next){
        this.item = item;
        this.next = next;
    }

    public ListNode(int item){
        this(item, null);
    }

    public int print(){
        return item;
    }

    public static void main(String[] args) {            
        ListNode list = new ListNode(21, new ListNode(5, new ListNode(19, null)));
        System.out.println(list.print());
    }

}

}

采纳答案by ValarDohaeris

public String toString() {
    String result = item + " ";
    if (next != null) {
        result += next.toString();
    }
    return result;
}

And then you can simply do

然后你可以简单地做

System.out.println(list.toString());

(I renamed your function from printto toStringto give a more accurate description of what it does)

(我将您的函数重命名为printtotoString以更准确地描述它的作用)

回答by John B

Consider creating a printall

考虑创建一个 printall

public void printAll(){
    System.out.println(item);
    if (next != null){
        next.printAll();
    }
}

回答by Vladimir

Your current implementation doesn't print anything: it simply returns item. More appropriate implementation would look like:

您当前的实现不打印任何内容:它只是返回项目。更合适的实现如下所示:

public void print() {
    System.out.println(item); 
}

You can then use recursion to print all items:

然后您可以使用递归打印所有项目:

public void printAll() {
    print();
    if (next != null) {
        System.out.println("; ");
        next.printAll(); 
    }
}

回答by goodies

Your print()function is returning only a single item that's why it is printing only 21.

您的print()函数只返回一个项目,这就是为什么它只打印 21。

Call recursively to print all the values until next != NULL

递归调用以打印所有值,直到 next != NULL

回答by jayrobin

Calling list.print()will only ever return the value of the head (21) - you are never making any reference or call to the next node: next.

调用list.print()将只返回头(21)的价值-你永远不会做任何参考或呼叫到下一个节点:next

Personally, I would remove the print() method and instead override toString():

就个人而言,我会删除 print() 方法,而是覆盖 toString():

@override
public String toString(){
    return item + "\n" + next;
}

I guess you probably don't want the null tail printed, so this is probably nicer:

我想你可能不希望打印空尾,所以这可能更好:

@override
public String toString(){
    if(next) {
        return item + "\n" + next;
    } else {
        return item + "\n";
    }
}

Then in, main:

然后在,主要:

public static void main(String[] args) {            
    ListNode list = new ListNode(21, new ListNode(5, new ListNode(19, null)));
    System.out.println(list);
}

回答by John Snow

You can use a foreach loop:

您可以使用 foreach 循环:

List<ListNode> theList = new LinkedList<ListNode>();
//add stuff to the list
for(ListNode n:theList)
   System.out.println(n.print();

THis will iterate over the list and return the next object, on this object we call the print()method

这将遍历列表并返回下一个对象,在这个对象上我们调用print()方法