代码随想录算法训练营第十天| 理论基础 ,232.用栈实现队列 ,225. 用队列实现栈

本文介绍了如何用Java中的栈数据结构实现队列功能,以及如何反过来使用队列实现栈。通过MyQueue和MyStack类的示例,展示了在编程中如何转换基本数据结构的操作。
摘要由CSDN通过智能技术生成

代码随想录算法训练营

今日任务
理论基础 ,232.用栈实现队列 ,225. 用队列实现栈



理论基础

https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

232.用栈实现队列

class MyQueue {

    //管理进栈
    Stack<Integer> stackIn;
    //管理出栈
    Stack<Integer> stackOut;

    public MyQueue() {
        stackIn=new Stack<>();
        stackOut=new Stack<>();
    }

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

    public int pop() {
        while(!stackIn.isEmpty()){
            Integer num = stackIn.pop();
            stackOut.push(num);
        }
        Integer pop = stackOut.pop();
        while(!stackOut.isEmpty()){
            Integer num = stackOut.pop();
            stackIn.push(num);
        }
        return pop;
    }

    public int peek() {
        while(!stackIn.isEmpty()){
            Integer num = stackIn.pop();
            stackOut.push(num);
        }
        Integer peek = stackOut.peek();
        while(!stackOut.isEmpty()){
            Integer num = stackOut.pop();
            stackIn.push(num);
        }
        return peek;
    }

    public boolean empty() {
        if(stackIn.isEmpty()&&stackOut.isEmpty()){
            return true;
        }
        return false;
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

225. 用队列实现栈

class MyStack {

    Queue<Integer> queueIn;
    Queue<Integer> queueOut;

    public MyStack() {
        queueIn=new LinkedList<>();
        queueOut=new LinkedList<>();
    }
    //入队offer
    //出队poll
    public void push(int x) {
        queueIn.offer(x);
    }

    public int pop() {
        //出栈
        while(queueIn.size()>1){
            Integer num = queueIn.poll();
            queueOut.offer(num);
        }
        Integer poll = queueIn.poll();
        while(!queueOut.isEmpty()){
            Integer num = queueOut.poll();
            queueIn.offer(num);
        }
        return poll;
    }

    public int top() {
        while(queueIn.size()>1){
            Integer num = queueIn.poll();
            queueOut.offer(num);
        }
        Integer peek = queueIn.peek();
        queueIn.poll();
        queueOut.offer(peek);
        while(!queueOut.isEmpty()){
            Integer num = queueOut.poll();
            queueIn.offer(num);
        }
        return peek;
    }

    public boolean empty() {
        if(queueIn.isEmpty()&&queueOut.isEmpty()){
            return true;
        }
        return false;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值