将节点添加到链表末尾后出现意外的空值
Unexpected null value after adding node to the end of linked list
这是在链表末尾插入节点的代码。我首先创建了一个头节点。然后在列表中输入值。在此之后,我创建了一个结束节点,并尝试在遍历列表末尾时将其与创建列表的最后一个节点链接起来。但我得到的是介于两者之间的空值。有人可以纠正我的错误吗?
预期 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();
}
}
把tail.next改成tail.next。接下来如下,
//loop to traverse at the end of list
while(tail.next.next!=null)
{
tail=tail.next;
}
您正在将结束节点插入到由 linkcre 方法创建的空节点的下一个节点。
这是在链表末尾插入节点的代码。我首先创建了一个头节点。然后在列表中输入值。在此之后,我创建了一个结束节点,并尝试在遍历列表末尾时将其与创建列表的最后一个节点链接起来。但我得到的是介于两者之间的空值。有人可以纠正我的错误吗?
预期 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();
}
}
把tail.next改成tail.next。接下来如下,
//loop to traverse at the end of list
while(tail.next.next!=null)
{
tail=tail.next;
}
您正在将结束节点插入到由 linkcre 方法创建的空节点的下一个节点。