class CQueue {
public:
CQueue() {
}
stacka;
stackb;
void appendTail(int value) {
a.push(value);
}
int deleteHead() {
if(a.empty())
{
return -1;
}
else
{
while(!a.empty())
{
b.push(a.top());
a.pop();
}
int x= b.top();
b.pop();
while(!b.empty())
{
a.push(b.top());
b.pop();
}
return x;
}
}
};
这多难受啊。。
class CQueue {
stack stack1,stack2;
public:
CQueue() {
while (!stack1.empty()) {
stack1.pop();
}
while (!stack2.empty()) {
stack2.pop();
}
}
void appendTail(int value) {
stack1.push(value);
}
int deleteHead() {
// 如果第二个栈为空
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
if (stack2.empty()) {
return -1;
} else {
int deleteItem = stack2.top();
stack2.pop();
return deleteItem;
}
}
};
答案可就舒服多了!!
哭。
加油!



