简体   繁体   English

为什么我不能打印链表中的最后一个元素

[英]Why can I not print the last element in my linkedlist

I have implemented a basic linkedlist, that contains a Node class and a Linkedlist class, the Node class is a java generic.我已经实现了一个基本的链表,它包含一个 Node 类和一个 Linkedlist 类,Node 类是一个 java 泛型。

I am just wondering, why does my last element not print, when I do a print statement here is my classes我只是想知道,为什么我的最后一个元素不打印,当我做一个打印语句时,这里是我的类

Linkedlist.class链表类

  /**
 * A linked list is a sequential list of nodes that hold data that point to nodes also containng data
 *
 * Linked lists are used in Lists, Queues and Stacks
 * Great for creating circular lists
 *
 * Head = first node in a linkedlist
 * Tail = last node in a linkedlist
 * Pointer = reference to the next node in a linkedlist.
 * Node = object containing data.
 */
public class Linkedlist<T> {
    Node Head;

    public Linkedlist(Node n)
    {
        Head = n;
    }

    public boolean contains(T obj)
    {

        Node n = Head;
        while (!(n.getNextNode().equals(null))){
            System.out.println(n.getNextNode());  //Last element does not print here
            n = n.getNextNode();
        }
        return false;
    }


}

Node.class节点类

 public class Node<T> {

    Node next = null;
    T data;


    public Node(T data){
        this.data = data;
    };

    public void setNext(Node n)
    {
        next = n;
    }

    public Node getNextNode()
    {
        return this.next;
    }

    public T getData()
    {return this.data;}

    public void printNode()
    {
        System.out.println(getData());
    }

}

Thank you for the assistance, I am using the contains method to check if my while loop is iterating over all the elements.感谢您的帮助,我正在使用 contains 方法检查我的 while 循环是否正在遍历所有元素。 ( I think it, but it is just stopping at the last element and not going any further). (我认为是这样,但它只是停在最后一个元素而不是进一步)。

Any help will be appreciated任何帮助将不胜感激

You have to rethink how you print the data.您必须重新考虑如何打印数据。 Currently you never print the data from Head node, as you only ever print the next node ( n.getNextNode() ).目前,您从不打印Head节点的数据,因为您只打印下一个节点( n.getNextNode() )。 You are also breaking the loop when the next element is null.当下一个元素为空时,您也会中断循环。 This causes the last element to not be printed, as getNextNode() will return null before the print call.这会导致最后一个元素不被打印,因为getNextNode()将在 print 调用之前返回 null。

Instead go like this:而是这样:

public boolean contains(T obj) {
    Node n = Head;
    do {
        System.out.println(n.getData()); // print data of current node
        n = n.getNextNode(); // move to next node
    } while (n != null); // break if we are behind next node. n is null
    return false;
}

or if you might have an empty list (node passed into constructor is null):或者如果您可能有一个空列表(传递给构造函数的节点为空):

public boolean contains(T obj) {
    Node n = Head;
    while (n != null) {
        System.out.println(n.getData());
        n = n.getNextNode();
    }
    return false;
}

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

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