//原理,利用两个栈,互相作用,来模仿堆的效果,先进先出。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace TwoStacksQueue
{
public class Program
{
public void Main(string[] args)
{
TwoStaksQueues twoStaksQueues = new TwoStaksQueues(); twoStaksQueues.Add();
twoStaksQueues.Add();
twoStaksQueues.Add(); Console.WriteLine(twoStaksQueues.Peek()); twoStaksQueues.Poll(); Console.WriteLine(twoStaksQueues.Peek()); Console.ReadLine();
}
public class TwoStaksQueues
{
public Stack<int> stacksPush;
public Stack<int> stackPop;
public TwoStaksQueues()
{
stackPop = new Stack<int>();
stacksPush = new Stack<int>();
}
public void Add(int newNumber)
{
stacksPush.Push(newNumber);
} public int Poll()
{
if (stacksPush.Count() == && stackPop.Count() == )
{ throw new ArgumentOutOfRangeException("DataStacks is Empty");
}
if (stackPop.Count() == )
{
while (stacksPush.Count() != )
{
stackPop.Push(stacksPush.Pop());
}
}
return stackPop.Pop();
}
public int Peek()
{
if (stacksPush.Count() == && stackPop.Count() == )
{
throw new IndexOutOfRangeException("DataStacks is Empty");
}
if (stackPop.Count() == )
{
while (stacksPush.Count() != )
{
stackPop.Push(stacksPush.Pop());
}
}
return stackPop.Peek(); }
}
}
}