用雙向鏈表和數組實現棧和隊列

1.棧和隊列概念

棧和隊列是一種邏輯上的數據結構:

  • "棧”(Stack)的本意是乾草堆;描述一種數據後入先出 LIFO (LastInput First Out)
  • “隊列”(Queue)就是等待做某事而排成的隊;描述一種先入先出 FIFO(First Input First Out)
    參考:《計算機是怎麼跑起來的》-[日]矢澤久雄 第6.4章節

2.代碼實現

  • 用雙向鏈表
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 雙向鏈表實現棧和隊列
 *
 * @author TaoistQu
 */
public class DoubleEndsQueueToStackAndQueue {
    /**
     * 實現一個雙向鏈表結構,可以從頭部和尾部依次添加元素
     * 也可以從頭部和尾部刪除元素
     *
     * @param <T>
     */
    public static class DoubleEndsQueue<T> {
        public Node<T> head;
        public Node<T> tail;

        /**
         * 從頭部加節點
         *
         * @param value
         */
        public void addFromHead(T value) {
            Node<T> cur = new Node<>(value);
            if (head == null) {
                head = cur;
                tail = cur;
            } else {
                cur.next = head;
                head.last = cur;
                head = cur;
            }
        }

        /**
         * 從尾部加節點
         *
         * @param value
         */
        public void addFromBottom(T value) {
            Node<T> cur = new Node<>(value);
            if (head == null) {
                head = cur;
                tail = cur;
            } else {
                cur.last = tail;
                tail.next = cur;
                tail = cur;
            }
        }

        /**
         * 從頭部彈出元素
         *
         * @return
         */
        public T popFromHead() {
            if (head == null) {
                return null;
            }
            Node<T> cur = head;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.next;
                head.last = null;
                cur.next = null;
            }
            return cur.value;
        }

        public T popFromBottom() {
            if (head == null) {
                return null;
            }
            Node<T> cur = tail;
            if (tail == head) {
                head = null;
                tail = null;
            } else {
                tail = tail.last;
                tail.next = null;
                cur.last = null;
            }
            return cur.value;
        }

        public boolean isEmpty() {
            return head == null;
        }
    }

    /**
     * 用雙向鏈表實現棧
     *
     * @param <T>
     */
    public static class MyStack<T> {
        private DoubleEndsQueue<T> queue;

        public MyStack() {
            queue = new DoubleEndsQueue<>();
        }

        public void push(T value) {
            queue.addFromHead(value);
        }

        public T pop() {
            return queue.popFromHead();
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }

    /**
     * 用雙向鏈表實現隊列
     *
     * @param <T>
     */
    public static class MyQueue<T> {
        private DoubleEndsQueue<T> queue;

        public MyQueue() {
            queue = new DoubleEndsQueue<>();
        }

        public void push(T value) {
            queue.addFromHead(value);
        }

        public T poll() {
            return queue.popFromBottom();
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }

    public static boolean isEqual(Integer o1, Integer o2) {
        if (o1 == null && o2 != null) {
            return false;
        }
        if (o1 != null && o2 == null) {
            return false;
        }
        if (o1 == null && o2 == null) {
            return true;
        }
        return o1.equals(o2);
    }

    public static void main(String[] args) {
        int oneTestDataNum = 100;
        int value = 10000;
        int testTimes = 100000;
        for (int i = 0; i < testTimes; i++) {
            MyStack<Integer> myStack = new MyStack<>();
            MyQueue<Integer> myQueue = new MyQueue<>();
            Stack<Integer> stack = new Stack<>();
            Queue<Integer> queue = new LinkedList<>();

            for (int j = 0; j < oneTestDataNum; j++) {
                int nums = (int) (Math.random() * value);
                if (stack.isEmpty()) {
                    myStack.push(nums);
                    stack.push(nums);
                } else {
                    if (Math.random() < 0.5) {
                        myStack.push(nums);
                        stack.push(nums);
                    } else {
                        if (!isEqual(myStack.pop(), stack.pop())) {
                            System.out.println("oops!");
                        }
                    }
                }
                int numq = (int) (Math.random() * value);
                if (stack.isEmpty()) {
                    myQueue.push(numq);
                    queue.offer(numq);
                } else {
                    if (Math.random() < 0.5) {
                        myQueue.push(numq);
                        queue.offer(numq);
                    } else {
                        if (!isEqual(myQueue.poll(), queue.poll())) {
                            System.out.println("oops!");
                        }
                    }
                }
            }

        }

        System.out.println("finish!");
    }

    /**
     * 雙向鏈表節點類
     *
     * @param <T>
     */
    public static class Node<T> {
        public T value;
        public Node<T> last;
        public Node<T> next;

        public Node(T data) {
            value = data;
        }
    }
}
用JDK的棧和隊列作對數器進行100000次隨機測試結果:

finish!

Process finished with exit code 0

用雙向鏈表實現代碼github地址

  • 用數組方式
/**
 * @author TaoistQu
 */
public class RingArray {
    /**
     * 實現一個固定大小的棧
     */
    public static class MyStack {
        private int[] arr;
        private int topIndex;
        private final int limit;

        public MyStack(int l) {
            arr = new int[l];
            limit = l;
        }

        public void push(int value) {
            if (topIndex == limit) {
                throw new RuntimeException("棧滿了,不能再繼續添加元素!!!");
            }
            arr[topIndex++] = value;
        }

        public int pop() {
            if (topIndex == 0) {
                throw new RuntimeException("棧空了,不能再取元素啦!!!");
            }
            return arr[--topIndex];
        }

        public boolean isEmpty() {
            return topIndex == 0;
        }

    }

    /**
     * 實現一個循環隊列;用size解耦pushi 和 polli隊列的兩個索引
     */
    public static class MyQueue {
        private int[] arr;
        private int pushi;
        private int polli;
        private int size;
        private final int limit;

        public MyQueue(int l) {
            arr = new int[l];
            limit = l;
        }

        public void push(int value) {
            if (size == limit) {
                throw new RuntimeException("隊列滿了,不能再繼續添加元素!!!");
            }
            size++;
            arr[pushi] = value;
            pushi = nextIndex(pushi);
        }

        public int pop() {
            if (size == 0) {
                throw new RuntimeException("隊列空了,不能再取元素啦!!!");
            }
            size--;
            int ans = arr[polli];
            polli = nextIndex(polli);
            return ans;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        /**
         * 循環索引
         *
         * @param i
         * @return
         */
        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1 : 0;
        }
    }


    public static void main(String[] args) {
        //MyQueue myStack = new MyQueue(10);
        MyStack myStack = new MyStack(10);
        for (int i = 1; i <= 10; i++) {
            myStack.push(i);
        }
        for (int i = 1; i <= 5; i++) {
            System.out.print(myStack.pop() + " ");
        }
        System.out.println();
        for (int i = 11; i <= 14; i++) {
            myStack.push(i);
        }
        while (!myStack.isEmpty()) {
            System.out.print(myStack.pop() + " ");
        }
        System.out.println();
    }
}
測試棧的結果:
10 9 8 7 6 
14 13 12 11 5 4 3 2 1 

測試隊列的結果:
1 2 3 4 5 
6 7 8 9 10 11 12 13 14 

用數組實現代碼github地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章