數據結構和算法(三)棧

棧定義
堆棧(英語:stack)又稱爲棧,是計算機科學中一種特殊的串列形式的抽象資料型別,其特殊之處在於只能允許在鏈接串列或陣列的一端(稱爲堆疊頂端指標,英語:top)進行加入數據(英語:push)和輸出數據(英語:pop)的運算。另外棧也可以用一維數組或連結串列的形式來完成。
這裏寫圖片描述

棧的基本特點

  • 先入後出,後入先出。
  • 除頭尾節點之外,每個元素有一個前驅,一個後繼。

這裏寫圖片描述

用順序表實現棧

@SuppressWarnings("all")
public class ArrayStack<T> {

    private static final int MINIMUM_SIZE = 1024;
    private T[] array = (T[]) new Object[MINIMUM_SIZE];
    private int size = 0;
    //入棧
    public boolean push(T value) {
        if(size >= array.length)
            array = Arrays.copyOf(array, size + (size <<1));
        array[size++] = value;
        return true;
    }
    //出棧(移除元素)
    public T pop() {
        if(size <=0) return null;
        T t = array[--size]; 
        array[size] = null;
        return t;
    }
    //出棧(元素不移除)
    public T peek() {
        if(size <=0) return null;
        T t = array[--size];
        return t;
    }
    //獲取元素
    public T get(int index) {
        if(index>=0 && index<size) 
            return array[index];
            return null;
    }
    public boolean remove(T value) {
        for(int i=0;i<size;i++) {
            T obj = array[i];
            if(obj.equals(value)) {
                return (remove(i));
            }
        }
        return false;
    }
    private boolean remove(int index) {
        if(index != --size) {
            System.arraycopy(array, index+1, array, index, size-index);
        }
        array[size] = null;
        return true;
    }
}

用鏈表實現棧

public class LinkedStack<T> {
    //定義結點類
    private static class Node<T>{
        private T value = null;  //結點裏存放的值
        private Node<T> above = null; //結點頭指針
        private Node<T> below = null; //結點尾指針
        private Node(T value) {
            this.value = value;
        }
    }
    private Node<T> top = null; //頭結點
    private int size = 0; //結點個數
    public LinkedStack() {
        top = null;
        size = 0;
    }
    /**
     * 入棧
     * @param value
     * @return
     */
    public boolean push(T value) {
        return push(new Node<T>(value));
    }
    private boolean push(Node<T> node) {
        if(top == null) {
            top = node;
        } else {
            Node<T> oldTop = top;
            top = node;
            top.below = oldTop;
            oldTop.above = top;
        }
        size++;
        return true;
    }
    /**
     * 出棧(將彈出的元素刪除)
     * @return
     */
    public T pop() {
        if(top == null) return null;
        Node<T> t = top;
        top = t.below;
        if(top != null) top.above = null;
        T value = t.value;
        size--;
        return value;
    }
    /**
     * 出棧(不刪除元素)
     * @return
     */
    public T peek() {
        return (top != null) ? top.value : null;
    }
    /**
     * 查找元素
     * @param index
     * @return
     */
    public T get(int index) {
        Node<T> current = top;
        for(int i=0;i<index;i++) {
            if(current == null) break;
            current = current.below;
        }
        return (current != null) ? current.value : null;
    }
    /**
     * 刪除元素
     * @param value
     * @return
     */
    public boolean remove(T value) {
        Node<T> node = top;
        while(node != null && (!node.value.equals(value))) {
            node = node.below;
        }
        if(node == null) return false;
        return remove(node);
    }
    public boolean remove(Node<T> node) {
        Node<T> above = node.above;
        Node<T> below = node.below;
        if(above != null && below != null) {
            above.below = below;
            below.above = above;
        } else if(above != null && below ==null) {
            above.below = null;
        } else if(above == null && below != null) {
            below.above = null;
            top = below;
        } else {
            top = null;
        }
        size--;
        return true;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章