以下是两个类:
import java.io.*;
class Node
{
int data;
Node link;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
void accept()throws IOException
{
System.out.println("Enter a number: ");
data=Integer.parseInt(buf.readLine());
}
}
import java.io.*;
class LinkedList
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
Node start=new Node();
void create()throws IOException
{
Node n=new Node();
n.accept();
start.link=n;
}
void addbeg()throws IOException
{
Node n=new Node();
n.accept();
n.link=start.link;
start.link=n;
}
void addend()throws IOException
{
Node n=new Node();
n.accept();
Node n1=new Node();
n1=start.link;
while(n1.link!=null)
{
n1=n1.link;
}
n1.link=n;
n.link=null;
}
void addafter()throws IOException
{
boolean flag=false;
System.out.println("Enter data after which node is to be added: ");
int d=Integer.parseInt(buf.readLine());
Node n=new Node();
n.accept();
Node n1=start.link;
while(n1.link!=null)
{
if(n1.data==d)
{
flag=true;
break;
}
n1=n1.link;
}
if(flag==true)
{
n.link=n1.link;
n1.link=n;
}
else
System.out.println("Data is not present in the list");
}
void addbefore()throws IOException
{
boolean flag=false;
Node n=new Node();
n.accept();
System.out.println("Enter the data before which node is to be added: ");
int d=Integer.parseInt(buf.readLine());
Node n1=start.link;
Node prev=start;
while(n1.link!=null)
{
if(n1.data==d)
{
n.link=n1;
prev.link=n;
flag=true;
break;
}
else
{
prev=n1;
n1=n1.link;
}
}
if(flag==false)
{
System.out.println("Data not found");
}
}
void delbeg()
{
Node n=start.link;
start.link=n.link;
n.link=null;
}
void delend()
{
Node n=start.link;
Node prev=start;
while(n.link!=null)
{
prev=n;
n=n.link;
}
prev.link=null;
}
void delspecific()throws IOException
{
boolean flag=false;
System.out.println("Enter data to be deleted: ");
int d=Integer.parseInt(buf.readLine());
Node n=start.link;
Node prev=start;
while(n.link!=null)
{
if(n.data==d)
{
prev.link=n.link;
n.link=null;
flag=true;
}
else
{
prev=n;
n=n.link;
}
}
if(flag==false)
{
System.out.println("Data is not present in the list");
}
}
void display()
{
System.out.println("Linked List: ");
Node n=start.link;
do
{
System.out.print(n.data+" -> ");
n=n.link;
}
while(n.link!=null);
System.out.println();
}
void main()throws IOException
{
boolean flag=false;
while(!flag)
{
System.out.println("Enter choice: ");
System.out.println("1. Create ");
System.out.println("2. Add at the Beginning ");
System.out.println("3. Add at the End ");
System.out.println("4. Add after a particular node ");
System.out.println("5. Add before a particular node ");
System.out.println("6. Delete at the Beginning ");
System.out.println("7. Delete at the End ");
System.out.println("8. Delete a specific node ");
System.out.println("9. View ");
System.out.println("10. End ");
int ch=Integer.parseInt(buf.readLine());
switch(ch)
{
case 1:create();
break;
case 2:addbeg();
break;
case 3:addend();
break;
case 4:addafter();
break;
case 5:addbefore();
break;
case 6:delbeg();
break;
case 7:delend();
break;
case 8:delspecific();
break;
case 9:display();
break;
case 10:flag=true;
System.exit(0);
break;
}
}
}
}
当我执行任何功能并查看链表时,最后一个节点总是丢失。过去四个小时我一直在研究这个程序,并且在没有显示最后一个节点时感到很惊讶。请帮忙
答案 0 :(得分:1)
注意: start
节点不持有一个项目,它只是指向第一个项目的节点。
问题出在display
方法:
Node n = start.link;
do {
System.out.print(n.data+" -> ");
n = n.link;
} while(n.link != null);
想象一下n
是倒数第二个节点的点。我们进入循环,打印倒数第二个项目,然后设置n=n.link
,即最后一个节点。接下来我们检查n.link!=null
,结果证明它是假的,因为我们在最后一个节点,因此我们不再进入循环,因此不打印最后一项。
另请注意,如果您显示空列表,则会NullPointerException
:n=start.link
将设置n=null
,因为您没有任何节点。然后,您进入循环并尝试打印,这将引发异常。
可以通过多种方式修复。例如,我们可以在实际打印之前检查当前节点n
:
Node n = start.link;
while(n != null){
System.out.print(n.data+" -> ");
n = n.link;
}