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

相关文章:

  • 2021-06-24
  • 2021-06-13
  • 2022-12-23
  • 2021-09-16
  • 2021-12-10
  • 2021-11-17
  • 2022-12-23
  • 2021-11-05
猜你喜欢
  • 2022-12-23
  • 2021-10-03
  • 2021-11-17
  • 2021-11-17
  • 2021-11-29
相关资源
相似解决方案