A Queue plays an important role in programming because it can simulate data operations in a Queue. Example
For example, queuing for tickets is a queue operation, followed by people, followed by people first, and requested tickets
Processed first. To simulate Queue operations, Queue adds the following restrictions on ArrayList
1. elements adopt the First-In-First-Out mechanism (FIFO, First In First Out), that is, elements that First enter the queue must First
Open a queue. The first element to enter is the Header element.
· An element can only be added to the end of a team (called a queue) and cannot be inserted in a certain position in the middle. That is to say,
The Insert method in ArrayList is not supported.
2. Only the elements in the queue header can be deleted (called the queue). You cannot directly perform operations on the non-queue elements in the queue.
To ensure the FIFO mechanism. That is to say, the Remove Method in ArrayList is not supported.
3. Direct access to non-team header elements in the queue is not allowed. That is to say, index access in ArrayList is not supported.
Q: only traversal is allowed.
Note:
Queue. Enqueue (object): An object element that is queued into the Queue.
Queue. Dequeue (): An object element that is queued from the Queue and returned.
Example:
Private void button _ test Queue_Click (object sender, EventArgs e)
{
String [] cars = new string [] {"BMW", "Mercedes-Benz", "Audi", "Dongfeng", "Rolls-Royce "};
Console. WriteLine ("START queue ");
Queue <string> que = new Queue <string> ();
Foreach (string str in cars)
{
Que. Enqueue (str );
Console. WriteLine ("Incoming queue-{0}", str );
}
Console. WriteLine ();
// Print the queue
PrintQueue (que );
Console. WriteLine ();
Console. WriteLine ("START output queue ");
While (que. Count> 0)
{
String str = que. Dequeue ();
Console. WriteLine ("outbound queue-{0}", str );
}
}
Private void PrintQueue (Queue <string> list)
{
Console. WriteLine ("start printing queue ");
Foreach (string str in list)
{
Console. WriteLine (str );
}
}
---------- Running result ----------------