Java栈实现

栈数组实现一:优点:入栈和出栈速度快,缺点:长度有限(有时候这也不能算是个缺点)

[java] view plain copy
  1. public class Stack {  
  2.     private int top = -1;  
  3.     private Object[] objs;  
  4.       
  5.     public Stack(int capacity) throws Exception{  
  6.         if(capacity < 0)  
  7.             throw new Exception("Illegal capacity:"+capacity);  
  8.         objs = new Object[capacity];  
  9.     }  
  10.       
  11.     public void push(Object obj) throws Exception{  
  12.         if(top == objs.length - 1)  
  13.             throw new Exception("Stack is full!");  
  14.         objs[++top] = obj;  
  15.     }  
  16.       
  17.     public Object pop() throws Exception{  
  18.         if(top == -1)  
  19.             throw new Exception("Stack is empty!");  
  20.         return objs[top--];  
  21.     }  
  22.       
  23.     public void dispaly(){  
  24.         System.out.print("bottom -> top: | ");  
  25.         for(int i = 0 ; i <= top ; i++){  
  26.             System.out.print(objs[i]+" | ");  
  27.         }  
  28.         System.out.print("\n");  
  29.     }  
  30.       
  31.     public static void main(String[] args) throws Exception{  
  32.         Stack s = new Stack(2);  
  33.         s.push(1);  
  34.         s.push(2);  
  35.         s.dispaly();  
  36.         System.out.println(s.pop());  
  37.         s.dispaly();  
  38.         s.push(99);  
  39.         s.dispaly();  
  40.         s.push(99);  
  41.     }  
  42. }  
[plain] view plain copy
  1. bottom -> top: | 1 | 2 |   
  2. 2  
  3. bottom -> top: | 1 |   
  4. bottom -> top: | 1 | 99 |   
  5. Exception in thread "main" java.lang.Exception: Stack is full!  
  6.     at Stack.push(Stack.java:17)  
  7.     at Stack.main(Stack.java:44)  

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

栈数组实现二:优点:无长度限制,缺点:入栈慢

[java] view plain copy
  1. import java.util.Arrays;  
  2.   
  3. public class UnboundedStack {  
  4.     private int top = -1;  
  5.     private Object[] objs;  
  6.       
  7.     public UnboundedStack() throws Exception{  
  8.         this(10);  
  9.     }  
  10.       
  11.     public UnboundedStack(int capacity) throws Exception{  
  12.         if(capacity < 0)  
  13.             throw new Exception("Illegal capacity:"+capacity);  
  14.         objs = new Object[capacity];  
  15.     }  
  16.       
  17.     public void push(Object obj){  
  18.         if(top == objs.length - 1){  
  19.             this.enlarge();  
  20.         }  
  21.         objs[++top] = obj;  
  22.     }  
  23.       
  24.     public Object pop() throws Exception{  
  25.         if(top == -1)  
  26.             throw new Exception("Stack is empty!");  
  27.         return objs[top--];  
  28.     }  
  29.       
  30.     private void enlarge(){  
  31.         int num = objs.length/3;  
  32.         if(num == 0)  
  33.             num = 1;  
  34.         objs = Arrays.copyOf(objs, objs.length + num);  
  35.     }  
  36.       
  37.     public void dispaly(){  
  38.         System.out.print("bottom -> top: | ");  
  39.         for(int i = 0 ; i <= top ; i++){  
  40.             System.out.print(objs[i]+" | ");  
  41.         }  
  42.         System.out.print("\n");  
  43.     }  
  44.       
  45.     public static void main(String[] args) throws Exception{  
  46.         UnboundedStack us = new UnboundedStack(2);  
  47.         us.push(1);  
  48.         us.push(2);  
  49.         us.dispaly();  
  50.         System.out.println(us.pop());  
  51.         us.dispaly();  
  52.         us.push(99);  
  53.         us.dispaly();  
  54.         us.push(99);  
  55.         us.dispaly();  
  56.     }  
  57. }  
[plain] view plain copy
  1. bottom -> top: | 1 | 2 |   
  2. 2  
  3. bottom -> top: | 1 |   
  4. bottom -> top: | 1 | 99 |   
  5. bottom -> top: | 1 | 99 | 99 |   

由于该栈是由数组实现的,数组的长度是固定的,当栈空间不足时,必须将原数组数据复制到一个更长的数组中,考虑到入栈时或许需要进行数组复制,平均需要复制N/2个数据项,故入栈的时间复杂度为O(N),出栈的时间复杂度依然为O(1)

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

[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)

posted @ 2016-12-22 10:06  Bug开发攻城狮  阅读(124)  评论(0编辑  收藏  举报