LeetCode 225 Implement Stack using Queues (Stack implementation using Queue )(*)

Source: Internet
Author: User

LeetCode 225 Implement Stack using Queues (Stack implementation using Queue )(*)
Translation

Use queues to implement the following stack operations. Push (x) -- add element x to stack pop () -- remove element top () from Stack top -- return stack top element empty () -- return whether stack is empty note: you must use a queue with only standard operations. That is to say, only push/pop/size/empty and other operations are effective. The queue may not be supported by native, depending on your language. As long as you only use standard queue operations, you can use list or deque (double-ended queue) to simulate the queue. You can assume that all operations are valid (for example, pop or peek operations are not used on an empty stack ).
Original
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 standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Analysis

If you are not clear about stacks and queues, Let's first look at this article: [algorithm] 7 unclear about stacks and queues? A picture gives you a complete understanding

As for this question, there is a very, very similar question. This topic uses queues to implement stacks. the following topic uses stacks to implement queues, because the principles are the same as described in the previous article. Let's take a look: leetCode 232 Implement Queue using Stacks (Queue using Stack )(*)

Code
class Stack {public:    queue
  
    q, q2;    // Push element x onto stack.    void push(int x) {        q.push(x);    }    // Removes the element on top of the stack.    void pop() {        if (q.size() == 1) q.pop();        else {            while (q.size() > 1) {                q2.push(q.front());                q.pop();            }            q.pop();            while (q2.size() > 0) {                q.push(q2.front());                q2.pop();            }        }    }    // Get the top element.    int top() {        if (q.size() == 1) return q.front();        while (q.size() > 1) {            q2.push(q.front());            q.pop();        }        int temp = q.front();        q2.push(q.front());        q.pop();        while (q2.size() > 0) {            q.push(q2.front());            q2.pop();        }        return temp;    }    // Return whether the stack is empty.    bool empty() {        return q.empty();    }};
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.