标签:
Implement the following operations of a queue using stacks.
push
to top, peek/pop from top, size,
and is empty operations are valid.class Queue {
private:
stack<int> stack1;
stack<int> stack2;
public:
// Push element x to the back of queue.
void push(int x) {
stack1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(!stack2.empty()){
stack2.pop();
}
else{
while(!stack1.empty()){
int temp = stack1.top();
stack1.pop();
stack2.push(temp);
}
stack2.pop();
}
}
// Get the front element.
int peek(void) {
if(!stack2.empty()){
return stack2.top();
}
else{
while(!stack1.empty()){
int temp = stack1.top();
stack1.pop();
stack2.push(temp);
}
return stack2.top();
}
}
// Return whether the queue is empty.
bool empty(void) {
return stack1.empty()&&stack2.empty();
}
};版权声明:本文为博主原创文章,未经博主允许不得转载。
[LeetCode]Implement Queue using Stacks
标签:
原文地址:http://blog.csdn.net/ciaoliang/article/details/46940033