使用两个栈实现队列

前言

原理后续补齐,语言c#

代码

class Program
{
	
	public class generateQueue
	{
		public Stack<int> stackin = new Stack<int>();

		public Stack<int> stackout = new Stack<int>();
		public void push (int element){
			stackin.Push(element);
		}

		public int outQuery()
		{
			if (stackout.Count == 0)
			{
				while (stackin.Count != 0)
				{
					stackout.Push(stackin.Pop());
				}
			}
		   return stackout.Pop();
		}

		public int count {
			get { return stackin.Count + stackout.Count; }
		}
	}
  
	static void Main(string[] args)
	{
		generateQueue generateQueueInstance = new generateQueue();
		generateQueueInstance.push(10);
		if (generateQueueInstance.count != 0)
		{
			generateQueueInstance.outQuery();
		}
		Console.ReadKey();
	}
}
posted @ 2020-03-19 18:10  敖毛毛  阅读(110)  评论(0编辑  收藏  举报