【LeetCode 经典题解】:队列与栈的双向模拟——从原理到代码详解

🎁个人主页:User_芊芊君子

🎉欢迎大家点赞👍评论📝收藏⭐文章

🔍系列专栏:Java.数据结构


【前言】

数据结构中,栈(后进先出)和队列(先进先出)特性迥异。本系列聚焦LeetCode经典题:队列实现栈与栈实现队列,剖析如何突破结构限制,实现特性互模拟,助你深入理解两者本质。

文章目录:

一、队列实现栈

LeetCode:队列实现栈


1.思路分析

两个队列实现栈,使用LinkedList来模拟队列,因为LinkedList实现了Queue接口,支持队列的标准

  • 入队:统一入到不为空的队列中;
  • 出队:将size-1个元素放到另一个队列,出剩下的那一个

2.代码详解

创建q1,q2两个队列,并实例化,因为LinkedList实现了Queue接口

java 复制代码
    public Queue<Integer> q1;
    public Queue<Integer> q2;
        public MyStack() {
            q1 = new LinkedList<>();
            q2 = new LinkedList<>();
        }

当两个队列都为空时,则栈空

java 复制代码
 public boolean empty() {
            return q1.isEmpty()&& q2.isEmpty();
        }

2.1 push(int x)

判断q1,q2是否为空,直接添加元素到不为空的那个队列,如果都为空,就添加到q1

java 复制代码
public void push(int x) {
            if(!q1.isEmpty()) {
                q1.offer(x);
            }else if(!q2.isEmpty()) {
                q2.offer(x);
            }else{
                q1.offer(x);
            }
        }

2.2 pop()

如果队列都为空,就返回-1。然后分开判断,q1不为空时,将它的size-1个元素转移到q2,然后出剩下的那个元素,q2不为空时,将它的size-1个元素转移到q1,然后出剩下的那个元素

java 复制代码
public int pop() {
            if (empty()) {
                return -1;
            }
            if (!q1.isEmpty()){
                int size = q1.size();
                while (size - 1 != 0) {
                    int val = q1.poll();
                    q2.offer(val);
                    size--;
                }
                return q1.poll();
            } else{
                int size = q2.size();
                while (size - 1 != 0) {
                    int val = q2.poll();
                    q1.offer(val);
                    size--;
                }
                return q2.poll();
            }
        }

2.3 top()

跟pop()一样,需要改变的就是转移元素的个数为·size·,这样转移完成时,载体val中保留的就是最后要获取的那个元素,直接返回就行

java 复制代码
public int top() {
            int val = 1;
            if (empty()) {
                return -1;
            }
            if (!q1.isEmpty()){
                int size = q1.size();
                while (size != 0) {
                    val = q1.poll();
                    q2.offer(val);
                    size--;
                }
                return val;
            } else{
                int size = q2.size();
                while (size != 0) {
                    val = q2.poll();
                    q1.offer(val);
                    size--;
                }
                return val;
            }
        }

二、栈实现队列

LeetCode:栈实现队列


1.思路分析

用两个双端队列Deque当作栈,LinkedList实现了Deque接口(也可以用LinkedList当作栈)

  • 入栈:所有元素先放到第一个栈中;
  • 出栈:
    1.如果第二个栈为空,就将s1栈中元素放到s2中,这样就会倒过来;
    2.如果不为空,直接出s2栈顶元素
  • 两个队列都为空,则模拟的栈是空的

2.代码分析

先创建两个栈,然后实例化。LinkedList实现了Deque接口(也可以用LinkedList当作栈)

java 复制代码
    public Deque<Integer> s1;
    public Deque<Integer> s2;

    public MyQueue() {
        s1 = new LinkedList<>();
        s2 = new LinkedList<>();
    }

当两个栈都为空时,则队列空

java 复制代码
 public boolean empty() {
        return s1.isEmpty()&& s2.isEmpty();
    }

2.1 push(int x)

直接放到s1中

java 复制代码
public void push(int x) {
       s1.push(x);
    }

2.2 pop()

先判断栈是否为空,然后当s2为空时,就可以将s1中所有元素,转移过来,然后出s2栈顶元素

java 复制代码
public int pop() {
        if(empty()) {
            return -1;
        }
        if(s2.isEmpty()) {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }

2.3 peek()

与pop() 一样,转移过来后,获取s2栈顶元素

java 复制代码
public int peek() {
        if(empty()) {
            return -1;
        }
        if(s2.isEmpty()) {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }

四、完整代码

1.队列实现栈

java 复制代码
public class MyStack {

    public Queue<Integer> q1;
    public Queue<Integer> q2;
        public MyStack() {
            q1 = new LinkedList<>();
            q2 = new LinkedList<>();
        }

        public void push(int x) {
            if(!q1.isEmpty()) {
                q1.offer(x);
            }else if(!q2.isEmpty()) {
                q2.offer(x);
            }else{
                q1.offer(x);
            }
        }

        public int pop() {
            if (empty()) {
                return -1;
            }
            if (!q1.isEmpty()){
                int size = q1.size();
                while (size - 1 != 0) {
                    int val = q1.poll();
                    q2.offer(val);
                    size--;
                }
                return q1.poll();
            } else{
                int size = q2.size();
                while (size - 1 != 0) {
                    int val = q2.poll();
                    q1.offer(val);
                    size--;
                }
                return q2.poll();
            }
        }

        public int top() {
            int val = 1;
            if (empty()) {
                return -1;
            }
            if (!q1.isEmpty()){
                int size = q1.size();
                while (size != 0) {
                    val = q1.poll();
                    q2.offer(val);
                    size--;
                }
                return val;
            } else{
                int size = q2.size();
                while (size != 0) {
                    val = q2.poll();
                    q1.offer(val);
                    size--;
                }
                return val;
            }
        }

        public boolean empty() {
            return q1.isEmpty()&& q2.isEmpty();
        }
    }

2.栈实现队列

java 复制代码
public class MyQueue {

    public Deque<Integer> s1;
    public Deque<Integer> s2;

    public MyQueue() {
        s1 = new LinkedList<>();
        s2 = new LinkedList<>();
    }

    public void push(int x) {
       s1.push(x);
    }

    public int pop() {
        if(empty()) {
            return -1;
        }
        if(s2.isEmpty()) {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }

    public int peek() {
        if(empty()) {
            return -1;
        }
        if(s2.isEmpty()) {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }

    public boolean empty() {
        return s1.isEmpty()&& s2.isEmpty();
    }
}

三、总结

  • "队列实现栈"与"栈实现队列"均通过双结构协作+元素转移实现特性突破:前者借两个队列的转移让"先进先出"模拟"后进先出";后者靠两个栈的分工让"后进先出"模拟"先进先出"。
  • 核心收获是理解结构特性的灵活转化------看似对立的栈和队列,可通过逻辑设计实现功能互通。掌握这种思维,能为解决复杂算法问题拓宽思路,助力你在数据结构领域的学习与实践。
相关推荐
sheeta199813 小时前
LeetCode 每日一题笔记 日期:2025.11.24 题目:1018. 可被5整除的二进制前缀
笔记·算法·leetcode
是小胡嘛16 小时前
C++之Any类的模拟实现
linux·开发语言·c++
口袋物联16 小时前
设计模式之工厂模式在 C 语言中的应用(含 Linux 内核实例)
linux·c语言·设计模式·简单工厂模式
qq_4798754317 小时前
X-Macros(1)
linux·服务器·windows
笨笨聊运维18 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
m***567219 小时前
Win10下安装 Redis
数据库·redis·缓存
HIT_Weston19 小时前
39、【Ubuntu】【远程开发】拉出内网 Web 服务:构建静态网页(二)
linux·前端·ubuntu
福尔摩斯张20 小时前
《C 语言指针从入门到精通:全面笔记 + 实战习题深度解析》(超详细)
linux·运维·服务器·c语言·开发语言·c++·算法
橘颂TA20 小时前
【剑斩OFFER】算法的暴力美学——两整数之和
算法·leetcode·职场和发展
TH_121 小时前
腾讯云-(3)-Linux宝塔面板使用
linux·云计算·腾讯云