用队列实现栈,用栈实现队列,听起来有点绕,都搞懂了就掌握了精髓!
来源:     阅读:411
依创模板店
发布于 2020-11-08 03:17
查看主页

一、背景

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

二、概念

2.1 栈

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

2.2 队列

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

三、栈和队列的相互实现

3.1 用队列实现栈
/** * 用队列模拟实现栈 * * @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 用栈实现队列
/** * 用栈模拟实现队列 * * @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());    }}

四、总结

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

免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境 windows
相关推荐
高效使用MAC工作流
Android 中的 theme 和 style(1)
2019年如何成为一个年薪 40 万以上的Java程序员?
Linux下安装MySQL步骤图解
【科普】继电器和电子管
首页
搜索
订单
购物车
我的