用队列实现栈,用栈实现队列,听起来有点绕,都搞懂了就掌握了精髓

2022-10-29  本文已影响0人  欧子有话说_

一、背景

栈和队列是数据结构中最常用到的两种结构,有非常广泛的运用,该篇文章将通过动画的手段,展示栈和队列相互实现的底层原理,让我们真正搞懂栈和队列的特性。

二、概念

2.1 栈

栈[Stack]:是一种限定仅在表尾进行插入和删除操作的线性表;即后进先出(LIFO-last in first out),最后插入的元素最先出来

image.png image.png

2.2 队列

队列[Queue]:是一种限定仅在表头进行删除操作,仅在表尾进行插入操作的线性表;即先进先出(FIFO-first in first out):最先插入的元素最先出来。

image.png image.png

三、栈和队列的相互实现

3.1 用队列实现栈

image.png
/**
 * 用队列模拟实现栈
 *
 * @author zhuhuix
 * @date 2020-06-09
 */
public class QueueImplStack {

    // 定义队列
    private Queue<Integer> queue;

    public QueueImplStack() {
        queue = new LinkedList();
    }

    // 入栈--在队尾加入元素后,让其他元素按顺序出队再入队,保持新加入的元素永远在队头
    public void push(Integer e) {
        queue.offer(e);
        int size = queue.size();
        int i = 0;
        while (i < size - 1) {
            queue.offer(queue.poll());
            i++;
        }
    }

    // 出栈--将队尾前的其它所有元素出队再入队,直至队尾元素移到队头
    public Integer pop() {
        return queue.poll();
    }

    // 查看栈顶元素--即队头元素
    public Integer peek() {
        return queue.peek();
    }

    // 是否为空
    public boolean isEmpty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        QueueImplStack stack = new QueueImplStack();
        stack.push(1);
        System.out.println(stack.peek());
        stack.push(2);
        System.out.println(stack.peek());
        stack.push(3);
        System.out.println(stack.peek());
        System.out.println("=============");
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());

    }
}

3.2 用栈实现队列

image.png
/**
 * 用栈模拟实现队列
 *
 * @author zhuhuix
 * @date 2020-06-09
 */
public class StackImplQueue {
    // 数据栈
    private Stack<Integer> stack;
    // 辅助栈
    private Stack<Integer> aux;

    StackImplQueue() {
        stack = new Stack<>();
        aux = new Stack<>();
    }

    // 入队--通过数据栈与辅助栈相互交换,保证新加入的元素沉在数据栈底
    public void enqueue(Integer e) {
        while (!stack.isEmpty()) {
            aux.push(stack.pop());
        }
        stack.push(e);
        while(!aux.isEmpty()){
            stack.push(aux.pop());
        }

    }

    // 出队--弹出数据栈元素
    public Integer dequeue(){
        return stack.pop();
    }

    // 查看队头元素
    public Integer peek(){
        return stack.peek();
    }

    // 是否为空
    public boolean isEmpty(){
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        StackImplQueue queue = new StackImplQueue();
        queue.enqueue(1);
        System.out.println(queue.peek());
        queue.enqueue(2);
        System.out.println(queue.peek());
        queue.enqueue(3);
        System.out.println(queue.peek());
        System.out.println("=============");
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());

    }
}

四、总结

通过以上栈和队列相互交叉的实践,我们对栈和队列的重大特性有了深入了解:

上一篇 下一篇

猜你喜欢

热点阅读