//利用双栈法
Stack<int> inputStack = new Stack<int>();
Stack<int> outStack = new Stack<int>();
void Push(int x)
{
inputStack.Push(x);
}
int Pop()
{
Adjust();
return outStack.Pop();
}
int Peek()
{
Adjust();
return outStack.Peek();
}
void Adjust()
{ if (outStack.Count>0) return;
while (inputStack.Count>0)
{
outStack.Push(inputStack.Pop());
}
}
利用临时栈
Stack<int> resStack = new Stack<int>();
void Push1(int x)
{
Stack<int> tempStack = new Stack<int>();
while (resStack.Count>0)
{
tempStack.Push(resStack.Pop());
}
tempStack.Push(x);
while (tempStack.Count>0)
{
resStack.Push(tempStack.Pop());
}
}
网友评论