[LeetCode-interview algorithm classic-Java implementation] [225-Implement Stack using Queues (Stack operations using Queues)],-javaqueues

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [225-Implement Stack using Queues (Stack operations using Queues)],-javaqueues
[225-Implement Stack using Queues (Stack operation with Queue )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Download the Code [https://github.com/wang-jun-chao]Original question

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 use only standard operations of a queue-which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. you may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack ).

Theme

Stack operations using queues
Push (x)-element into Stack
Pop ()-element output Stack
Top ()-Get the element value at the top of the stack
Empty ()-judge whether the stack is empty
Note:
Only standard queue operations can be used. first-in-first-out (FIFO) is used to determine the number of queue elements and whether the queue is empty.
Due to programming language reasons, some languages do not support the support queue and can be replaced by a linked list or a two-way linked list, but can only use standard queue operations.
You can assume that all the operations are valid, that is, when the queue is empty, there will be no operations for elements to exit the stack or to find the top elements of the stack.

Solutions

Simulate a stack with two queues

Code Implementation

Algorithm Implementation class

Import java. util. using list; import java. util. list; public class MyStack {// maintain two queues, one of which is always empty, preparing private List for pop and top Operations <Integer> aList = new queue List <> (); private List <Integer> bList = new external List <> (); // Push element x onto stack. public void push (int x) {// if aList is not empty, add x to aList if (! AList. isEmpty () {aList. add (x);} // otherwise, the else {bList. add (x) ;}// Removes the element on top of the stack. public void pop () {// at least one of the two queues is empty. Set aList to non-empty if (aList. isEmpty () {List <Integer> tmp = bList; bList = aList; aList = tmp;} // all except the last element are transferred to bList while (aList. size ()> 1) {bList. add (aList. remove (0);} // Delete the last element (corresponding to the top element of the stack on the stack) aList. clear ();} // Get the top element. public int top () {// at least one of the two queues is empty. Set aList to non-empty if (aList. isEmpty () {List <Integer> tmp = bList; bList = aList; aList = tmp;} // all except the last element are transferred to bList while (aList. size ()> 1) {bList. add (aList. remove (0);} bList. add (aList. get (0); return aList. remove (0);} // Return whether the stack is empty. public boolean empty () {return aList. isEmpty () & bList. isEmpty ();}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the source for reprinting [http://blog.csdn.net/derrantcm/article/details/48084069]

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.