op="";
try{
op=in.readLine();
}catch (IOException e){}
option=0;
try{
option=Integer.valueOf(op).intValue();
}catch (NumberFormatException e){
System.out.println("\n Please input (1,2,3,4,5) ...");
}
switch(option){
case 1:
obj.insert_f();
break;
case 2:
obj.delete_f();
break;
case 3:
obj.modify_f();
break;
case 4:
obj.dispiay_f();
break;
case 5:
obj.save_f();
System.exit(0);
}
}while (true);
}
}
3、双向链表
import java.io.*;
class student {
public String name ="";
public int score=0;
public student llink;
public student rlink;
}
class DoubleList{
static student prev,ptr,head,current;
static DataInputStream infile;
static DataOutputStream outfile;
DoubleList (){
ptr = new student ();
ptr.name = "0";
ptr.llink = ptr;
ptr.rlink = ptr;
head = ptr ;
}
public static void anykey_f(){
char tChar;
System.out.println(" press any key to continue ... ");
try {
tChar = (char) System.in.read () ;
} catch (IOException e) {}
}
public static void load_f()
{
boolean done =false;
System.out.print("File loading...");
try{
infile=new DataInputStream(
new FileInputStream("dblink.dat"));
} catch(IOException e) {
System.err.println("failed!");
System.err.println("dblink.dat not found!");
return;
}
while(done==false)
{
ptr=new student();
try{
ptr.name=infile.readUTF();
ptr.score=infile.readInt();
}
catch(EOFException eof){
done=true;
}
catch(IOException e){}
if(done==true)
ptr=null;
}
System.out.println("OK!");
try{
infile.close();
}catch(IOException e){}
}
public static void save_f()
{
student node;
System.out.print("File saveing...");
try{
outfile=new DataOutputStream(new FileOutputStream("dblink.dat")); }catch(IOException e){
System.out.println("failed!");
return;
}
node=head;
do{
try{
outfile.writeUTF(node.name);
outfile.writeInt(node.score);
}catch(IOException e){}
node=node.rlink;
}while(node!=head);
System.out.println("OK!");
try{
outfile.close();
}catch(IOException e) {}
}
public static void insert_f(){
DataInputStream in = new DataInputStream(System.in);
String name_t="" , st="";
ptr = new student();
System.out.print("\n please enter student name : ");
System.out.flush();
try {
ptr.name = in.readLine();
} catch (IOException e) {}
System.out.print(" please enter student score: ");
System.out.flush();
try {
st = in.readLine();
} catch (IOException e) {}
try {
ptr.score = Integer.valueOf (st).intValue();
} catch (NumberFormatException e) {}
System.out.println("");
prev = head;
current = head.rlink;
while ((current != head) && (current.score >= ptr.score)) {
prev = current;
current = current.rlink;
}
ptr.rlink = current;
ptr.llink = prev;
prev.rlink = ptr;
current.llink = ptr;
}
public static void delete_f(){
DataInputStream in=new DataInputStream(System.in);
String del_name="";
int count =0;
student clear;
if(head.rlink==head)
System.out.println("\n No student record !!");
else{
System.out.print("\n Delete student name:");
System.out.flush();
try{
del_name=in.readLine();
}catch (IOException e){}
prev=head;
current=head.rlink;
while((current.rlink!=head)&&(! del_name.equals(current.name))){ prev=current;
current=current.rlink;
}
prev.rlink=current.rlink ;
current.rlink.llink=prev;
current=null;
System.out.println("Student "+del_name +"record(s) deleted!\n"); }if (current==head){
System.out.println("Student "+del_name +"not found!\n"); }
anykey_f();
}public static void modify_f(){
DataInputStream in=new DataInputStream(System.in);
int count=0;
String n_temp="",s_temp="";