【用队列实现栈】【用栈实现队列】Leetcode 232 225

【用队列实现栈】【用栈实现队列】Leetcode 232 225

---------------🎈🎈题目链接 用队列实现栈🎈🎈-------------------
---------------🎈🎈题目链接 用栈实现队列🎈🎈-------------------

在这里插入图片描述
在这里插入图片描述

队列的相关操作

创建队列
Queue<Integer> myqueue = new LinkedList<>();
添加元素
myqueue.add();
队列最初的元素
myqueue.peek();
队列弹出元素:
myqueue.poll();
队列大小
myqueue.size();
队列是否为空
myqueue.isEmpty();

栈的相关操作

创建栈
Stack<Integer> mystack = new Stack<>();
添加元素
mystack.push();
栈顶元素
mystack.peek();
弹出栈顶元素:
mystack.pop();
栈大小
mystack.size();
栈是否为空
mystack.isEmpty();

用队列实现栈

class MyStack {
   
    Queue<Integer> myqueue;

    public MyStack() {
   
        myqueue = new LinkedList<>();
    }
    
    public void push(int x) {
   
        myqueue.add(x);
    }
    
    public int pop() {
   
        for(int i = 0; i < myqueue.size()-1; i++){
   
            myqueue.add(myqueue.poll());
        }
        return myqueue.poll();
    }
    
    public int top() {
   
        int top;
        for(int i = 0; i < myqueue.size()-1; i++){
   
            myqueue.add(myqueue.poll());
        }
        top = myqueue.peek();
        myqueue.add(myqueue.poll());
        return top;
    }
    
    public boolean empty() {
   
        if(myqueue.isEmpty()){
   
            return true;
        }
        else 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();
 */

           
    

用栈实现队列

class MyQueue {
   

    Stack<Integer> mystackin;
    Stack<Integer> mystackout;

    public MyQueue() {
   
        mystackin = new Stack<>();
        mystackout = new Stack<>();
    }
    
    public void push(int x) {
   
        while(!mystackout.isEmpty()){
   
            mystackin.push(mystackout.pop());
        }
        mystackin.push(x);
    }
    
    public int pop() {
   
        while(!mystackin.isEmpty()){
   
            mystackout.push(mystackin.pop());
        }
        return mystackout.pop();
    }
    
    public int peek() {
   
        while(!mystackin.isEmpty()){
   
            mystackout.push(mystackin.pop());
        }
        return mystackout.peek();

    }
    
    public boolean empty() {
   
        if(mystackin.isEmpty() && mystackout.isEmpty()){
   
            return true;
        }
        else 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();
 */

相关推荐

  1. leetcode-队列实现

    2024-01-17 07:08:03       63 阅读
  2. leetcode-实现队列

    2024-01-17 07:08:03       53 阅读
  3. 实现队列

    2024-01-17 07:08:03       39 阅读
  4. LeetCode-232. 实现队列 设计 队列

    2024-01-17 07:08:03       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 07:08:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 07:08:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 07:08:03       82 阅读
  4. Python语言-面向对象

    2024-01-17 07:08:03       91 阅读

热门阅读

  1. redis实现延迟任务(四)

    2024-01-17 07:08:03       59 阅读
  2. vue使用el-input监听不了回车事件解决方法

    2024-01-17 07:08:03       54 阅读
  3. vue3和vue2区别

    2024-01-17 07:08:03       44 阅读
  4. 探索 ChatGPT 中文版:开启自然语言处理新纪元

    2024-01-17 07:08:03       62 阅读
  5. leetcode2719. 统计整数数目

    2024-01-17 07:08:03       49 阅读
  6. 编程探秘:Python深渊之旅-----云端部署(六)

    2024-01-17 07:08:03       56 阅读
  7. BigDecimal使用案例

    2024-01-17 07:08:03       51 阅读
  8. 蓝桥杯简介

    2024-01-17 07:08:03       54 阅读
  9. 后端开发笔记20240106

    2024-01-17 07:08:03       55 阅读
  10. Oracle/DM序列基本使用

    2024-01-17 07:08:03       57 阅读