1. package com.tw.dst.link;  
  2.  
  3. /**  
  4.  * <p>链表类</p>  
  5.  * @author tangw  
  6.  *  
  7.  */ 
  8. public class Link {  
  9.     public int iData;  
  10.     public double dData;  
  11.     public Link next;  
  12.       
  13.     public Link(int idata,double ddata){  
  14.         this.iData = idata;  
  15.         this.dData = ddata;  
  16.     }  
  17.       
  18.     public void displayLink(){  
  19.         System.out.println(" idata="+iData+"  ddata:"+dData);  
  20.     }  
  21.  
  22. }  
 
  1. package com.tw.dst.link;  
  2.  
  3.  
  4. /**  
  5.  * <p>单链表</p>  
  6.  * @author tangw 2010-11-29  
  7.  *  
  8.  */ 
  9. public class LinkList {  
  10.       
  11.     private Link first;//第一个链结点的引用  
  12.  
  13.     public LinkList() {  
  14.         first = null;//引用类型初始化会自动赋值为null,此处不是必须的  
  15.     }  
  16.     //判断是否为空  
  17.     public boolean isEmpty(){  
  18.         return (first == null);  
  19.     }  
  20.     //插入数据  
  21.     public void insertFirst(int id,double dd){  
  22.         Link link = new Link(id,dd);  
  23.         link.next = first;  
  24.         first = link;  
  25.     }  
  26.       
  27.     //删除数据  
  28.     public Link deleteFirst(){  
  29.         Link temp = first;  
  30.         first = first.next;  
  31.         return temp;      
  32.     }  
  33.     //查找指定节点  
  34.     public Link find(int key){  
  35.         Link current = first;  
  36.         while( current.iData!=key ){  
  37.             if( current.next == null){  
  38.                 return null;  
  39.             }else{  
  40.                 current = current.next;//将当前对象的下一个引用置为当前  
  41.             }  
  42.         }  
  43.         return current;  
  44.     }  
  45.       
  46.     //删除指定节点  
  47.     public Link delete(int key){  
  48.         Link current = first;   //存储当前对象  
  49.         Link previous = first;  //存储先前对象  
  50.         while(current.iData !=key ){  
  51.             if(current.next ==null){  
  52.                 return null;  
  53.             }else{  
  54.                 previous = current;  
  55.                 current = current.next;  
  56.             }  
  57.         }  
  58.         if( current == first){  
  59.             first = first.next;  
  60.         }else{  
  61.             previous.next = current.next;  
  62.         }  
  63.         return current;  
  64.     }  
  65.       
  66.       
  67.       
  68.       
  69.       
  70.     //输出链表数据  
  71.     public void displayList(){  
  72.         System.out.println("list (first-->last)");  
  73.         Link current = first;  
  74.         while(current!=null){  
  75.             current.displayLink();  
  76.             current =current.next;  
  77.         }  
  78.         System.out.println("");  
  79.     }  
  80.       
  81.     /**  
  82.      * @param args  
  83.      */ 
  84.     public static void main(String[] args) {  
  85.         //----1---插入  
  86.         LinkList linkList = new LinkList();  
  87.         linkList.insertFirst(222.99);  
  88.         linkList.insertFirst(444.99);  
  89.         linkList.insertFirst(666.99);  
  90.           
  91.         linkList.displayList();  
  92.         //---2--删除  
  93.         /*while(!linkList.isEmpty()){  
  94.             Link aLink = linkList.deleteFirst();  
  95.             System.out.println("--deleted");  
  96.             aLink.displayLink();  
  97.         }  
  98.           
  99.         linkList.displayList();*/ 
  100.           
  101.         //==3 查找=====  
  102.         Link link = linkList.find(44);  
  103.         if(null !=link){  
  104.             System.out.println("---idata:"+link.dData);  
  105.         }else{  
  106.             System.out.println("-find---no ");  
  107.         }  
  108.           
  109.         //---4: 删除指定元素  
  110.         Link delLink = linkList.delete(44);  
  111.         ifnull != delLink){  
  112.             System.out.println("----del link:"+delLink.dData);  
  113.         }else{  
  114.             System.out.println("-delete---no ");  
  115.         }  
  116.         linkList.displayList();  
  117.     }  
  118.