Queue和Stack的区别(队列和栈)
Queue有两个口,那么就是先进新出,而Stack只有一个口,后进先出.
举两个例子说明;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Queue queue1 = new Queue();
queue1.Enqueue(1);
queue1.Enqueue("Hello");
int[] newArr = {9,4,5};
for (int i = 0; i < newArr.Length;i++ )
{
queue1.Enqueue(newArr[i]);
}
while (queue1.Count > 0)
{
Console.WriteLine(queue1.Dequeue());
}
Console.ReadKey();
}
}
}
输出结果:
1
Hello
9
4
5
再看用Stack的例子
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
stack.Push(1);
stack.Push("Hello");
int[] newArr = { 9, 4, 5 };
for (int i = 0; i < newArr.Length; i++)
{
stack.Push(newArr[i]);
}
while (stack.Count > 0)
{
Console.WriteLine(stack.Pop());
}
Console.ReadKey();
}
}
}
输出结果:
5
4
9
Hello
1