C # Set queue

Source: Internet
Author: User
ArticleDirectory
    • 1. Queue Definition
    • 2. Advantages
    • 3. Queue Constructor
    • 4. Attributes of queue
    • 5. Queue Method
    • 6. Queue usage example
    • 7. Remarks
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

 

Code

     Class  Program
{
Static Void Main ( String [] ARGs)
{
// Create a queue
Queue myq = New Queue ();
Myq. enqueue ( " The " ); // Join
Myq. enqueue ( " Quick " );
Myq. enqueue ( " Brown " );
Myq. enqueue ( " Fox " );
Myq. enqueue ( Null ); // Add null
Myq. enqueue ( " Fox " ); // Add repeated 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 capture the invalidoperationexception triggered when the queue is empty.

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.