使用两个栈实现队列

前言

原理后续补齐,语言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();
	}
}
原文地址:https://www.cnblogs.com/aoximin/p/12526118.html