📜  C#|队列类

📅  最后修改于: 2021-05-29 14:24:25             🧑  作者: Mango

队列代表对象的先进先出集合。当您需要对项目进行先进先出的访问时,可以使用它。当您在列表中添加项目时,它称为enqueue ,而当您删除项目时,它称为dequeue 。此类位于System.Collections命名空间下,并实现ICollection,IEnumerable和ICloneable接口。

队列类的特征:

  • Enqueue将元素添加到队列的末尾。
  • 出队从队列开始处删除最旧的元素。
  • Peek返回位于队列开始处的最旧元素,但不会将其从队列中删除。
  • 队列的容量是队列可以容纳的元素数。
  • 随着元素添加到队列中,通过重新分配内部数组,容量会根据需要自动增加。
  • 队列接受null作为引用类型的有效值,并允许重复的元素。

建设者

Constructor Description
Queue() Initializes a new instance of the Queue class that is empty, has the default initial capacity, and uses the default growth factor.
Queue(ICollection) Initializes a new instance of the Queue class that contains elements copied from the specified collection, has the same initial capacity as the number of elements copied, and uses the default growth factor.
Queue(Int32) Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the default growth factor.
Queue(Int32, Single) Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the specified growth factor.

例子:

// C# code to create a Queue
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue 
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("one");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("two");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("three");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("four");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("five");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("six");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
    }
}
输出:
Total number of elements in the Queue are : 1
Total number of elements in the Queue are : 2
Total number of elements in the Queue are : 3
Total number of elements in the Queue are : 4
Total number of elements in the Queue are : 5
Total number of elements in the Queue are : 6

特性

Property Description
Count Gets the number of elements contained in the Queue.
IsSynchronized Gets a value indicating whether access to the Queue is synchronized (thread safe).
SyncRoot Gets an object that can be used to synchronize access to the Queue.

例子:

// C# code to Get the number of
// elements contained in the Queue
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("Chandigarh");
        myQueue.Enqueue("Delhi");
        myQueue.Enqueue("Noida");
        myQueue.Enqueue("Himachal");
        myQueue.Enqueue("Punjab");
        myQueue.Enqueue("Jammu");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
    }
}
输出:
Total number of elements in the Queue are : 6

方法

Method Description
Clear() Removes all objects from the Queue.
Clone() Creates a shallow copy of the Queue.
Contains(Object) Determines whether an element is in the Queue.
CopyTo(Array, Int32) Copies the Queue elements to an existing one-dimensional Array, starting at the specified array index.
Dequeue() Removes and returns the object at the beginning of the Queue.
Enqueue(Object) Adds an object to the end of the Queue.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an enumerator that iterates through the Queue.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
Peek() Returns the object at the beginning of the Queue without removing it.
Synchronized(Queue) Returns a new Queue that wraps the original queue, and is thread safe.
ToArray() Copies the Queue elements to a new array.
ToString() Returns a string that represents the current object.
TrimToSize() Sets the capacity to the actual number of elements in the Queue.

范例1:

// C# code to Check if a Queue
// contains an element
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue(5);
        myQueue.Enqueue(10);
        myQueue.Enqueue(15);
        myQueue.Enqueue(20);
        myQueue.Enqueue(25);
  
        // Checking whether the element is
        // present in the Queue or not
        // The function returns True if the
        // element is present in the Queue, else
        // returns False
        Console.WriteLine(myQueue.Contains(7));
    }
}
输出:
False

范例2:

// C# code to Convert Queue to array
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue 
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("Geeks");
        myQueue.Enqueue("Geeks Classes");
        myQueue.Enqueue("Noida");
        myQueue.Enqueue("Data Structures");
        myQueue.Enqueue("GeeksforGeeks");
  
        // Converting the Queue
        // into object array
        Object[] arr = myQueue.ToArray();
  
        // Displaying the elements in array
        foreach(Object obj in arr)
        {
            Console.WriteLine(obj);
        }
    }
}
输出:
Geeks
Geeks Classes
Noida
Data Structures
GeeksforGeeks

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.queue?view=netframework-4.7.2