Java 链表

来源:互联网 发布:股东人数查询软件 编辑:程序博客网 时间:2024/05/16 10:38

java中虽没有指针之说,但却也能实现同指针一样的功效。

[java] view plaincopy
  1. ///////////////节点//////////////////////  
  2. class node  
  3. {  
  4.     public double x;  
  5.     public node next;  
  6.     public node pre;  
  7.     node (double x,node n)  
  8.     {  
  9.         this.x =x;  
  10.         this.pre =null;  
  11.         this.next =n;  
  12.     }  
  13.     node (double x)  
  14.     {  
  15.         this.x =x;  
  16.     }  
  17.     public node getnext()  
  18.     {  
  19.         return next;  
  20.     }  
  21.     public void setnext(node next)  
  22.     {  
  23.         this.next =next;  
  24.     }  
  25.     public node getpre()  
  26.     {  
  27.         return pre;  
  28.     }  
  29.     public void setpre(node pre)  
  30.     {  
  31.         this.pre =pre;  
  32.     }  
  33.     /*public node(double x,node next) 
  34.     { 
  35.         this.x=x; this.next =next; 
  36.     }*/  
  37. }  
  38. ///////////////链表实现///////////////////////  
  39. class list  
  40. {  
  41.     public node head,tail;  
  42.     public int length;  
  43.     list()  
  44.     {  
  45.         head= tail =null;  
  46.     }  
  47.     public boolean isEmpty()//判断是否为空    
  48.     {  
  49.         return head==null;  
  50.     }  
  51.     public void addhead(double x)// 加入head节点 一般调用一次  
  52.     {  
  53.         head=new node (x,null);  
  54.         //pt.setnext(head.getnext());  
  55.         //head.setnext(pt);  
  56.         if(tail==null) tail=head;  
  57.     }  
  58.     public void addtail(double x)// 往后 添加节点    
  59.     {  
  60.         if(!isEmpty())  
  61.         {  
  62.             tail.next =new node(x);  
  63.             tail=tail.next ;  
  64.         }else   
  65.         {  
  66.             head=tail =new node(x);  
  67.         }  
  68.     }  
  69.     public void print()  
  70.     {  
  71.         for(node temp=head;temp!=null;temp=temp.next )  
  72.         {  
  73.             System.out.print(temp.x +" ");  
  74.         }  
  75.         System.out.println("");  
  76.     }  
  77.     public void addnow(double x,double y)//在特定的位置 插入一个节点  
  78.     {  
  79.         for(node temp=head;temp!=null;temp=temp.next )  
  80.         {  
  81.             if(temp.x ==x )  
  82.             {  
  83.                 node now= temp.next ;  
  84.                 node ne=new node(y);  
  85.                 temp.next =ne;  
  86.                 ne.next =now;  
  87.             }  
  88.         }  
  89.     }  
  90. }  
  91. //////////////////////////////////////////  
  92. public class test {  
  93.   
  94.     /** 
  95.      * @param args 
  96.      */  
  97.     public static void main(String[] args) {  
  98.         // TODO Auto-generated method stub  
  99.         list k=new list();  
  100.         k.addtail(1);  
  101.         k.addtail(2);k.addtail(4);k.addtail(5);  
  102.         k.addnow(23);//在2之后,插入为3的节点  
  103.         k.print();  
  104.           
  105.     }  

0 0
原创粉丝点击