用队列实现栈 (Implement Stack using Queues)

 

思路:

// @Title: 用队列实现栈 (Implement Stack using Queues)
// @Author: qisiii
// @Date: 2022-02-28 18:29:10
// @Runtime: 0 ms
// @Memory: 38.9 MB
// @comment: 
// @flag: 
class MyStack {
    private Queue<Integer> push=new LinkedList(); 
    private Queue<Integer> pop=new LinkedList(); 

    public MyStack() {

    }
    
    public void push(int x) {
        pop.offer(Integer.valueOf(x));
        while(!push.isEmpty()){
          Integer  v=  push.poll();
          pop.offer(v);
        }
       Queue<Integer> temp=push;
       push=pop;
       pop=temp;
    }
    
    public int pop() {
        return push.poll();
    }
    
    public int top() {
        return push.peek();
    }
    
    public boolean empty() {
        return push.isEmpty();
    }
}

/**
 * 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();
 */

+++ title = “用队列实现栈 (Implement Stack using Queues)” draft = false +++

思路:

// @Title: 用队列实现栈 (Implement Stack using Queues)
// @Author: qisiii
// @Date: 2024-09-14 11:48:07
// @Runtime: 0 ms
// @Memory: 40.4 MB
// @comment: 
// @flag: 
class MyStack {
    private Queue<Integer> popQueue=new LinkedList<>();
    private Queue<Integer> tempQueue=new LinkedList<>();
    public MyStack() {

    }

    public void push(int x) {
        tempQueue.offer(x);
        while (!popQueue.isEmpty()){
            tempQueue.offer(popQueue.poll());
        }
        while (!tempQueue.isEmpty()){
            popQueue.offer(tempQueue.poll());
        }
    }

    public int pop() {
       return popQueue.poll();
    }

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

    public boolean empty() {
        return popQueue.isEmpty();
    }
}

/**
 * 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();
 */

思路:

// @Title: 用队列实现栈 (Implement Stack using Queues)
// @Author: qisiii
// @Date: 2024-08-13 15:35:27
// @Runtime: 0 ms
// @Memory: 40.3 MB
// @comment: 
// @flag: 
class MyStack {

    Queue<Integer> push=new LinkedList<>();
    Queue<Integer> pop=new LinkedList();
    public MyStack() {

    }

    public void push(int x) {
       push.add(x);
       while (!pop.isEmpty()){
           push.add(pop.poll());
       }
       Queue temp=new ArrayDeque();
       temp=pop;
       pop=push;
       push=temp;
    }

    public int pop() {
        return pop.poll();
    }

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

    public boolean empty() {
        return pop.isEmpty();
    }
}

思路:将数据保持成栈的顺序稳定在一个队列里

// @Title: 用队列实现栈 (Implement Stack using Queues)
// @Author: qisiii
// @Date: 2024-01-02 21:44:53
// @Runtime: 0 ms
// @Memory: 40.2 MB
// @comment: 将数据保持成栈的顺序稳定在一个队列里
// @flag: ORANGE
class MyStack {

    Queue<Integer> push=new ArrayDeque();
    Queue<Integer> pop=new ArrayDeque();
    public MyStack() {

    }

    public void push(int x) {
       pop.add(x);
       while (!push.isEmpty()){
           pop.add(push.poll());
       }
       Queue temp=new ArrayDeque();
       temp=pop;
       pop=push;
       push=temp;
    }

    public int pop() {
        return push.poll();
    }

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

    public boolean empty() {
        return push.isEmpty();
    }
}

/**
 * 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();
 */
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18