使用两个队列实现栈

在计算机科学中,栈是一种数据结构,它遵循后进先出(LIFO)的原则。这意味着最后一个被添加到栈的元素将是第一个被移除的元素。然而,Java的标准库并没有提供栈的实现,但我们可以使用两个队列来模拟一个栈的行为。

首先,我们需要创建一个名为MyStack的类,该类包含两个栈:queue1queue2。这两个栈将用于实现队列的功能。接下来,我们需要实现队列的基本操作,包括pushpoppeekempty

首先,我们需要创建一个栈类

java 复制代码
public class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack(){
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
}

push方法

push(int value): 将一个元素添加到栈中。首先,我们将该元素添加到queue2中。然后,我们将queue1中的所有元素移动到queue2中,直到queue1为空。最后,我们交换queue1queue2的角色,使得queue1始终是栈顶元素所在的队列。

java 复制代码
public void push(int value){
        queue2.offer(value);
        while (!queue1.isEmpty()){
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

pop方法

pop(): 从栈中移除并返回栈顶元素。由于栈顶元素位于queue1中,我们只需调用queue1.poll()即可。

java 复制代码
public int pop(){
        return queue1.poll();
    }

top()方法

top(): 返回栈顶元素但不将其从栈中移除。由于栈顶元素位于queue1中,我们只需调用queue1.peek()即可。

java 复制代码
public int top(){
        return queue1.peek();
    }

isEmpty方法

isEmpty(): 检查栈是否为空。我们只需检查queue1是否为空即可。

java 复制代码
public boolean isEmpty(){
        return queue1.isEmpty();
    }

完整代码

java 复制代码
public class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack(){
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int value){
        queue2.offer(value);
        while (!queue1.isEmpty()){
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    public int pop(){
        return queue1.poll();
    }

    public int top(){
        return queue1.peek();
    }

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

}

测试类

java 复制代码
public class Test {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        System.out.println(myStack.isEmpty());  // true
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(myStack.pop()); // 3
        System.out.println(myStack.pop()); // 2
        System.out.println(myStack.isEmpty()); // false
        System.out.println(myStack.pop()); // 1
        System.out.println(myStack.isEmpty()); // true
    }
}

运行结果

相关推荐
froginwe118 分钟前
Pandas DataFrame:深入理解数据分析的利器
开发语言
Miraitowa_cheems9 分钟前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先
Jm_洋洋11 分钟前
【Linux系统编程】程序替换:execve(execl、execlp、execle、execv、execvp、execvpe)
linux·运维·c语言·开发语言·程序人生
CodeCraft Studio15 分钟前
国产化Word处理控件Spire.Doc教程:用Java实现TXT文本与Word互转的完整教程
java·c#·word·spire.doc·word文档转换·txt转word·word转txt
徐子童21 分钟前
数据结构---优先级队列(堆)
java·数据结构·面试题·优先级队列··topk问题
滑水滑成滑头26 分钟前
**标题:发散创新:智能交通系统的深度探究与实现**摘要:本文将详细
java·人工智能·python
学编程就要猛30 分钟前
数据结构初阶:Java泛型
数据结构·1024程序员节
冯诺依曼的锦鲤33 分钟前
算法练习:前缀和专题
开发语言·c++·算法
siriuuus40 分钟前
Maven 核心概念及生命周期
java·maven
闭着眼睛学算法1 小时前
【双机位A卷】华为OD笔试之【哈希表】双机位A-跳房子I【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·c++·python·算法·华为od·散列表