简体   繁体   English

在 Java 中制作链表

[英]Making a Linked List in Java

I am adding elements in a Linked List via scanning the elements one by one using a for loop, but at the end there is a 0 coming while printing the list.我通过使用 for 循环逐个扫描元素在链表中添加元素,但最后打印列表时出现 0。 The last node is pointing to null but still the list is having one element that is 0. I am providing my source code below and then inputs最后一个节点指向 null,但列表中仍然有一个元素为 0。我在下面提供我的源代码,然后输入

import java.util.Scanner;
import static java.lang.System.out;
class Node{
    int data;
    Node next;
    Node(){
       this.next=null; 
          }
    Node(int data){
        this.data=data;
        this.next=null;
          }
}
public class MyClass{
    public static void main(String args[]) {
        Node head=new Node();
        Node temp=head;
        Scanner sc = new Scanner(System.in);
        int size=sc.nextInt();
        for(int i=1;i<=size;i++){
            temp.data=sc.nextInt();
            temp.next=new Node();
            temp=temp.next;
        }
        temp=null;
        while(head!=null){
            out.print(head.data+" ");
            head=head.next;
        }
    }
  }

Inputs: 5 1 2 3 4 5输入:5 1 2 3 4 5

The next pointer in the last node of a linked list should be null to denote that it is the last node.链表最后一个节点的next指针应该为空,表示它是最后一个节点。

In your case, you are keeping it 'not null'.在您的情况下,您将其保持为“非空”。 In your for loop, just don't instantiate the next pointer if it is the last element you are reading.在您的for循环中,如果next指针是您正在阅读的最后一个元素,则不要实例化它。

    for(int i=1;i<=size;i++){
        temp.data=sc.nextInt();
        if(i != size) {
           temp.next=new Node();
           temp=temp.next;
        }
    }

You're creating the head-node outside of the loop and a new node within it.您正在循环外部创建头节点并在其中创建一个新节点。
You therefore get 1 extra node.因此,您将获得 1 个额外节点。

You might try the following:您可以尝试以下操作:

public static void main(String args[]) {
    Node head=null;
    Node last=null;
    Scanner sc = new Scanner(System.in);
    int size=sc.nextInt();
    for(int i=1;i<=size;i++){
        if (head == null){
            head = new Node();
            last = head;
        } else {
            last.next = new Node();
            last = last.next();
        }
        last.data=sc.nextInt();
    }
    while(head!=null){
        out.print(head.data+" ");
        head=head.next;
    }
}

The problem is that even though you are setting temp=null after you exit your loop, you still have one extra unassigned node.问题是,即使您在退出循环后设置 temp=null,您仍然有一个额外的未分配节点。

The simplest fix is to remove the '=' sign in your for loop so that you exit the loop after your last node and then assign your final value, like this:最简单的解决方法是删除 for 循环中的 '=' 符号,以便在最后一个节点之后退出循环,然后分配最终值,如下所示:

    for(int i=1;i<size;i++){
        temp.data=sc.nextInt();
        temp.next=new Node();
        temp=temp.next;
    }
    temp.data=sc.nextInt();

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

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