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 operations of a queue–which means only
Push to back, peek/pop from front, size, and was empty operations is
Valid.
- Depending on your language, queue May is not supported natively. You
May simulate a queue by using a list or deque (double-ended queue),
As long as you have only standard operations of a queue. May
Assume that all operations is valid (for example, no pop or top
Operations'll be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to Mystack instead of Stack.
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and all test cases.
Hide Tags Stack Data Structure
The problem is that we use the queue to implement a stack.
And only use the push (), pop (), front (), empty (), size () function in the queue.
For a description of the queues, you can view the description of the queue in Cpluscplus. A queue is an FIFO (fifo,first-in first-out) container adapter that is inserted at the end of the container and ejected from the other end.
A queue is implemented as a container adapter, which is a class that encapsulates an object of a particular container class as the underlying data structure, and provides some special operations to access its elements. The element is inserted at the end of the container (back) and ejected from its front end (front).
Because I have seen the use of stacks to implement the queue, it is using two stacks to implement the queue, so I would like to be able to use two queues to implement the stack, the results found really can. Define two queues as member variables of the custom stack, when you need to insert elements when you find a non-empty queue to insert elements into, the popup element will not be empty the first n-1 of the queue (n is the size of the elements in the queue) of the element popped and inserted into the empty queue, and then pop the last element, The time complexity of this insertion is O (1), The last element pops up and the time complexity of getting the last element is O (n), as shown below:
The C + + code is as follows:
Runtime:0ms
classStack { Public://Push element x onto stack. voidPushintx) {if(Queueone.empty ()) {Queuetwo.push (x); }Else{Queueone.push (x); } }//Removes the element on top of the stack. voidPop () {if(Queueone.empty ()) { while(Queuetwo.size ()! =1) {Queueone.push (Queuetwo.front ()); Queuetwo.pop (); } queuetwo.pop (); }Else{ while(Queueone.size ()! =1) {Queuetwo.push (Queueone.front ()); Queueone.pop (); } queueone.pop (); } }//Get the top element. intTop () {if(Queueone.empty ()) { while(Queuetwo.size ()! =1) {Queueone.push (Queuetwo.front ()); Queuetwo.pop (); }intResult=queuetwo.front (); Queueone.push (Queuetwo.front ()); Queuetwo.pop ();returnResult }Else{ while(Queueone.size ()! =1) {Queuetwo.push (Queueone.front ()); Queueone.pop (); }intResult=queueone.front (); Queuetwo.push (Queueone.front ()); Queueone.pop ();returnResult } }//Return whether the stack is empty. BOOLEmpty () {returnQueueone.empty () &&queuetwo.empty (); }Private: Queue<int>Queueone; Queue<int>Queuetwo;};
But there is a better solution in discuss, which only needs to use a queue. And the amount of code is greatly reduced.
Each insertion of an element into the queue, the preceding element is popped and reinserted into the queue, so that the last element inserted into the queue is always at the front of the queue, such as inserting a,b,c,d four elements, the order of elements in the queue is A,AB,ABC,ABCD, The time complexity of this insertion is O (n), and the time complexity of popping and getting the last element is O (1), isn't it wonderful? This is illustrated below:
The C + + code is as follows:
Runtime:0ms
classStack { Public://Push element x onto stack. voidPushintx) {Q.push (x); for(intI=0; I<q.size ()-1; i++) {Q.push (Q.front ()); Q.pop (); } }//Removes the element on top of the stack. voidPop () {q.pop (); }//Get the top element. intTop () {returnQ.front (); }//Return whether the stack is empty. BOOLEmpty () {returnQ.empty (); }Private: Queue<int>Q;};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode225:implement Stack using Queues