使用java的链表,其中有插入命令

mutmk8jj  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(331)

我创建了一个链表插入程序,我想在按下1时插入节点,按下0时退出,但只插入第一个节点,即头部,而不是其他节点。。。。似乎第一个节点工作正常,但其他节点没有插入。。请帮助确定问题所在。

import java.util.*;
// A simple Java program for traversal of a linked list 
public class LinkedList { 
    Node head; // head of list which is object of Inner Node Class.. thus head.data is a valid statement..

    /* Linked list Node.  This inner class is made static so that 
       main() can access it */
    static class Node { 
        int data; 
        Node next;
        //Constructor
        Node(int d) 
        { 
            data = d; 
            next = null; 
        } // Constructor 
    } 

    /* This function prints contents of linked list starting from head */
    public void printList() 
    { 
        Node n = head; 
        while (n != null) { 
            System.out.print(n.data + " "); 
            n = n.next; 
        } 
    } 

    /* method to create a simple linked list with 3 nodes*/
    public static void main(String[] args) 
    { 
        Scanner sc=new Scanner(System.in);

        int dt=0;
        /* Start with the empty list. */
        LinkedList llist = new LinkedList(); 

        System.out.println("Enter 1 for entry 0 for exit");
        int p = sc.nextInt();
        while(p!=0)
        {
            if(llist.head==null)
            {
                System.out.println("Enter the first data in linked list");
                dt=sc.nextInt();
                llist.head = new Node(dt);
                llist.head.next=null;
            }
          else     //problem is here// 
            {  Node n=llist.head.next;
                 System.out.println("Enter the data in linked list");
                 dt=sc.nextInt();  
                while (n != null)
                { 
                    Node nd = new Node(dt); 
                    nd.next = null;
                }
            }
        System.out.println("Enter 1 for entry 0 for exit");    
        p = sc.nextInt();    
        }

        llist.printList(); 
    } 
}
wgx48brx

wgx48brx1#

在while循环中,您可以在 n.next 不为空。这意味着您将在 n.next 为空,即您到达了列表的末尾。
这个 else 分支可以这样写:

LinkedList.Node n=llist.head;
System.out.println("Enter the data in linked list");
dt=sc.nextInt();
while (n.next != null)
{
    n = n.next;
}
// if we reach here, n.next must be null, i.e. we have reached the end of the list
n.next = new LinkedList.Node(dt);

或者,可以存储插入变量中的最后一个节点。这样你就不需要每次都找到列表的末尾。

LinkedList.Node tail = null;
while(p!=0)
{
    if(llist.head==null)
    {
        System.out.println("Enter the first data in linked list");
        dt=sc.nextInt();
        llist.head = new LinkedList.Node(dt);
        llist.head.next=null;
        tail = llist.head;
    } else {
        System.out.println("Enter the data in linked list");
        dt=sc.nextInt();

        tail.next = new LinkedList.Node(dt);
        tail = tail.next;
    }
    System.out.println("Enter 1 for entry 0 for exit");
    p = sc.nextInt();
}

更好的方法是添加 insert 方法 LinkedList :

public void insert(int data) {
    if (head == null) {
        head = new Node(data);
    } else {
        LinkedList.Node n = head;
        while (n.next != null) {
            n = n.next;
        }
        n.next = new Node(data);
    }
}

然后外部while循环可以变成:

while(p!=0)
{
    System.out.println("Enter the data in linked list");
    dt=sc.nextInt();
    llist.insert(dt);
    System.out.println("Enter 1 for entry 0 for exit");
    p = sc.nextInt();
}

相关问题