随笔 - 0  文章 - 3  trackbacks - 0
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章档案(50)

搜索

  •  

最新评论

首先要写一个节点对象:

java 代码




  1. public class Node {   

  2.     public Node previous;   

  3.     public Node next;   

  4.     private Object value;   

  5.        

  6.     public Node(Object data){   

  7.         this.value  = data;   

  8.         this.next = null;   

  9.         this.previous = null;   

  10.     }    

  11. }   



java 代码




  1. public class List {   

  2.   

  3.     private Node head;   

  4.     private Node tail;   

  5.     private int size;   

  6.        

  7.     public void reset(){   

  8.         head = null;   

  9.         tail = null;   

  10.         size = 0;   

  11.     }   

  12.        

  13.     //在链表的最后添加一个节点   

  14.     public void addNode(Object obj){   

  15.         Node node = new Node(obj);   

  16.         if(size ==0){   

  17.             head = node;   

  18.             tail = node;   

  19.         }   

  20.         else{   

  21.             node.previous = tail;   

  22.             tail.next = node;   

  23.             node.next = null;   

  24.             tail = node;   

  25.                

  26.         }   

  27.         size++;   

  28.     }   

  29.        

  30.    //在链表的某个位置插入一个对象,第一个节点之前的位置为0   

  31.     public void insertNode(Object obj,int position){   

  32.         if(position<0 || position>size){   

  33.             System.out.println("insert position is out of band!");   

  34.             return;   

  35.         }   

  36.         Node node = new Node(obj);   

  37.         if(size ==0){   

  38.             head = node;    

  39.             tail = node;   

  40.         }   

  41.         else if(position == 0){   

  42.             node.next = head;   

  43.             node.next.previous = node;   

  44.             head = node;   

  45.         }   

  46.         else{   

  47.             Node current = head;   

  48.             for(int i = 1;i position;i++){

  49.                 current = current.next;   

  50.             }   

  51.             node.previous = current;   

  52.             node.next = current.next;   

  53.             if(position == size){   

  54.                 current.next = node;   

  55.                 tail = node;   

  56.             }   

  57.             else {   

  58.                 current.next.previous = node;   

  59.                 current.next = node;    

  60.             }   

  61.         }   

  62.         size++;   

  63.     }   

  64.        

  65.    //删除一个节点   

  66.     public void deleteNode(int position){   

  67.         if(position <= 0 || position >size){   

  68.             System.out.println("delete position is out of band!");   

  69.             return;   

  70.         }   

  71.         Node current = head;   

  72.         if(position ==1){   

  73.             if(size == 1){   

  74.                 head = null;   

  75.                 tail = null;   

  76.             }   

  77.             else{   

  78.                 head = current.next;   

  79.                 current.next.previous = null;   

  80.             }   

  81.         }   

  82.         else{   

  83.             for(int i = 1;i position;i++){

  84.                 current = current.next;   

  85.             }   

  86.             if(position == size){   

  87.                 tail = current.previous;   

  88.                 tail.next = null;   

  89.                    

  90.             }   

  91.             else{   

  92.                  current.previous.next = current.next;   

  93.               current.next.previous = current.previous;   

  94.             }    

  95.         }   

  96.         current.next = null;   

  97.         current.previous = null;   

  98.         size--;   

  99.     }   

  100. }   

  101.   






评论也很精彩,请点击查看精彩评论。欢迎您也添加评论。查看详细 >>





JavaEye推荐
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA
广州:急招 JAVA开发经理/系统架构师(10-15K/月)也招聘java程序员
与Hibernate之父面对面-4月19日 Gavin King上海交流研讨会
高薪工作机会 美国法国上海 15-20k/月 J2EE SA



文章来源: http://xiaozhe.javaeye.com/blog/59147
posted on 2007-03-11 22:47 xiaozhe 阅读(359) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: