下面的程序先创建了一个链表。然后对链表进行排序。
可是我在运行这个程序不知道为什么对输入的数据不处理(这是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()));
}}
可是我在运行这个程序不知道为什么对输入的数据不处理(这是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()));
}}
可以使用String s = new BufferedReader(new InputStreamReader(System.in)).readLine()或者判断的时候判断10,13是结尾具体用哪个判断了我忘了,一个是/r一个是/n的asc码
其他逻辑我没看。这里搞定后面的你如果不会debug的话可以在每一句后面添加system.out.println("");来查找错误。
看你的说法,我很怀疑你是运行程序后就等着,没有输入数据