"225-implement stack using Queues (stack operation with queues)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
code Download "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 the only standard operations of a queue–which means only push-to-back, peek/pop from front, size, and is empty Operations is valid.
Depending on your language, queue May is not supported natively. You could simulate a queue by using a list or deque (double-ended queue), as long as if you have standard operations of a Q Ueue.
You may assume this all operations is valid (for example, no pop or top operations would be called on an empty stack).
Main Topic
Using queues to implement stack operations
Push (x) – element into the stack
Pop () – element out of stack
Top () – Take the top element value of the stack
Empty () – Determine if the stack is empty
Attention:
Can only use the standard operation of the queue, FIFO, the number of queue elements, determine whether the queue is empty
Some languages do not caress queues for programming language reasons, and can be replaced with linked lists or doubly linked lists, but only with standard queue operations
You can assume that all operations are legal, that is, when the queue is empty, there will be no element out of the stack and the operation of the top element of the stack
Thinking of solving problems
Use two queues to simulate a stack
Code Implementation
Algorithm implementation class
ImportJava.util.LinkedList;ImportJava.util.List; Public class mystack { //Maintain two queues, where there is always one queue empty, ready for pop and top operations PrivateList<integer> alist =NewLinkedlist<> ();Privatelist<integer> blist =NewLinkedlist<> ();//Push element x onto stack. Public void Push(intx) {//If alist is not empty, add X to Alist if(!alist.isempty ()) {Alist.add (x); }//Otherwise always added to Blist Else{Blist.add (x); } }//Removes the element on top of the stack. Public void Pop() {//Two at least one of the queues is empty, set alist to non-empty if(Alist.isempty ()) {list<integer> tmp = blist; Blist = alist; Alist = tmp; }//Transfer to blist except for the last element while(Alist.size () >1) {Blist.add (Alist.remove (0)); }//Delete the last element (corresponding to the stack top element)Alist.clear (); }//Get the top element. Public int Top() {//Two at least one of the queues is empty, set alist to non-empty if(Alist.isempty ()) {list<integer> tmp = blist; Blist = alist; Alist = tmp; }//Transfer to blist except for the last element while(Alist.size () >1) {Blist.add (Alist.remove (0)); } Blist.add (Alist.get (0));returnAlist.remove (0); }//Return whether the stack is empty. Public Boolean Empty() {returnAlist.isempty () && blist.isempty (); }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48084069"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "225-implement stack using Queues (stack operation with queues)"