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

3.4 Queue via Stacks

2015-10-27 12:35 387 查看
Using two stacks to implement queue.

class Queue {
public:
stack<int> stkIn;
stack<int> stkOut;
// Push element x to the back of queue.
void push(int x) {
stkIn.push(x);
}

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

// Get the front element.
int peek(void) {
if(stkOut.empty()){
while(stkIn.size()){
stkOut.push(stkIn.top());
stkIn.pop();
}
}
return stkOut.top();
}

// Return whether the queue is empty.
bool empty(void) {
return (stkOut.empty() && stkIn.empty());
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stack