日期:2014-05-20  浏览次数:21232 次

链表排序
下面的程序先创建了一个链表。然后对链表进行排序。
可是我在运行这个程序不知道为什么对输入的数据不处理(这是Java   in   Algorithm里的程序),什么输出都没有,请大家帮忙看看是怎么回事。谢谢
//这是一个输入程序
import   java.io.*;
public   class   In   {

private   static   int   c;
        private   static   boolean   blank(){
        return   Character.isWhitespace((char)c);
        }
private   static   void   readC(){
try{
c=System.in.read();
}catch(IOException   e){c=-1;}
}
public   static   void   init(){readC();}
public   static   boolean   empty(){return   c==-1;}
public   static   String   getString(){
if(empty())return   null;
String   s= " ";
do{s+=(char)c;readC();}
while(!(empty()||blank()));
while(!empty()&&blank())readC();
return   s;
}
public   static   int   getInt(){
return   Integer.parseInt(getString());
}
public   static   double   getDouble(){
return   Double.parseDouble(getString());
}

}


//下面是主程序

public   class   ListSortExample   {

static   class   Node{
int   val;Node   next;
public   Node(int   v,Node   t){val=v;next=t;}
}

static   Node   create(){
Node   a=new   Node(0,null);
for(In.init();!In.empty();)
a.next=new   Node(In.getInt(),a.next);
return   a;
}

static   Node   sort(Node   a){
Node   t,x,u,   b=new   Node(0,null);
while(a.next!=null){
t=a.next;u=t.next;a.next=u;
for(x=b;x.next!=null;x=x.next)
if(x.next.val> t.val)break;
t.next=x.next;x.next=t;
}
return   b;
}

static   void   print(Node   h){
for(Node   t=h.next;t!=null;t=t.next){
System.out.println(t.val   + "   ");
}
}
public   static   void   main(String[]   args)   {

                        print(sort(create()));
}

}




------解决方案--------------------
等我拿回去在研究。。。UP。。。DOWN
------解决方案--------------------
你的读入数据的地方有点问题。

System.in.read();这个东西一定要回车才会把数据输入到你的程序里面。

回车是/r/n

但是System.in.read();这个东西好像会把/r也读到你的程序里面

(好久不用有点记不清楚了,你可以把读入的asc码打出来就能看到)

这样你转成int的时候应该会抛出异常。。。。。。。。(你没写,有没有异常?)


可以使用String s = new BufferedReader(new InputStreamReader(System.in)).readLine()

或者判断的时候判断10,13是结尾具体用哪个判断了我忘了,一个是/r一个是/n的asc码


其他逻辑我没看。这里搞定后面的你如果不会debug的话可以在每一句后面添加system.out.println( " ");来查找错误。