我已經寫了下面的代碼,其中方法rev(list,list)
不工作。請幫我確定有什麼問題。程序在Java中反轉鏈接列表
import java.io.*;
public class list
{
int d;
list l;
list()
{
d=0;
l=null;
}
void create()throws IOException
{
int n;// store number of nodes
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the first data");
this.d=Integer.parseInt(br.readLine());
System.out.println("Enter number of nodes to be made");
n=Integer.parseInt(br.readLine());
list temp;
list ptr=this;
for(int i=1;i<n;i++)
{
temp=new list();
System.out.println("Enter the next data");
temp.d=Integer.parseInt(br.readLine());
temp.l=null;
ptr.l=temp;
temp=null;
ptr=ptr.l;
}
}
void delete(list lst, int n)
{
list ptr=lst;
list ptr1=ptr;
int c=1;
while(c<n)
{
ptr1=ptr;
ptr=ptr.l;
c++;
}
ptr1.l=ptr.l;
ptr.l=null;
ptr=null;
ptr1=null;
}
void insertmid(list lst,int x, int n)
{
list temp=new list();
temp.d=x;
temp.l=null;
list ptr=lst;
int c=1;
while(c<n)
{
ptr=ptr.l;
c++;
}
temp.l=ptr.l;
ptr.l=temp;
}
void rev(list lst,list lst1)
{
lst1=null;
list ptr=new list();
while(lst!=null)
{
ptr =lst;
lst=lst.l;
ptr.l=lst1;
lst1=ptr;
}
}
void display()
{
list ptr=this;
while(ptr!=null)
{
System.out.print(ptr.d+"\t");
ptr=ptr.l;
}
System.out.println();
}
public static void main(String args[])throws IOException
{
list l2=new list();
list l3=new list();
l2.create();
l2.display();
l2.insertmid(l2,14,2);
l2.display();
l2.delete(l2, 3);
l2.display();
l2.rev(l2,l3);
l2.display();
}
}
「不工作」。不是一個工作錯誤的描述。請更具體一些。是否有編譯時間錯誤或打印出「今天我正在罷工,稍後再試。」? – zapl 2014-11-05 16:13:17
反轉不起作用 – 2014-11-05 16:16:28
只有列表中的第一個數字被打印 – 2014-11-05 16:16:44