Original title Address
https://leetcode.com/problems/implement-stack-using-queues/ Topic Description
Implement the following operations of a stack using queues.
Using a queue to implement a stack consists mainly of the following methods: Push (x) –push element x onto stack. into the stack pop () –removes the element on top of the stack. Stack top () –get the top element. Gets the top element of the stack empty () –return whether the stack is empty. Returns whether the stack is empty
Notes:
Attention:
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.
Only the standard operation of the queue can be used, that is: push is inserted at the end of the team, Peek/pop is ejected from the first team, size gets the queue sizes, empty.
You may assume this all operations is valid (for example, no pop or top operations would be called on an empty stack).
Assume that all operations are legitimate (for example, the pop and top methods are not called when the stack is empty). Thinking of solving problems
Use two queues to simulate a stack. Suppose there are two stacks A and B, and at least one of them is empty.
1. Enter the stack push ()
When we insert an element x, we put x in the queue where it is empty, and then put all the elements in the other queue into the queue sequentially . As follows:
Demo 1,2,3,4 into the stack
(1) push (1)
-------------- --------------
A: 1 B:
-------------- -- ------------
(2) push (2)
-------------- --------------
A: B: 2,1
------------ -- --------------
(3) push (3)
-------------- --------------
A: 3,2,1 B:
-------------- --------------
(4) push (4)
-------------- --------------
A: B: 4,3,2,1
-------------- --------------
2. Get Top ()/stack pop ()
It is simple to get the top element and the stack operation, because one of the two queues is empty, we just need to remove or eject the first element from the queue that never is empty.
(5) Top ()---> Return 4
-------------- --------------
A: B: 4,3,2,1
-------------- --------------
(5) Pop ()
-------------- --------------
A: B: 3,2,1
------------ -- --------------
3. Null empty ()
When two queues are empty at the same time, the description stack is empty. Code
The code is really simple.
Class Stack {public://Push element x onto Stack.
void push (int x) {if (A.empty ()) {A.push (x));
while (!b.empty ()) {A.push (B.front ());
B.pop ();
}} else {B.push (x);
while (!a.empty ()) {B.push (A.front ());
A.pop ();
}}}//Removes the element on top of the stack.
void Pop () {if (A.empty ()) B.pop ();
else A.pop ();
}//Get the top element.
int Top () {return A.empty ()? B.front (): A.front ();
}//Return whether the stack is empty.
bool Empty () {return a.empty () && b.empty ();
} private://Two queues to simulate a stack of queue<int> A;
Queue<int> b;
};
int main () {cout << "add [1-100] to stack." << Endl;
Stack s;
for (int i = 1; i < 101; ++i) S.push (i); while (!s.empty ()) {cout << s.top() << ",";
S.pop ();
} return 0; }
Full Code Https://github.com/Orange1991/leetcode/blob/master/225/cpp/main.cpp
2015/8/28