1. Queue Definition
The system. Collections. Queue class indicates the object's first-in-first-out set. objects stored in the queue are inserted at one end and removed from the other end.
2. Advantages
1. sequential processing of sets (first-in-first-out ).
2. elements that can accept null values and repeat are allowed.
3. Queue Constructor
Constructors |
Note |
Queue () |
Initialize a new instance of the queue class. This instance is empty and has the default initial capacity (32) and uses the default growth factor (2.0 ). |
Queue (icollection) |
Initializes a new instance of the queue class. This instance contains elements copied from the specified set, has the same initial capacity as the number of copied elements, and uses the default growth factor. |
Queue (int32) |
Initialize a new instance of the queue class. This instance is empty and has the specified initial capacity and uses the default growth factor. |
Queue (int32, Single) |
Initialize a new instance of the queue class. This instance is empty and has the specified initial capacity and uses the specified growth factor. |
4. Attributes of queue
Attribute name |
Note |
Count |
Obtains the number of elements contained in a queue. |
5. Queue Method
Method Name |
Note |
Void clear () |
Remove all objects from the queue. |
Bool contains (Object OBJ) |
Determine whether an element is in the queue. |
Object clone () |
Create a superficial copy of the queue. |
Void copyto (array, int index) |
Copy the queue element from the specified array index to the existing one-dimensional array. |
Object dequeue () |
Remove and return the object at the beginning of the queue. |
Void enqueue (Object OBJ) |
Add the 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 () |
Copy the queue element to the new array. |
Void trimtosize () |
Set the capacity to the actual number of elements in the queue. |
6. Queue usage example
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
class Program { static void Main( string [] args) { // Create a queue Queue myQ = new Queue(); myQ.Enqueue( "The" ); // Enter the team myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); myQ.Enqueue( null ); // Add null myQ.Enqueue( "fox" ); // Add duplicate elements // Print the number and value of the queue Console.WriteLine( "myQ" ); Console.WriteLine( "\tCount: {0}" , myQ.Count); // Print all values in the queue Console.Write( "Queue values:" ); PrintValues(myQ); // Print the first element in the queue and remove it. Console.WriteLine( "(Dequeue)\t{0}" , myQ.Dequeue()); // Print all values in the queue Console.Write( "Queue values:" ); PrintValues(myQ); // Print the first element in the queue and remove it. Console.WriteLine( "(Dequeue)\t{0}" , myQ.Dequeue()); // Print all values in the queue Console.Write( "Queue values:" ); PrintValues(myQ); // Print the first element in the queue Console.WriteLine( "(Peek) \t{0}" , myQ.Peek()); // Print all values in the queue Console.Write( "Queue values:" ); PrintValues(myQ); Console.ReadLine(); } public static void PrintValues(IEnumerable myCollection) { foreach (Object obj in myCollection) Console.Write( " {0}" , obj); Console.WriteLine(); } } |
7. Remarks
1. The capacity of the queue is the number of elements that can be stored by the queue. The default initial capacity of queue is 32. When an element is added to the queue, the capacity is automatically increased as needed by reallocation. You can call trimtosize to reduce the capacity. The proportional factor is the number multiplied by the current capacity when a larger capacity is required. Determine the growth factor when constructing the queue. Default growth factor is 2.0.
2. Queue can accept null references as valid values and allow repeated elements.
3. An empty reference can be added to the queue as a value. To distinguish between the null value and the end of the queue, check the Count attribute or catch the invalidoperationexception thrown when the queue is empty.