Stack學習筆記

Stack 簡介

Stack 是一種後進先出的數據結構實現,也就是LIFO(last in first out). 舉個例子吧,大學食堂的大媽會攤煎餅。攤好了就放到桌子上,後面攤好的會疊加到之前一個上。如果有個人來買,大媽通常會把最上面的煎餅給到到你。當然,一般情況她不會從最底下抽一個出來給你。那樣容易把餅子扯爛了,而且從底下拿餅子,就是大媽在演示FIFO(first in firt out)啦。開個玩笑,一般是大家在旁邊等着大媽攤餅子。煎餅果子切克鬧,有錢沒錢來一套。走起。

Stack class

Stack 很單純,只有一個父類 Vector,它通過五個操作擴展了Vector類,這些操作允許將 Vector 視爲 Stack。提供了通常的 推入 push() 和 彈出 pop() 操作,以及一種 查看堆棧頂部項目的方法peek()

public class Stack<E> extends Vector<E> {}

當stack倍創建後,他是不包含任何對象的。Deque接口及其實現提供了一組更完整和一致的LIFO堆棧操作,應優先於此類使用。
Deque stack = new ArrayDeque();

代碼不多,就全部貼上來:

public
class Stack<E> extends Vector<E> {
    /**
     * Creates an empty Stack.
     */
    public Stack() {
    }

    /**
     * Pushes an item onto the top of this stack. This has exactly
     * the same effect as:
     * <blockquote><pre>
     * addElement(item)</pre></blockquote>
     *
     * @param   item   the item to be pushed onto this stack.
     * @return  the <code>item</code> argument.
     * @see     java.util.Vector#addElement
     */
    public E push(E item) {
        addElement(item);

        return item;
    }

    /**
     * Removes the object at the top of this stack and returns that
     * object as the value of this function.
     *
     * @return  The object at the top of this stack (the last item
     *          of the <tt>Vector</tt> object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E pop() {
        E       obj;
        int     len = size();

        obj = peek();
        removeElementAt(len - 1);

        return obj;
    }

    /**
     * Looks at the object at the top of this stack without removing it
     * from the stack.
     *
     * @return  the object at the top of this stack (the last item
     *          of the <tt>Vector</tt> object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

    /**
     * Tests if this stack is empty.
     *
     * @return  <code>true</code> if and only if this stack contains
     *          no items; <code>false</code> otherwise.
     */
    public boolean empty() {
        return size() == 0;
    }

    /**
     * Returns the 1-based position where an object is on this stack.
     * If the object <tt>o</tt> occurs as an item in this stack, this
     * method returns the distance from the top of the stack of the
     * occurrence nearest the top of the stack; the topmost item on the
     * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
     * method is used to compare <tt>o</tt> to the
     * items in this stack.
     *
     * @param   o   the desired object.
     * @return  the 1-based position from the top of the stack where
     *          the object is located; the return value <code>-1</code>
     *          indicates that the object is not on the stack.
     */
    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = 1224463164541339165L;
}

可以看到,Stack 也是做了同步操作的。

發佈了76 篇原創文章 · 獲贊 9 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章