将节点添加到链表末尾后出现意外的空值

发布于 2025-01-13 17:47:26 字数 2105 浏览 4 评论 0原文

这是在 LinkedList 末尾插入节点的代码。我首先创建了一个头节点。然后在列表中输入值。之后,我创建了一个结束节点,并在遍历列表末尾时尝试将其与创建的列表的最后一个节点链接。但我得到了两者之间的空值。有人可以纠正我的错误吗?

预期 O/P:2 3 4

O/P 获取:2 3 4 0 5

import java.util.Scanner;
     
 public class SinglyLinkedList {
     SinglyLinkedList next,head,ptr;
     int v;
     void headcre()
     {
         head=new SinglyLinkedList();
         ptr=head;
     }
     void linkcre(int n)
     {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter your values to be added in the list");
         for(int i=0;i<n;i++)
         {
             ptr.v=sc.nextInt();
             ptr.next=new SinglyLinkedList();
             ptr=ptr.next;
         }
         ptr.next=null;
     }
    
     void insertend()
     {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter value to be inserted at end");
 
         
         SinglyLinkedList node=new SinglyLinkedList();   //Creating a node
         node.v=sc.nextInt(); //assigning value to node
         node.next=null;
 
         SinglyLinkedList tail=head;
         
         //loop to traverse at the end of list
         while(tail.next!=null)
         {   
             tail=tail.next;
         }
         
         //pointing the list to the newly created node
         tail.next=node;
         tail=tail.next;
         
        //Printing the list after inserting end node
         SinglyLinkedList ptr3=head;
         while(ptr3!=null)
         {
             System.out.print(ptr3.v+" ");
             ptr3=ptr3.next;
         }
         //System.out.println(); 
     }

     public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter number of values in list");
         int n=sc.nextInt();
         SinglyLinkedList obj=new SinglyLinkedList();
          
         //function to create head
         obj.headcre();
         
         //function to create list
         obj.linkcre(n);
         
         //function to insert node at the end
         obj.insertend();
     }
 }

This is the code to insert a node at the end of a LinkedList. I have first created a head node. Then enter the values in list. After this, I created an end node and tried to linked it with the last node of the created list while traversing at the end of the list. But I am getting the null value in between. Can somebody correct my mistake?

Expected O/P: 2 3 4

O/P getting: 2 3 4 0 5

import java.util.Scanner;
     
 public class SinglyLinkedList {
     SinglyLinkedList next,head,ptr;
     int v;
     void headcre()
     {
         head=new SinglyLinkedList();
         ptr=head;
     }
     void linkcre(int n)
     {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter your values to be added in the list");
         for(int i=0;i<n;i++)
         {
             ptr.v=sc.nextInt();
             ptr.next=new SinglyLinkedList();
             ptr=ptr.next;
         }
         ptr.next=null;
     }
    
     void insertend()
     {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter value to be inserted at end");
 
         
         SinglyLinkedList node=new SinglyLinkedList();   //Creating a node
         node.v=sc.nextInt(); //assigning value to node
         node.next=null;
 
         SinglyLinkedList tail=head;
         
         //loop to traverse at the end of list
         while(tail.next!=null)
         {   
             tail=tail.next;
         }
         
         //pointing the list to the newly created node
         tail.next=node;
         tail=tail.next;
         
        //Printing the list after inserting end node
         SinglyLinkedList ptr3=head;
         while(ptr3!=null)
         {
             System.out.print(ptr3.v+" ");
             ptr3=ptr3.next;
         }
         //System.out.println(); 
     }

     public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter number of values in list");
         int n=sc.nextInt();
         SinglyLinkedList obj=new SinglyLinkedList();
          
         //function to create head
         obj.headcre();
         
         //function to create list
         obj.linkcre(n);
         
         //function to insert node at the end
         obj.insertend();
     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-20 17:47:26

将 tail.next 更改为 tail.next.next ,如下所示,

 //loop to traverse at the end of list
    while(tail.next.next!=null)
    {   
        tail=tail.next;
    }

您将结束节点插入到由 linkcre 方法创建的空节点的下一个节点。

Change the tail.next to tail.next.next as below,

 //loop to traverse at the end of list
    while(tail.next.next!=null)
    {   
        tail=tail.next;
    }

You are inserting the end node to the next of the null node created by the linkcre method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文