1.栈是一种后进先出的数据结构.用两个栈实现先进先出的队列.可以考虑一个栈做入栈,一个栈做出栈.每次push入入栈,pop时从出栈出.若出栈为空,则从入栈获取出栈的元素.同时出栈时注意判断栈空.
private static Stack<int> s1=new Stack<int>();
private static Stack<int> s2=new Stack<int>();
public static void push(int node)
{
s1.Push(node);
}
public static int pop()
{
if(s1.Count==0&&s2.Count==0) throw new StackOverflowException("Queue is empty!");
if (s2.Count == 0)
{
while (s1.Count != 0)
{
s2.Push(s1.Pop());
}
}
return s2.Pop();
}