Stack介绍以及设计一个有getMin 功能的栈

来源:互联网 发布:微传单软件 编辑:程序博客网 时间:2024/06/06 01:15

Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来

Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用


[plain] view plain copy
  1. E push(E item)   
  2.          把项压入堆栈顶部。   
  3. E pop()   
  4.          移除堆栈顶部的对象,并作为此函数的值返回该对象。   
  5. E peek()   
  6.          查看堆栈顶部的对象,但不从堆栈中移除它。   
  7. boolean empty()   
  8.          测试堆栈是否为空。    
  9. int search(Object o)   
  10.          返回对象在堆栈中的位置,以 1 为基数。  

Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组( a growable array of objects)那么这个数组的哪里作为Stack的栈顶,哪里作为Stack的栈底?

答案只能从源代码中寻找,jdk1.6:

[java] view plain copy
  1. public class Stack<E> extends Vector<E> {  
  2.     /** 
  3.      * Creates an empty Stack. 
  4.      */  
  5.     public Stack() {  
  6.     }  
  7.   
  8.     /** 
  9.      * Pushes an item onto the top of this stack. This has exactly 
  10.      * the same effect as: 
  11.      * <blockquote><pre> 
  12.      * addElement(item)</pre></blockquote> 
  13.      * 
  14.      * @param   item   the item to be pushed onto this stack. 
  15.      * @return  the <code>item</code> argument. 
  16.      * @see     java.util.Vector#addElement 
  17.      */  
  18.     public E push(E item) {  
  19.     addElement(item);  
  20.   
  21.     return item;  
  22.     }  
  23.   
  24.     /** 
  25.      * Removes the object at the top of this stack and returns that 
  26.      * object as the value of this function. 
  27.      * 
  28.      * @return     The object at the top of this stack (the last item 
  29.      *             of the <tt>Vector</tt> object). 
  30.      * @exception  EmptyStackException  if this stack is empty. 
  31.      */  
  32.     public synchronized E pop() {  
  33.     E   obj;  
  34.     int len = size();  
  35.   
  36.     obj = peek();  
  37.     removeElementAt(len - 1);  
  38.   
  39.     return obj;  
  40.     }  
  41.   
  42.     /** 
  43.      * Looks at the object at the top of this stack without removing it 
  44.      * from the stack. 
  45.      * 
  46.      * @return     the object at the top of this stack (the last item 
  47.      *             of the <tt>Vector</tt> object). 
  48.      * @exception  EmptyStackException  if this stack is empty. 
  49.      */  
  50.     public synchronized E peek() {  
  51.     int len = size();  
  52.   
  53.     if (len == 0)  
  54.         throw new EmptyStackException();  
  55.     return elementAt(len - 1);  
  56.     }  
  57.   
  58.     /** 
  59.      * Tests if this stack is empty. 
  60.      * 
  61.      * @return  <code>true</code> if and only if this stack contains 
  62.      *          no items; <code>false</code> otherwise. 
  63.      */  
  64.     public boolean empty() {  
  65.     return size() == 0;  
  66.     }  
  67.   
  68.     /** 
  69.      * Returns the 1-based position where an object is on this stack. 
  70.      * If the object <tt>o</tt> occurs as an item in this stack, this 
  71.      * method returns the distance from the top of the stack of the 
  72.      * occurrence nearest the top of the stack; the topmost item on the 
  73.      * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> 
  74.      * method is used to compare <tt>o</tt> to the 
  75.      * items in this stack. 
  76.      * 
  77.      * @param   o   the desired object. 
  78.      * @return  the 1-based position from the top of the stack where 
  79.      *          the object is located; the return value <code>-1</code> 
  80.      *          indicates that the object is not on the stack. 
  81.      */  
  82.     public synchronized int search(Object o) {  
  83.     int i = lastIndexOf(o);  
  84.   
  85.     if (i >= 0) {  
  86.         return size() - i;  
  87.     }  
  88.     return -1;  
  89.     }  
  90.   
  91.     /** use serialVersionUID from JDK 1.0.2 for interoperability */  
  92.     private static final long serialVersionUID = 1224463164541339165L;  
  93. }  

通过peek()方法注释The object at the top of this stack (the last item of the Vector object,可以发现数组(Vector)的最后一位即为Stack的栈顶

pop、peek以及search方法本身进行了同步

push方法调用了父类的addElement方法

empty方法调用了父类的size方法

Vector类为线程安全类

综上,Stack类为线程安全类(多个方法调用而产生的数据不一致问题属于原子性问题的范畴)


设计一个有getMin 功能的栈

private Stack <Integer> stackData ;private Stack <Integer> stackMin ;public getMinStack(){    this.stackData = new Stack <Integer> ();    this.stackMin = new Stack <Integer> ();}public void push(int newNum){    if(stackMin.isEmpty()){        stackMin.push(newNum);    }else if(newNum>this.getMin()){        stackMin.push(newNum);    }    stackData.push(newNum);}public int pop(){    if(stackMin.isEmpty()){        throw new RuntimeException("stack is empty");    }    int value= stackData.pop();    if(value==this.getMin()){        stackMin.pop();    }    return value;}public int getMin(){    if(stackMin.isEmpty()){        throw new RuntimeException("stack is empty");    }    return stackMin.peek();}



原创粉丝点击