呵呵,搞出来了,谢谢三楼的提醒,我把int 都改成了 String 型就ok了!!谢谢了 贴出程序。。
不过发现一个比较弱的问题,说出来不要见笑。。。就是我先用for循环时没有打分号,编译却通过了
但是delete功能就不能实现了,总是随机的删除一个数,不知道是怎么回事,能再给个解释么 谢谢了
import java.io.*;
class intsllnode{
public String info;
public intsllnode next;
public intsllnode (String i) {
this (i,null);
}
public intsllnode (String i,intsllnode n) {
info=i;next=n;
}
}
public class intsllist{
protected intsllnode head,tail;
private String input;
private BufferedReader buffer = new BufferedReader(
new InputStreamReader(System.in));
public intsllist() {
head=tail=null;
}
public boolean isempty() {
return head==null;
}
public void printall(){
for (intsllnode tmp=head;
tmp!=null;
tmp=tmp.next )
System.out.print(tmp.info+" ");
}
public String find(){
String el=getstring("find this num");
intsllnode tmp=head;
for (;tmp!=null&&!el.equals(tmp.info);
tmp=tmp.next);
if (tmp==null)
return null;
else return tmp.info;
}
public void delete (){
String el=getstring("I want delete this num");
if(head!=null)
if (el.equals(head.info))
head=head.next;
else{
intsllnode pred=head,tmp=head.next;
for(;tmp!=null&&!el.equals(tmp.info);
pred=pred.next,tmp=tmp.next);
if (tmp!=null)
pred.next=tmp.next;
}
}
public void addtotail(){
String el=getstring("insert a num");
if (!isempty()){
tail.next=new intsllnode(el);
tail=tail.next;
}
else head=tail=new intsllnode(el);
}
private String getstring(String msg) {
System.out.print(msg + " ");
System.out.flush();
try {
input = buffer.readLine();
} catch(IOException io) {
}
return input;
}
public void run() {
while (true) {
char option = getstring("\nEnter one of the following options:\n" +
"1. addtotail\n" +
"2. find\n" +
"3. delete\n" +"4.statues\n"+
"Your option:").charAt(0);
switch (option) {
case '1': addtotail(); break;
case '2': System.out.print(find()); break;
case '3': delete(); break;
case '4': printall(); break;
case '5': return;
default: System.out.println("Wrong option, try again.");
}
}
}
public static void main(String args[]) {
(new intsllist()).run();
}
}