class SimulateQueueBy2Stacks
{
private Stack<int> _stack1 = new Stack<int>();
private Stack<int> _stack2 = new Stack<int>();
public void Enqueue(int value)
{
_stack1.Push(value);
}
public int Dequeue()
{
if(Empty())
throw new Exception("队列空");
else
{
if (_stack2.Count == 0)
{
while (_stack1.Count != 0)
{
_stack2.Push(_stack1.Pop());
}
}
return _stack2.Pop();
}
}
public bool Empty()
{
return _stack1.Count == 0 && _stack2.Count == 0;
}
}
一个栈用来进,一个栈用来出。需要满足,用来出的栈必须全部出栈才能有新值入栈,用来进的栈必须全部出栈才能有新值入栈