首页 > 其他分享 >day10 打卡232. 用栈实现队列 225. 用队列实现栈

day10 打卡232. 用栈实现队列 225. 用队列实现栈

时间:2023-03-10 20:56:46浏览次数:60  
标签:return queue1 队列 queue int 用栈 打卡 public

day10 打卡232. 用栈实现队列 225. 用队列实现栈

232. 用栈实现队列

232题目链接

class MyQueue {
    // 管理进的元素
    Stack<Integer> stackIn;
    // 管理出的元素
    Stack<Integer> stackOut;

    public MyQueue() {
        this.stackIn = new Stack();
        this.stackOut = new Stack();
    }
    
    public void push(int x) {
        stackIn.push(x);
    }
    
    public int pop() {
        dumpstackIn();
        return stackOut.pop();
    }
    
    public int peek() {
        dumpstackIn();
        return stackOut.peek();
    }
    
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }

    // 将进来的栈全部放到出去的栈
    private void dumpstackIn() {
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
        }
    }
}

225. 用队列实现栈

225题目链接

1.使用两个队列

class MyStack {
    // 跟栈保持一样的元素
    Queue<Integer> queue1;
    // 辅助队列,每次都是空的
    Queue<Integer> queue2;

    public MyStack() {
        this.queue1 = new LinkedList<>();
        this.queue2 = new LinkedList<>();
    }
    
    public void push(int x) {
        queue2.offer(x);
        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 empty() {
        return queue1.isEmpty();
    }
}

2.使用一个队列

class MyStack {
    Queue<Integer> queue;

    public MyStack() {
        this.queue = new LinkedList<>();
    }
    
    public void push(int x) {
        // 每次加进来元素都需要重新排序
        queue.offer(x);
        int size = queue.size();
        while (size-1>0) {
            Integer num = queue.poll();
            queue.offer(num);
            size--;
        }
    }
    
    public int pop() {
        return queue.poll();
    }
    
    public int top() {,
        return queue.peek();
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }
}

参考资料

代码随想录

标签:return,queue1,队列,queue,int,用栈,打卡,public
From: https://www.cnblogs.com/zzzsl/p/17204626.html

相关文章

  • 2023-03-10 Java中使用ArrayDeque实现栈和队列
    栈和队列的实现实际上完全可以用JDK自带的类ArrayDeque来实现作为队列使用publicabstractbooleanadd(EparamE);//加入元素到队尾publicabstractbooleanoffe......
  • TZOJ 3196: 看病要排队 优先队列
    描述看病要排队这个是地球人都知道的常识。不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻......
  • 微服务学习计划——消息队列
    微服务学习计划——消息队列我们在微服务中一个命令会逐渐调用各个微服务,但如果一一调用不仅需要微服务实时同步交互还会浪费效率所以我们通常会采用MQ,也就是消息队列Mes......
  • 第127篇:异步函数(async和await)练习题(异步,消息队列)
    好家伙,本篇为做题思考书接上文 题目如下: 1.请给出下列代码的输出结果,并配合"消息队列"写出相关解释asyncfunctionfoo(){console.log(2);console.lo......
  • 微服务学习计划——消息队列
    微服务学习计划——消息队列我们在微服务中一个命令会逐渐调用各个微服务,但如果一一调用不仅需要微服务实时同步交互还会浪费效率所以我们通常会采用MQ,也就是消息队列Mes......
  • 每日打卡
    练习python编程:代码:a,b=input().split(',')a1,b1=a.strip(),int(b.strip())print(a1*b1)结果:   代码:a,b=input().split(',')b=int(b)print(int......
  • 模拟队列
        重点:队列是有队头指针hh和队尾指针tt,判断空的条件是如果hh>=tt,队列就为空。栈只能从一个入口出入,栈底永远在0位置(这个位置不存元素)。但是队列是从队尾tail......
  • ORACLE SEQUENCE(队列)用法2
    PS:当需要产生唯一ID的时候,MSSQL只需将ID设置为int类型,并且设置为自增长就能达到.oracle中需要用序列+触发器才可以达到MSSQL中的只增长效果.(俩者还是有区别的.oracl......
  • 华为OD机试 特异性双端队列
    特异性双端队列......
  • 第三章 C语言:栈和队列
    一、栈特性:先进后出,顺序存储#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<stdbool.h>#defineCAPACITY3//默认初识容量typedefintSD......