C # collection class Queue

Source: Internet
Author: User

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 ----------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.