0
package practice;
import java.io.*;
import java.util.*;
class Node{
int data;
Node next;
Node(int d){
data=d;
next=null;
}
}
class practice
{
public static Node insert(Node head,int d)
{
if(head==null)
head = new Node(d);
else
{
Node cn = head;
while(cn!=null)
{
cn=cn.next;
cn = new Node(d);
cn= cn.next;
}
}
return head;
}
public static void display(Node head)
{
Node start=head;
while(start!=null)
{
System.out.print(start.data+" ");
start=start.next;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Node head=null;
int N=sc.nextInt();
while(N-->0){
int ele=sc.nextInt();
head=insert(head,ele);
}
display(head);
}
}
我正在尝试创建一个链表作为指向列表的起始节点的节点。并将n
元素添加到列表的尾部。但是当试图显示列表时,我只得到第一个元素作为输出。卡住链接列表 - 只显示第一个节点时显示
例如, 对于输入
3
4
5
6
输出是4
当它应该是4 5 6
试过了! 仍然获得第一个元素作为输出! – Vishal
提交输入'3 4 5 6'(意思是3个项目 - 4,5,6)后,我得到预期的输出和我的变化 - '4 5 6'。 – rgettman
是啊! 明白了! 犯了一个错误! – Vishal