本帖最后由 王琪 于 2014-3-6 12:20 编辑
- import java.util.*;
- public class java3{
- public static void main(String[]args){
- LinkedList2 li=new LinkedList2 ();
- Tianjia(li);
- System.out.println("添加的节点为:姓名 学号 年龄");
- System.out.print(li);
- System.out.println("取出节点的序号:");
- Integer k=new Integer(new Scanner(System.in).next());
- DATA w;
- while(true){
- try{
- w=Quchhu(li,k);
- }catch(Exception e){System.out.println("不存在 please put in new");
- k=Integer.valueOf(new Scanner(System.in).next());
- continue;}
- break;
- }
- System.out.println("移除成功.."+w);
- System.out.println("查找的key请输入..");
- String s =new Scanner (System.in).nextLine().trim();
- w=chazhao(li,s);
- if(w!=null)System.out.println("查找成功\n"+w);
- }
-
- static void Tianjia(LinkedList2 a){
- String key="",name="";Integer age=0;
- String[]s;
- try{
- System.out.println("输入添加的节点:{ 姓名 学号 年龄 }:");
- s=(new Scanner(System.in).nextLine()).split(" ");
- name=s[0].trim();
- key=s[1].trim();
- age=Integer.valueOf(s[2].trim());
- while(age!=0){
- a.add(new DATA(name,key,age));
- System.out.println("添加成功..");
- System.out.println("输入添加的节点:{ 姓名 学号 年龄 }:");
- s=(new Scanner(System.in).nextLine()).split(" ");
- name=s[0].trim();
- key=s[1].trim();
- age=Integer.valueOf(s[2].trim());
- }
- }catch(Exception e){
- System.out.println("添加不成功..");
- Tianjia(a);}
- System.out.println(a);
- }
- static DATA Quchhu(LinkedList2 a,int i)throws Exception {
- DATA b=(DATA)a.get(i);
- a.remove(i);
- return b;
- }
- static void chazhao(LinkedList2 a,int i){
- System.out.println(a.get(i));
- }
- static DATA chazhao(LinkedList2 a,String s){
- ListIterator<Object> it=a.listIterator();
- while(it.hasNext()){
- Object o=it.next();
- if(((DATA)o).equals(s.trim())){
- return (DATA)o;
- }
- }
- return null;
- }
- }
- class LinkedList2 extends LinkedList<Object>{
- public String toString(){
- String s="";
- int j=0;
- ListIterator<Object> i=this.listIterator();
- while(i.hasNext()){
- s+=(j++)+":"+String.valueOf(i.next())+"\n";
- }
- return s;
- }
- }
- class DATA{
- static String key;
- static String name;
- Integer age;
- public DATA(){}
- public DATA(String name,String key,Integer age){
- this.key=key;
- this.name=name;
- this.age=age;
- }
- public String getkey(){
- return key;
- }
- public String toString(){
- return name+" "+key+" "+age;
- }
- }
复制代码
class LinkedList2 extends LinkedList<Object>{
public String toString(){
String s="";
int j=0;
ListIterator<Object> i=this.listIterator();
while(i.hasNext()){
s+=(j++)+":"+String.valueOf(i.next())+"\n";
}
return s;
}
}
改了原来链表的输出,不知是不是这样改
|