Leetcode 232 Implement Queue using Stacks (using stacks to implement Queues) (*)

Source: Internet
Author: User

translation
用栈来实现队列的下列操作。push(x) —— 将元素x写入到队列的尾部pop() —— 从队列首部移除元素peek() —— 返回队列首部元素empty() —— 返回队列是否为空注意:你必须使用一个只有标准操作的栈。也就是说,只有push/pop/size/empty等操作是有效的。栈可能不被原生支持,这取决于你所用的语言。只要你只是用stack的标准操作,你可以用list或者deque(double-ended queue)来模拟栈。你可以假设所有的操作都是有效的(例如,pop或peek操作不会被用在空队列上)。
Original
Implement theFollowing operations of aQueueusingStacks.push (x)--Push element x to the back of the queue.Pop ()--Removes the element from in front of the queue.Peek ()--Get the front element.Empty()--Return whether the queue is empty.Notes:you must use only standard operations of aStack--which means only push to top, peek/pop from top, size, and was empty operations is valid.DependingOn   your language, stack    is not being supported  natively. SimulateaStack by using aListorDeque (double-ended queue), as Long  asOperations of aStack. Assume that all operations is valid ( forexample, no poporPeek operations'll be calledOn an    empty queue).
Analysis

As you can see, we all need to write 4 functions, in fact, two can be directly written out.

Look at the code below, first define two stacks, and you can review the first sentence of the topic:

theofausing stacks.

In fact, Chinese is profound, English is also, here is a good disclosure is to use multiple stacks to describe a queue. A major, an auxiliary, how good ...

Push, whether it is a stack or queue, is pushed up from behind, a row;

Let's see how to take out the first element, for the queue, we peek should be the first to go in, but for the queue, we first take out is indeed the last to go in.

Oh, I forgot, there may be children's shoes don't know much about the various operations and differences of stacks and queues, you can look at my article and explain them in great detail.

"Algorithm" 7 is not clear stack and queue? A picture to give you full experience

So look at the following code, if there is only one element in the stack, then there is no doubt that it is.

If there are multiple elements, they are all transferred to the other stack first, and then the order is reversed and then an element is left.

This part is the key of pop and peek, Peek just take out this data out, this element should still be saved up, so we also give it to S2, and pop is directly to pop up, this data will not enter S2.

When the above operation is completed, the data of the S2 is transferred to S, and the data that has just been reversed is restored to the original order.

In the process of testing the code, but also write the size of the queue, in fact, just a line, that is, the size of S.

If you have finished reading this question, you can continue with the following question: Leetcode 225 Implement Stack using Queues (using queue to implement stack) (*)

Ok, this article because I was in bed at night on the pillow written, so write a little bit messy ... More than 400 have been written by serious sitting.

Code
classQueue { Public: Stack<int>s, S2;//Push element x to the back of the queue.    voidPushintx) {s.push (x); }//Removes the element from in front of the queue.    voidPopvoid) {if(s.size () = =1) S.pop ();Else{ while(S.size () >1) {S2.push (S.top ());            S.pop (); } s.pop (); while(S2.size () >0) {S.push (S2.top ());            S2.pop (); }        }    }//Get the front element.    intPeekvoid) {if(s.size () = =1)returnS.top (); while(S.size () >1) {S2.push (S.top ());        S.pop (); }inttemp = S.top ();        S2.push (S.top ()); S.pop (); while(S2.size () >0) {S.push (S2.top ());        S2.pop (); }returnTemp }//Return whether the queue is empty.    BOOLEmptyvoid) {returnS.empty (); }};

Leetcode 232 Implement Queue using Stacks (using stacks to implement Queues) (*)

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.