class CQueue {
public:
CQueue() {
s1=new stack<int>;
s2=new stack<int>;
}
void appendTail(int value) {
s1->push(value);
}
int deleteHead() {
if(s2->size()<=0){
while(s1->size()>0){
int top=s1->top();
s1->pop();
s2->push(top);
}
}
if(s2->size()==0) return -1;
int head=s2->top();
s2->pop();
return head;
}
private:
stack<int>* s1;
stack<int>* s2;
};
剑指offer 面试题09. 用两个栈实现队列
最新推荐文章于 2022-03-24 02:36:00 发布