队列就像列表,遵循先进先出(FIFO)
这是一个例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Queue
{
class Program
{
static void Main(string[] args)
{
// 这里我们创建了一个自定义的只保存整数的空队列
Queue<int=> MyQueue = new Queue<int=>();
// 使用Enqueue方法我们将第一个数字(57)添加到队列中
MyQueue.Enqueue(57);
// 添加另一个数字到队列
MyQueue.Enqueue(101);
// 添加另一个数字到队列
MyQueue.Enqueue(18);
// 添加另一个数字到队列
MyQueue.Enqueue(123);
Console.WriteLine(MyQueue.Dequeue());
// dequeue方法做2件事,
// 它输出队列中的下一个项目
// 它删除从队列中删除该项目
Console.WriteLine(MyQueue.Peek());
// 这里我们不使用dequeueing访问下一个项目
Console.WriteLine(MyQueue.Dequeue());
// 我们也可以在队列中查看所有项目
Console.WriteLine("Here are the remaining items in the list:");
foreach (int number in MyQueue)
{
Console.WriteLine(number);
}
}
}
}
日期:2020-07-07 20:54:26 来源:oir作者:oir
