Lettcode_232_Implement Queue using Stacks, lettcode

Source: Internet
Author: User

Lettcode_232_Implement Queue using Stacks, lettcode

This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/48392363


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 onlypush to top,peek/pop from top,size, Andis emptyOperations 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 ).



Ideas:

(1) Use stacks to implement queues.

(2) to use stacks to implement queues, you must first understand the nature of stacks and queues. STACK: first-in-first-out, only adding and deleting elements at the top of the stack; queue: first-in-first-out, only adding elements at the end of the team and deleting elements from the team header. In this way, to use stacks to implement queues, You need to operate on two stacks. Here, you need to specify one stack as the stack of storage elements, which is assumed to be stack2, and the other is stack1. When an element is added, first determine whether stack2 is null (stack2 can be considered as the object of the element stored in the target Queue). If it is not empty, you need to put all the elements in stack2 into stack1 (Auxiliary stack), so that the first element stored in stack1 is the team-end element, and then add the elements to be added to the queue to stack1, this is equivalent to putting all elements in stack1 into stack2, so that the top element of stack2 becomes the first element in the queue, the pop and peek operations on the queue can be directly performed on stack2.

(3) For details, see the code below. I hope this article will help you.


The algorithm code is implemented as follows:

package leetcode;import java.util.Stack;/** * @author liqqc * */public class Implement_Queue_using_Stacks {public Stack<Integer> _stack1 = new Stack<Integer>();public Stack<Integer> _stack2 = new Stack<Integer>();public void push(int x) {while (!_stack2.isEmpty()) {_stack1.push(_stack2.pop());}_stack1.push(x);while (!_stack1.isEmpty()) {_stack2.push(_stack1.pop());}}// Removes the element from in front of queue.public void pop() {_stack2.pop();}// Get the front element.public int peek() {return _stack2.peek();}// Return whether the queue is empty.public boolean empty() {return _stack2.isEmpty();}}



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.