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

huangapple go评论74阅读模式
英文:

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)
            tail = head;
        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();
    }
}

show() 方法中,当 while 循环到达链表的最后一个元素时,它会退出循环,因此最后一个元素不会被打印出来。这就是在 show 方法中添加最后一个打印语句的原因。
您已经向列表中添加了三个元素,但当执行 show() 方法时,只有前两个元素(12 和 11)被打印出来。您遗漏了什么呢?谢谢。

英文:
public class Linked_List &lt;E&gt;{
public static class Node&lt;E&gt;{
private E element;
private Node&lt;E&gt; next;
public Node(E e,Node&lt;E&gt; n) {
element=e;
next=n;
}
public E getElement() {
return element;
}
public Node&lt;E&gt; getNext() {
return next;
}
public void setNext(Node&lt;E&gt; n) {
next=n;
}
}
public Node&lt;E&gt; head=null;
public Node&lt;E&gt; 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&lt;&gt;(e,head);
if(size==0)
head=tail;
size++;
}
public void addLast(E e) {
Node&lt;E&gt; newest =new Node&lt;&gt;(e,null);
if(isEmpty())
head=newest;
else
tail.setNext(newest);
tail=newest;
size++;
}
public void show() {
Node&lt;E&gt; n=head;
if(size==0) {
System.out.println(&quot;No elements to print&quot;);
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&lt;Integer&gt; list = new Linked_List&lt;Integer&gt;();
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. 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. What is it that i am missing? Thanks.

答案1

得分: 3

这里怎么样。这里应该是 tail = head

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

How about here. This should say tail = head;

	public void addFirst(E e) {
head = new Node&lt;&gt;(e, head);
if (size == 0) {
head = tail;
}
size++;
}
</details>
# 答案2
**得分**: 0
addFirst()更改为:
```java
public void addFirst(E e) {
head = new Node<>(e, head);
size++;
}
你可以调试addFirst(),第一个元素从未添加到LinkedList中。
英文:

addFisrt() change to:

public void addFirst(E e) {
head = new Node&lt;&gt;(e, head);
size++;
}

you can debug addFirst(), the first element never add to LinkedList.

答案3

得分: -1

问题在于 while 循环的条件:

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

当列表到达元素 n-1 时,它将打印该元素,然后 n 将变为 n=n.next,即最后一个元素。但是现在 n 作为最后一个元素,他的 n.next 将为 null,因此循环条件不再满足,循环将终止。

英文:

The problem is in the while loop condition:

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. But now n being the last element his n.next will be null therefore while will break since the condition is not met anymore.

答案4

得分: -2

你自己回答了你的问题:

在show()方法中,当while循环达到列表的最后一个元素时,它会退出,因此该元素不会被打印出来。

这段代码:

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

表示“当它不是最后一个元素时,打印它”。也就是说,这段代码明确不想打印最后一个元素。

我这里假设你的问题描述中没有包含最后的打印语句 - 你是为了解决while循环的问题而添加了最后的打印语句。

你需要的是:

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

这表示“当它是一个实际的元素时,打印它”。

英文:

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.

This code:

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

says "while it's not the last element, print it". i.e., 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.

You need:

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

which says 'while it is an actual element, print it'.

huangapple
  • 本文由 发表于 2020年5月4日 09:30:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61583852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定