// 两个栈实现一个队列
int enqueue(int data)
{
stack1.push(data);
}
int dequeue()
{
if (stack2.size() == 0)
{
while (stack1.size() > 0)
{
stack2.push(stack1.pop());
stack1.pop();
}
}
int data = stack1.top();
stack1.pop();
return data;
}
//两个队列实现一个栈
int enstack(int data)
{
if (queue1.size() > 0)
{
queue1.push(data);
}
else if (queue2.size() > 0)
{
queue2.push(data);
}
else
{
queue1.push(data);
}
}
int destack()
{
if (queue1.size() == 0)
{
while (queue2.size() > 1)
{
queue1.push(queue2.top());
queue2.pop();
}
}
int data = queue2.top();
queue2.pop();
return data;
}
阅读(494) | 评论(0) | 转发(0) |