双向链表JAVA代码

时间:2024-06-29 18:07:56
  1. //双向链表类
  2. publicclassDoubleLinkList{
  3.  
  4.     //结点类
  5.     publicclassNode{
  6.  
  7.         publicObject data;
  8.         publicNode next;
  9.         publicNode prior;
  10.  
  11.         publicNode(Object obj,Node next,Node prior){
  12.             this.data = obj;
  13.             this.next = next;
  14.             this.prior = prior;
  15.         }
  16.     }
  17.  
  18.     Node head;          //记录头结点信息即可(头结点下标为-1)
  19.     int size;
  20.  
  21.     //初始化一个空链表
  22.     publicDoubleLinkList(){
  23.         //初始化头结点,让头指针指向头结点。并且让当前结点对象等于头结点。
  24.         this.head =newNode(null, null, null);
  25.         this.size =0;
  26.  
  27.     }
  28.  
  29.     //定位
  30.     publicNode locate(int index) throws Exception
  31.     {
  32.         //容错性
  33.         if(index <-1|| index > size)
  34.             thrownewException("参数错误!");
  35.  
  36.         //定位到temp指向第index个(index为下标,从0开始)
  37.         Node temp = head;
  38.         for(int i =-1; i < index; i++)
  39.             if(temp != null)
  40.                 temp = temp.next;
  41.  
  42.         return  temp;
  43.     }
  44.  
  45.  
  46.     publicvoiddelete(int index) throws Exception{
  47.  
  48.         if(isEmpty())
  49.             thrownewException("链表为空,无法删除!");
  50.  
  51.         if(index <0|| index > size -1)
  52.             thrownewException("参数错误!");
  53.  
  54.         Node temp = locate(index -1);               //定位到要操作结点的前一个结点对象
  55.         temp.next = temp.next.next;
  56.         if(temp.next != null)                     //当删除到最后一个元素:temp.next == null
  57.             temp.next.prior = temp;
  58.         size--;
  59.     }
  60.  
  61.  
  62.     publicvoid insert(int index,Object obj) throws Exception{
  63.         //容错性
  64.         if(index <0|| index > size )
  65.             thrownewException("参数错误!");
  66.  
  67.         Node temp = locate(index -1);               //定位到要操作结点的前一个结点对象
  68.         Node p =newNode(obj,temp.next,temp);
  69.         temp.next = p;
  70.         if(p.next != null)                       //当插入到最后一个位置:p.next == null
  71.             p.next.prior = p;
  72.         size++;
  73.     }
  74.  
  75.     public boolean isEmpty(){
  76.         return size ==0;
  77.     }
  78.  
  79.     publicint size(){
  80.         returnthis.size;
  81.     }
  82.  
  83. }
  1. publicclassTest{
  2.  
  3.     publicstaticvoid main(String[] args) throws Exception{
  4.         DoubleLinkListlist=newDoubleLinkList();
  5.         for(int i =0; i <10; i++){
  6.             int temp =((int)(Math.random()*100))%100;
  7.             list.insert(i, temp);
  8.             System.out.print(temp +" ");
  9.         }
  10.  
  11.         list.delete(4);
  12.         System.out.println("\n"+"after deleting the 5th number:");
  13.         for(int i =0; i <list.size; i++){
  14.             System.out.print(list.locate(i).data.toString()+" ");
  15.         }
  16.     }
  17.  
  18. }
输出结果:
  1. 9588231885799228418
  2. after deleting the 5th number:
  3. 95882315799228418