Leetcode 225 Implement Stack using Queues (using queues to implement stacks) (*)

Source: Internet
Author: User
translation
Use the queue to implement the following operation of the stack.

push (x)--add element x to Stack
pop ()--remove element top () from top of stack ()--
return stack top element empty
()--return stack is empty

Note:

You must use a queue that has only standard operations. In

other words, only push/pop/size/empty and other operations are valid. The

queue may not be native supported, depending on the language you are using.

You can use the list or deque (double-ended queue) to simulate the queue as long as you are only using the queue standard operation.

you can assume that all operations are valid (for example, pop or peek operations are not used on empty stacks).
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 with only standard operations of a queue 

– which means only push to back, peek/pop from front, siz E, and is the empty operations is valid.


Depending on your language, queue May is not supported natively. 

You could simulate a queue by using a list or deque (double-ended queue), as long as if you have standard 

operations of A queue.


You may assume this all operations is valid (for example, 

no pop or top operations'll be called on an empty stack) .
Analysis

If the stack and the queue is not clear, you can first read the text: "Algorithm" 7 is not clear stack and queue. A picture to give you full experience

As for this topic, there is a very very similar topic. The topic is to use the queue to implement the stack, the following is a stack to implement the queue, because in the previous article explained, the principle is the same, we can see for themselves: Leetcode 232 Implement queue using Stacks (with a stack to implement queues) (*) Code

Class Stack {public:queue<int> 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.