双端链表(不是双向链表)的原理

本文介绍了一种双向链表的数据结构实现方法,并详细说明了如何进行表头插入、表尾插入、表头删除及表尾删除等操作。通过具体代码展示了这些操作的时间复杂度以及在不同情况下的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

与单向链表的不同之处在保存有对最后一个链接点的引用(last)

insertFirst:在表头插入一个新的链接点,时间复杂度O(1)

insertLast:在表尾插入一个新的链接点,时间复杂度O(1)

deleteFirst:删除表头的链接点,时间复杂度O(1)

deleteLast::删除表尾的链接点,由于只保存了表尾的链接点,而没有保存表尾的前一个链接点(这里就体现出双向链表的优势了),所以在删除表尾链接点时需要遍历以找到表尾链接点的前一个链接点,需查找N-1次,也就是O(N)


  1. public class FirstLastList {  
  2.     private class Data{  
  3.         private Object obj;  
  4.         private Data next = null;  
  5.           
  6.         Data(Object obj){  
  7.             this.obj = obj;  
  8.         }  
  9.     }  
  10.       
  11.     private Data first = null;  
  12.     private Data last = null;  
  13.       
  14.     public void insertFirst(Object obj){  
  15.         Data data = new Data(obj);  
  16.         if(first == null)  
  17.             last = data;  
  18.         data.next = first;  
  19.         first = data;  
  20.     }  
  21.       
  22.     public void insertLast(Object obj){  
  23.         Data data = new Data(obj);  
  24.         if(first == null){  
  25.             first = data;  
  26.         }else{  
  27.             last.next = data;  
  28.   
  29.         }  
  30.         last = data;  
  31.     }  
  32.       
  33.     public Object deleteFirst() throws Exception{  
  34.           if(first == null)  
  35.              throw new Exception("empty");  
  36.           Data temp = first;  
  37.           if(first.next == null)  
  38.              last = null;  
  39.           first = first.next;  
  40.           return temp.obj;  
  41.    }     
  42.       
  43.     public void deleteLast() throws Exception{  
  44.         if(first == null)  
  45.             throw new Exception("empty");  
  46.         if(first.next == null){  
  47.             first = null;  
  48.             last = null;  
  49.         }else{  
  50.             Data temp = first;  
  51.             while(temp.next != null){  
  52.                 if(temp.next == last){  
  53.                     last = temp;  
  54.                     last.next = null;  
  55.                     break;  
  56.                 }  
  57.                 temp = temp.next;  
  58.             }  
  59.         }  
  60.     }  
  61.       
  62.     public void display(){  
  63.         if(first == null)  
  64.             System.out.println("empty");  
  65.         Data cur = first;  
  66.         while(cur != null){  
  67.             System.out.print(cur.obj.toString() + " -> ");  
  68.             cur = cur.next;  
  69.         }  
  70.         System.out.print("\n");  
  71.     }  
  72.       
  73.     public static void main(String[] args) throws Exception {  
  74.         FirstLastList fll = new FirstLastList();  
  75.         fll.insertFirst(2);  
  76.         fll.insertFirst(1);  
  77.         fll.display();  
  78.         fll.insertLast(3);  
  79.         fll.display();  
  80.         fll.deleteFirst();  
  81.         fll.display();  
  82.         fll.deleteLast();  
  83.         fll.display();  
  84.     }  
  85. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值