1. Queue definition
The System.Collections.Queue class represents an advanced first-out collection of objects, objects stored in the queue (queues) are inserted at one end, and removed from the other end.
2. Advantages
1, the collection can be processed sequentially (FIFO).
2, can accept null values, and allow duplicate elements.
3, queue of the constructor
Constructor functions |
Comments |
Queue () |
Initializes a new instance of the Queue class that is empty, has the default initial capacity (32), and uses the default growth factor (2.0). |
Queue (ICollection) |
Initializes a new instance of the Queue class that contains the 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. |
4. Queue properties
Property name |
Comments |
Count |
Gets the number of elements contained in the Queue. |
5. How to queue
Method name |
Comments |
Void Clear () |
Removes all objects from the Queue. |
Bool Contains (Object obj) |
Determines whether an element is in a Queue. |
Object Clone () |
Creates a shallow copy of the Queue. |
Void CopyTo (Array array,int index) |
Copies the Queue element to an existing one-dimensional array starting at the specified array index. |
Object Dequeue () |
Removes and returns the object at the beginning of the Queue. |
Void Enqueue (Object obj) |
Adds an object to the end of the Queue. |
Object Peek () |
Returns the object at the beginning of the Queue but does not remove it. |
Object[]toarray () |
Copies the Queue element to the new array. |
Void TrimToSize () |
Sets the capacity to the actual number of elements in the Queue. |
6. Example of queue usage
classProgram {Static voidMain (string[] args) { //Create a queueQueue MyQ =NewQueue (); Myq.enqueue (" the");//QueueMyq.enqueue ("Quick"); Myq.enqueue ("Brown"); Myq.enqueue ("Fox"); Myq.enqueue (NULL);//add nullMyq.enqueue ("Fox");//add a repeating element//number and value of print queuesConsole.WriteLine ("MyQ"); Console.WriteLine ("\tcount: {0}", Myq.count); //print all values in a queueConsole.Write ("Queue values:"); Printvalues (MyQ); //prints the first element in a queue and removesConsole.WriteLine ("(Dequeue) \t{0}", Myq.dequeue ()); //print all values in a queueConsole.Write ("Queue values:"); Printvalues (MyQ); //prints the first element in a queue and removesConsole.WriteLine ("(Dequeue) \t{0}", Myq.dequeue ()); //print all values in a queueConsole.Write ("Queue values:"); Printvalues (MyQ); //Print the first element in a queueConsole.WriteLine ("(Peek) \t{0}", Myq.peek ()); //print all values in a queueConsole.Write ("Queue values:"); Printvalues (MyQ); Console.ReadLine (); } Public Static voidprintvalues (IEnumerable mycollection) {foreach(Object objinchmycollection) Console.Write ("{0}", obj); Console.WriteLine (); }
7. Notes
1. The queue capacity is the number of elements that the queue can hold. The default initial capacity of the Queue is 32. When you add elements to a Queue, the capacity is automatically increased as needed by reallocation. You can reduce capacity by calling TrimToSize. The equal factor is the number of the current capacity to multiply when a larger capacity is required. Determines the growth factor when constructing a Queue. The default growth factor is 2.0.
2. The Queue can accept null references as valid values and allow duplicate elements.
3. Null references can be added to the Queue as values. To differentiate between null values and queue endings, check the Count property or the InvalidOperationException exception that is thrown when the queue is empty.
Analysis of C # Queue