队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead
操作返回 -1 )
输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]
输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]
class CQueue {
private:
stack<int> instack;
stack<int> outstack;/*一个作为输入栈,一个作为输出栈*/
public:
CQueue() {
}
void appendTail(int value) {
instack.push(value);
}
int deleteHead() {
if(!outstack.empty()){
int value = outstack.top();
outstack.pop();
return value;
}
else{
while(!instack.empty()){
int top = instack.top();
instack.pop();
outstack.push(top);
}
if(outstack.empty()){
return -1;
}else{
int output = outstack.top();
outstack.pop();
return output;
}
}
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/