225. Implement Stack using Queues

import java.util.LinkedList;
import java.util.Queue;

class MyStack {

    Queue<Integer> q1=new LinkedList<Integer>();
    Queue<Integer> q2=new LinkedList<Integer>();

    // Push element x onto stack.
    public void push(int x) {
        if(q1.isEmpty()&&q2.isEmpty())
        {
            q1.add(x);
        }
        else
        {
            if(!q1.isEmpty())
                q1.add(x);
            else
                q2.add(x);
        }

    }

    // Removes the element on top of the stack.
    public void pop() {

        if(!q1.isEmpty())
        {
            while(q1.size()!=1)
            {
                q2.add(q1.poll());
            }
            q1.poll();
        }
        else
        {
            while(q2.size()!=1)
            {
                q1.add(q2.poll());
            }
            q2.poll();
        }

    }

    // Get the top element.
    public int top() {
        int res=0;
        if(!q1.isEmpty())
        {
            while(!q1.isEmpty())
            {
                res=q1.poll();
                q2.add(res);
            }
        }
        else
        {
            while(!q2.isEmpty())
            {
                res=q2.poll();
                q1.add(res);
            }
        }
        return res;

    }

    // Return whether the stack is empty.
    public boolean empty() {
        if(q1.isEmpty()&&q2.isEmpty())
            return true;
        else
            return false;
    }
}
时间: 2024-10-11 03:42:54

225. Implement Stack using Queues的相关文章

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

232. Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whet

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

[leetcode] 225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

[LeetCode] 225. Implement Stack using Queues Java

题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use on

【LeetCode】225 - Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

Java for LeetCode 225 Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

(easy)LeetCode 225.Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

[leedcode 225] Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

[LC] 225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Example: MyStack stack = n