#include <iostream>
#include <stack>
#include <exception>
using namespace std;
class TwoQue
{
public:
void add(int num)
{
stackPush.push(num);
}
int poll()
{
if(stackPop.empty() && stackPush.empty())
throw new exception();
else if(stackPop.empty())
{
while(!stackPush.empty())
{
int val = stackPush.top();
stackPop.push(val);
stackPush.pop();
}
}
int val = stackPop.top();
stackPop.pop();
return val;
}
int top()
{
if(stackPop.empty() && stackPush.empty())
throw new exception();
else if(stackPop.empty())
{
while(!stackPush.empty())
{
int val = stackPush.top();
stackPop.push(val);
stackPush.pop();
}
}
return stackPop.top();
}
private:
stack<int> stackPush;
stack<int> stackPop;
};
俩个栈模拟队列
猜你喜欢
转载自blog.csdn.net/wzc2608/article/details/80563930
今日推荐
周排行