C# 队列<T>

Queue<T> 是一个泛型类,它使用先进先出 (FIFO) 原则来组织指定数据类型的元素。例如:

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a queue 
        Queue<string> fruits = new Queue<string>();

        // adds "Apple" and "Orange" to the queue
        fruits.Enqueue("Apple");
        fruits.Enqueue("Orange");

        // print elements of the queue 
        foreach (string item in fruits)
        {
            Console.WriteLine(item);
        }
    }
}

输出

Apple
Orange

这里,fruits 是一个包含字符串元素("Apple""Orange")的队列。

我们将详细学习 Queue<T>


队列实现

在队列中,元素以先进先出 (FIFO) 的方式存储和访问。也就是说,先添加的元素将先被移除。

Working of queue data structure: first in first out
队列数据结构的工作原理

在 C# 中创建队列

要在 C# 中创建 Queue<T>,我们需要使用 System.Collection.Generic 命名空间。以下是在 C# 中创建 Queue<T> 的方法:

Queue<dataType> queueName = new Queue<dataType>();

这里,dataType 指示队列的类型。例如:

// create integer type stack
Queue<int> queue1 = new Queue<int>();

// create string type stack
Queue<string> queue2 = new Queue<string>();

C# 队列方法

C# 提供了3个主要的 Queue<T> 方法。这些方法是:

  • Enqueue() - 将元素添加到队列的末尾
  • Dequeue() - 从队列的开头移除并返回一个元素
  • Peek() - 从队列的开头返回一个元素而不移除它

让我们逐一详细学习这些方法。


队列Enqueue() 方法

要将元素添加到队列的末尾,我们使用 Enqueue() 方法。例如:

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a queue 
        Queue<int> numbers = new Queue<int>();

// adds 65 and 17 to the queue numbers.Enqueue(65); numbers.Enqueue(17
); // print elements of the queue foreach (int item in numbers) { Console.WriteLine(item); } } }

输出

65
17

在上面的示例中,我们创建了一个名为 numbersQueue<T> 类。

然后,我们使用 Enqueue() 方法将元素添加到队列中。

  • numbers.Enqueue(65) - 将 65 添加到队列
  • numbers.Enqueue(17) - 将 17 添加到队列

然后,我们使用 foreach 循环打印这些元素。

由于队列遵循FIFO原则,因此首先添加的元素(65)在输出中也首先显示。


队列Dequeue() 方法

要从队列的开头移除一个元素,我们使用 Dequeue() 方法。例如:

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a queue 
        Queue<string> colors = new Queue<string>();

        // adds "Red" and "Blue" to the queue
        colors.Enqueue("Red");
        colors.Enqueue("Blue");

// removes element from the beginning of the colors queue var removedElement = colors.Dequeue();
Console.WriteLine("Removed Element: " + removedElement); } }

输出

Removed Element: Red

在上面的示例中,我们使用 Dequeue() 方法从 colors 队列中移除一个元素。

该方法从队列开头移除并返回了 "Red"


队列Peek() 方法

Peek() 方法在不移除元素的情况下返回队列开头的元素。例如:

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a queue 
        Queue<string> planet = new Queue<string>();

        // adds "Earth" and "Jupiter" to the queue
        planet.Enqueue("Earth");
        planet.Enqueue("Jupiter");

// returns element from the beginning of the planet queue Console.WriteLine("Element at beginning of queue: " + planet.Peek());
} }

输出

Element at beginning of queue: Earth

在这里,我们使用 Peek() 方法显示了 planet 队列开头的元素。


检查队列中是否存在某个元素

我们可以使用 Contains() 方法来检查队列中是否存在某个元素。

如果指定的元素存在于队列中,该方法将返回 True。如果不存在,则返回 False。例如:

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a queue 
        Queue<string> planet = new Queue<string>();

        // adds "Earth" and "Jupiter" to the queue
        planet.Enqueue("Earth");
        planet.Enqueue("Jupiter");

// check if queue contains "Mars" Console.WriteLine(planet.Contains("Mars"));
// check if queue contains "Jupiter" Console.WriteLine(planet.Contains("Jupiter")); } }

输出

False
True
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战