下面的程序先创建了一个链表。然后对链表进行排序。
可是我在运行这个程序不知道为什么对输入的数据不处理(这是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()));
}}
解决方案 »
- 这段代码创建了几个对象
- 在netbeans7.1.2里,点击一个项目的菜单项后,如何在主窗口菜单栏下面的JScrollPane打开外部(同一项目)已拖拽设计好的JFrame窗体?
- 数据库源码
- Java中的代码块种类
- 真的是郁闷`谁能帮下啊`急死人了
- 100分求!怎样用JAVA写一个浏览网页的应用呢?
- 【在线急救】在jsp页面打印byte[]数据,多了一个字节数据出来
- 对象序列化的问题?
- 请问在JAVA中怎样实现目录的树状表现形式,接点从数据库中取?
- 谁能解释一下为什么 sleep() 是静态方法?
- String.trim的方法是个大BUG???
- 对象的clone方法实现复制对象与用构造函数和属性方法复制对象有区别吗?
可以使用String s = new BufferedReader(new InputStreamReader(System.in)).readLine()或者判断的时候判断10,13是结尾具体用哪个判断了我忘了,一个是/r一个是/n的asc码
其他逻辑我没看。这里搞定后面的你如果不会debug的话可以在每一句后面添加system.out.println("");来查找错误。
看你的说法,我很怀疑你是运行程序后就等着,没有输入数据