简体   繁体   English

遍历链表Java的实现

[英]Iterating through an implementation of a Linked List Java

So I read several posts regarding this subject but they all refer to iterating through a linked list that is already implemented by Java; 因此,我阅读了有关此主题的几篇文章,但它们都涉及通过Java已经实现的链表进行迭代。 for example, LinkedList<String> list = new LinkedList<String>(); 例如, LinkedList<String> list = new LinkedList<String>(); . And then goes on to say use a for loop to iterate through the linked list. 然后继续说使用for循环遍历链表。 However, I'm trying to implement my own linked list and I'm not sure how to iterate through them. 但是,我正在尝试实现自己的链表,并且不确定如何遍历它们。 In other words, I have the following code: 换句话说,我有以下代码:

class Node {
    private Node next = null; 
    private int data; 

    public Node(int d) {
        data = d; 
    }

    void appendToTail(int d) {
        Node end = new Node(d); 
        Node n = this; 
        while(n.next != null) {
            n = n.next; 
        }
        n.next = end; 
    }

    void print() {
        Node n = this; 
        while(n.next != null) {
            System.out.println(n); 
            n = n.next; 
        }
    }

    public static void main(String [] args) {
        Node x = new Node(4); 
        x.appendToTail(5); 
        x.print(); 
    }   
}

The print() function that I had written is my effort in trying to iterate through the linked list. 我编写的print()函数是我尝试遍历链接列表的工作。 But, it is not working. 但是,它不起作用。 Anybody know how to iterate through the linked list, given your own implementation of the linked list? 在您自己执行链表的情况下,有人知道如何遍历链表吗?

Change 更改

while(n.next != null) 

to

while(n != null)

because inside the loop you are printing the current node n and then pointing it to its next node by: n = n.next; 因为在循环内您正在打印当前节点 n ,然后通过以下方式将其指向下一个节点n = n.next;

You should check whether current node is null or not, not the next node. 您应该检查当前节点是否为null ,而不是下一个节点。 Because you will miss the last node of the list in that way, next part will be null for last node and loop would not execute. 因为您将以这种方式错过列表的最后一个节点,所以对于最后一个节点, next一部分将为null ,并且不会执行循环。

You need to print data part of the node. 您需要打印节点的data部分。 You have not defined the toString method for your Node class. 您尚未为Node类定义toString方法。

void print() {
  Node n = this;
  while(n != null) {
    System.out.println(n.data);
    n = n.next;
  }
}

You can define the toString as below for your Node class, then you can directly print the Node object in System.out statement. 您可以为Node类定义如下的toString ,然后可以在System.out语句中直接打印Node对象。

@Override
public String toString() {
  return "Node{" +
      ", data=" + data +
      '}';
}

You should be checking n for null, not n.next() :: 您应该检查n是否为null,而不是n.next() ::

while(n != null)

but you have all the aspects of a for loop (initial state, termination condition and iteration expression), so this can better be expressed as a for loop: 但是您具有for循环的所有方面(初始状态,终止条件和迭代表达式),因此可以将其更好地表示为for循环:

for (Node n = this; n != null; n = n.next)
    System.out.println(n.data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM