将节点添加到链表末尾后出现意外的空值
这是在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 tail.next 更改为 tail.next.next ,如下所示,
您将结束节点插入到由 linkcre 方法创建的空节点的下一个节点。
Change the tail.next to tail.next.next as below,
You are inserting the end node to the next of the null node created by the linkcre method.