栈和队列的java实现

栈单链表实现:没有长度限制,并且出栈和入栈速度都很快

[java]  view plain copy
  1. public class LinkedList {  
  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.       
  13.     public void insertFirst(Object obj){  
  14.         Data data = new Data(obj);  
  15.         data.next = first;  
  16.         first = data;  
  17.     }  
  18.       
  19.     public Object deleteFirst() throws Exception{  
  20.         if(first == null)  
  21.             throw new Exception("empty!");  
  22.         Data temp = first;  
  23.         first = first.next;  
  24.         return temp.obj;  
  25.     }  
  26.               
  27.     public void display(){  
  28.         if(first == null)  
  29.             System.out.println("empty");  
  30.         System.out.print("top -> bottom : | ");  
  31.         Data cur = first;  
  32.         while(cur != null){  
  33.             System.out.print(cur.obj.toString() + " | ");  
  34.             cur = cur.next;  
  35.         }  
  36.         System.out.print("\n");  
  37.     }  
  38. }  
[java]  view plain copy
  1. public class LinkedListStack {  
  2.     private LinkedList ll = new LinkedList();  
  3.       
  4.     public void push(Object obj){  
  5.         ll.insertFirst(obj);  
  6.     }  
  7.       
  8.     public Object pop() throws Exception{  
  9.         return ll.deleteFirst();  
  10.     }  
  11.       
  12.     public void display(){  
  13.         ll.display();  
  14.     }  
  15.       
  16.     public static void main(String[] args) throws Exception{  
  17.         LinkedListStack lls = new LinkedListStack();  
  18.         lls.push(1);  
  19.         lls.push(2);  
  20.         lls.push(3);  
  21.         lls.display();  
  22.         System.out.println(lls.pop());  
  23.         lls.display();  
  24.     }  
  25. }  
[plain]  view plain copy
  1. top -> bottom : | 3 | 2 | 1 |   
  2. 3  
  3. top -> bottom : | 2 | 1 |   

数据项入栈和出栈的时间复杂度都为常数O(1)

队列双端链表实现

[java]  view plain copy
  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 Object deleteFirst() throws Exception{  
  23.         if(first == null)  
  24.             throw new Exception("empty");  
  25.         Data temp = first;  
  26.         if(first.next == null)  
  27.             last = null;  
  28.         first = first.next;  
  29.         return temp.obj;  
  30.     }  
  31.               
  32.     public void display(){  
  33.         if(first == null)  
  34.             System.out.println("empty");  
  35.         System.out.print("first -> last : | ");  
  36.         Data cur = first;  
  37.         while(cur != null){  
  38.             System.out.print(cur.obj.toString() + " | ");  
  39.             cur = cur.next;  
  40.         }  
  41.         System.out.print("\n");  
  42.     }  
  43. }  
[java]  view plain copy
  1. public class FirstLastListQueue {  
  2.     private  FirstLastList fll = new FirstLastList();  
  3.   
  4.     public void push(Object obj){  
  5.         fll.insertLast(obj);  
  6.     }  
  7.       
  8.     public Object pop() throws Exception{  
  9.         return fll.deleteFirst();  
  10.     }  
  11.       
  12.     public void display(){  
  13.         fll.display();  
  14.     }  
  15.       
  16.     public static void main(String[] args) throws Exception{  
  17.         FirstLastListQueue fllq = new FirstLastListQueue();  
  18.         fllq.push(1);  
  19.         fllq.push(2);  
  20.         fllq.push(3);  
  21.         fllq.display();  
  22.         System.out.println(fllq.pop());  
  23.         fllq.display();  
  24.         fllq.push(4);  
  25.         fllq.display();  
  26.     }  
  27. }  
[plain]  view plain copy
  1. first -> last : | 1 | 2 | 3 |   
  2. 1  
  3. first -> last : | 2 | 3 |   
  4. first -> last : | 2 | 3 | 4 |   

长度不受限制并且插入和删除的时间复杂度都为O(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值