LeetCode 232 Implement Queue using Stacks (Queue using Stack )(*)

Source: Internet
Author: User

LeetCode 232 Implement Queue using Stacks (Queue using Stack )(*)
Translation

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

As you can see, We need to write a total of four functions. In fact, two functions can be written directly.

Let's look at the code below. First, we define two stacks. Let's review the first sentence of the question:

Implement the following operations of a queue using stacks.

In fact, the Chinese language is profound and profound, and the English language is the same. It is good to reveal that multiple stacks are used to describe a queue. How good is it ......

If push is performed, both the stack and the queue are pushed up from the back, just one line; empty is the same.

Next let's take a look at how to get the first element. For the queue, we should first get the peek, but for the queue, the first one is indeed the last one.

Oh, I forgot. Maybe some kids shoes don't know much about the operations and differences between stacks and queues. You can read my article and explain them in detail.

[Algorithm] Seven unclear stacks and queues? A picture gives you a complete understanding

So let's look at the following code. If there is only one element in the stack, there is no doubt that it is.

If there are multiple elements, first move them all to another stack. At this time, its order will be reversed, and then an element will be left.

This part is the key to pop and peek. If peek is used to extract the data, this element should still be saved, so we also gave it to s2; pop directly pops up, so this data will not enter s2.

After the preceding operations are completed, all s2 data is transferred to s. At this time, the data that has just been reversed is restored to the original order.

In the process of testing the code, the size of the Queue is also written, but it is only a row, that is, the size of s.

If you have finished writing this question, continue with the following question: LeetCode 225 Implement Stack using Queues (Stack implementation using Queue )(*)

Okay, this article is messy because I wrote it in bed with a pillow at night ...... In the past, more than four hundred articles were written by children and children.

Code
class Queue {public:    stack
  
    s, s2;    // Push element x to the back of queue.    void push(int x) {        s.push(x);    }    // Removes the element from in front of queue.    void pop(void) {        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.    int peek(void) {        if (s.size() == 1) return s.top();        while (s.size() > 1) {            s2.push(s.top());            s.pop();        }        int temp = s.top();        s2.push(s.top());        s.pop();        while (s2.size() > 0) {            s.push(s2.top());            s2.pop();        }        return temp;    }    // Return whether the queue is empty.    bool empty(void) {        return s.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.