双向链表

  1 ***********IntDllNode.java*******************
  2 public class IntDllNode {
  3     public IntDllNode next,prve;
  4     public int value;
  5     public IntDllNode(int e){
  6         value=e;
  7         next=prve=null;
  8     } 
  9     public IntDllNode(int e,IntDllNode n1,IntDllNode n2){
 10         this.value=e;
 11         next=n1;
 12         prve=n2;
 13     }
 14 }
 15 
 16 *************IntDllList.java********************
 17 
 18 
 19 public class IntDllList {
 20 
 21     /**
 22      * @param args
 23      */
 24     IntDllNode head,tail;
 25     // 构造函数
 26     public IntDllList(){
 27         head=tail=null;
 28     }
 29     //判断是否为空
 30     public boolean isEmpty(){
 31         return (head==null);
 32     }
 33     //从头部插入新节点
 34     public void addToHead(int e){
 35         if(head==null){
 36             head=tail=new IntDllNode(e);
 37         }
 38         else{
 39             IntDllNode temp=new IntDllNode(e,head,null);
 40             head.prve=temp;
 41             head=temp;
 42         }
 43     }
 44     //从尾部插入新节点
 45     public void addToTail(int e){
 46         if(tail==null){
 47             head=tail=new IntDllNode(e);
 48         }
 49         else{
 50             IntDllNode temp=new IntDllNode(e);
 51             temp.prve=tail;
 52             tail.next=temp;
 53             tail=temp;
 54         }
 55     }
 56     //从头部删除一个节点
 57     public int deleteFromHead(){
 58         int result;
 59         if(isEmpty()){
 60             System.out.println("The list is empty");
 61             return -1;
 62         }
 63         if(head==tail){
 64             result=head.value;
 65             head=tail=null;
 66             return result;
 67         }
 68         else{
 69             result=head.value;
 70             head=head.next;
 71             head.prve=null;
 72             return result;
 73         }
 74     }
 75     //从尾部删除一个节点
 76     public int deleteFromTail(){
 77         int result;
 78         if(isEmpty()){
 79             System.out.println("The list is empty");
 80             return -1;
 81         }
 82         if(head==tail){
 83             result=tail.value;
 84             head=tail=null;
 85             return result;
 86         }
 87         else{
 88             result=tail.value;
 89             tail=tail.prve;
 90             tail.next=null;
 91             return result;
 92         }
 93     }
 94     //正向打印整个链表
 95     public void printAll(){
 96         if(isEmpty()){
 97             System.out.println("The list is empty");
 98             return ;
 99         }
100         IntDllNode temp=head;
101         while(temp!=null){
102             System.out.println(temp.value);
103             temp=temp.next;
104         }
105     }
106     //反向打印整个链表
107     public void printAllreverse(){
108         if(isEmpty()){
109             System.out.println("The list is empty");
110             return ;
111         }
112         IntDllNode temp=tail;
113         while(temp!=null){
114             System.out.println(temp.value);
115             temp=temp.prve;
116         }
117     }
118     
119     public static void main(String[] args) {
120         // TODO Auto-generated method stub
121         IntDllList list=new IntDllList();
122         list.addToHead(1);
123         list.addToHead(2);
124         list.addToHead(3);
125         list.addToHead(4);
126         list.addToHead(5);
127         list.addToTail(6);
128         list.addToTail(7);
129         list.addToTail(8);
130         list.addToTail(9);
131         list.addToTail(0);
132         list.printAll();
133         System.out.println("**");
134         list.printAllreverse();
135     }
136 
137 }

 

posted on 2013-11-16 18:25  随风蔷薇  阅读(164)  评论(0编辑  收藏  举报