简体   繁体   English

在java实现单链表中,为什么链表的最后一个元素没有打印出来?

[英]In java implementation of Singly Linked List, Why is the last element of the linked list not being printed?

public class Linked_List <E>{
    public static class Node<E>{
        private E element;
        private Node<E> next;
        public Node(E e,Node<E> n) {
        element=e;
        next=n;
    }
    public E getElement() {
        return element;
    }
    public Node<E> getNext() {
        return next;
    }
    public void setNext(Node<E> n) {
        next=n;
    }
}

public Node<E> head=null;
public Node<E> tail=null;
public int size=0;

public Linked_List() {}

public int size() {
    return size;
}

public boolean isEmpty() {
    return size==0;
}
public void addFirst(E e) {
    head=new Node<>(e,head);
    if(size==0)
        head=tail;
    size++;
}

public void addLast(E e) {
    Node<E> newest =new Node<>(e,null);
    if(isEmpty())
        head=newest;
    else
        tail.setNext(newest);
    tail=newest;
    size++;
}
public void show() {

    Node<E> n=head;
    if(size==0) {
        System.out.println("No elements to print");
        System.exit(0);
    }
    while(n.next!=null) {
        System.out.println(n.element);
        n=n.next;
    }
    System.out.println(n.element);
}

public static void main(String[] args) {
    Linked_List<Integer> list = new Linked_List<Integer>();

    list.addFirst(10);
    list.addFirst(11);
    list.addFirst(12);

    list.show();

}
}

In show() method when the while reaches the last element of the list, it exits so the element doesn't get printed.在 show() 方法中,当 while 到达列表的最后一个元素时,它会退出,因此该元素不会被打印。 Hence the last print statement in the show method.因此显示方法中的最后一个打印语句。 I have added three elements into the list but when I execute the show() method only the first two elements that is 12 and 11 get printed.我在列表中添加了三个元素,但是当我执行 show() 方法时,只有前两个元素 12 和 11 被打印出来。 What is it that i am missing?我错过了什么? Thanks.谢谢。

How about here.这里怎么样。 This should say tail = head ;这应该说tail = head ;

    public void addFirst(E e) {
        head = new Node<>(e, head);
        if (size == 0) {
            head = tail;
        }
        size++;
    }

addFisrt() change to: addFisrt() 更改为:

public void addFirst(E e) {
    head = new Node<>(e, head);
    size++;
}

you can debug addFirst(), the first element never add to LinkedList.您可以调试 addFirst(),第一个元素永远不会添加到 LinkedList。

The problem is in the while loop condition:问题出在while循环条件中:

while(n.next!=null) {
        System.out.println(n.element);
        n=n.next;
    }

When the list reaches element n-1 it will print the element and then n will become n=n.next which is the last element.当列表到达元素n-1时,它将打印该元素,然后 n 将变为n=n.next ,这是最后一个元素。 But now n being the last element his n.next will be null therefore while will break since the condition is not met anymore.但是现在 n 是最后一个元素,他的n.next将是 null 因此 while 将因为不再满足条件而中断。

You answered your own question:你是在自问自答:

In show() method when the while reaches the last element of the list, it exits so the element doesn't get printed.在 show() 方法中,当 while 到达列表的最后一个元素时,它会退出,因此该元素不会被打印。

This code:这段代码:

while(n.next!=null) {
    System.out.println(n.element);
    n=n.next;
}

says "while it's not the last element, print it".说“虽然它不是最后一个元素,但打印它”。 ie, the code explicitly does not want to print the last element.即,代码明确不想打印最后一个元素。

I am here assuming that you're not including the final print statement in your problem description - that you added the final print to work around the while-loop problem.我在这里假设您没有在问题描述中包含最终打印语句 - 您添加了最终打印以解决 while 循环问题。

You need:你需要:

while (n != null) {
    System.out.println(n.element);
    n = n.next;
}

which says 'while it is an actual element, print it'.它说'虽然它是一个实际元素,但打印它'。

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

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