您的位置:首页 > 产品设计 > UI/UE

Implement Queue using Stacks

2015-10-23 10:42 477 查看
class Queue {
stack<int> input,output;
public:
// Push element x to the back of queue.
void push(int x) {
input.push(x);

}

// Removes the element from in front of queue.
void pop(void) {
peek();
output.pop();
}

// Get the front element.
int peek(void) {
if(output.empty())
{
while(!input.empty())
{
output.push(input.top());
input.pop();
}
}
return output.top();

}

// Return whether the queue is empty.
bool empty(void) {
if(input.empty()&&output.empty())
return true;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: