链表是一种使用Java实现的数据结构,其中包含插入操作的命令。

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

Linked List using java which has insertion command

问题

import java.util.*;

public class LinkedList {
    Node head;

    static class Node {
        int data;
        Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    public void printList() {
        Node n = head;
        while (n != null) {
            System.out.print(n.data + " ");
            n = n.next;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int dt = 0;
        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 the linked list");
                dt = sc.nextInt();
                llist.head = new Node(dt);
                llist.head.next = null;
            } else {
                Node n = llist.head;
                System.out.println("Enter data in the linked list");
                dt = sc.nextInt();
                while (n.next != null) {
                    n = n.next;
                }
                Node nd = new Node(dt);
                n.next = nd;
            }
            System.out.println("Enter 1 for entry 0 for exit");
            p = sc.nextInt();
        }

        llist.printList();
    }
}
英文:

I created a linked list insertion program where i want to insert node when pressed 1 and exit when 0 is pressed but only first node i.e the head is being inserted not other nodes....
It seems like the first node is working fine but the other nodes are not inserted ..
Please help to identify where the problem lies.

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

答案1

得分: 0

在 while 循环中,当 n.next 不为 null 时,你可以持续循环。这意味着当 n.next 为 null 时,也就是到达链表末尾时,你将退出循环。

else 分支可以这样写:

LinkedList.Node n = llist.head;
System.out.println("输入链表中的数据");
dt = sc.nextInt();
while (n.next != null)
{
    n = n.next;
}
// 若执行到此处,n.next 必定为 null,即我们已经到达链表末尾
n.next = new LinkedList.Node(dt);

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

LinkedList.Node tail = null;
while (p != 0)
{
    if (llist.head == null)
    {
        System.out.println("输入链表中的第一个数据");
        dt = sc.nextInt();
        llist.head = new LinkedList.Node(dt);
        llist.head.next = null;
        tail = llist.head;
    } else {
        System.out.println("输入链表中的数据");
        dt = sc.nextInt();
        
        tail.next = new LinkedList.Node(dt);
        tail = tail.next;
    }
    System.out.println("输入 1 进行插入,输入 0 退出");
    p = sc.nextInt();
}

甚至更好的方式是在 LinkedList 中添加一个 insert 方法:

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("输入链表中的数据");
    dt = sc.nextInt();
    llist.insert(dt);
    System.out.println("输入 1 进行插入,输入 0 退出");
    p = sc.nextInt();
}
英文:

In the while loop, you can keep looping while n.next is not null. This means that you will exit the loop when n.next is null, i.e. you reached the end of the list.

The else branch could be written like this:

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);

Alternatively, you could store the last node that was inserted in a variable. This way you don't need to find the end of the list every time.

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

An even better way to do this would be to add an insert method to 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);
}
}

Then the outer while loop can become:

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

huangapple
  • 本文由 发表于 2020年9月7日 14:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63772483.html
匿名

发表评论

匿名网友

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

确定