C # Queue

Source: Internet
Author: User

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.

Read and delete an element at the head of the queue, note that this element is also deleted while reading the element. If there are no more elements in the queue. Throws an exception

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.

The capacity of the queue is reset because the capacity of the queue is not reset after the Dequeue method is called to read the deleted element.

6. Example of queue usage

class Program {      static void Main( string [] args)      {          //创建一个队列          Queue myQ = new Queue();          myQ.Enqueue( "The" ); //入队          myQ.Enqueue( "quick" );          myQ.Enqueue( "brown" );          myQ.Enqueue( "fox" );          myQ.Enqueue( null ); //添加null          myQ.Enqueue( "fox" ); //添加重复的元素          // 打印队列的数量和值          Console.WriteLine( "myQ" );          Console.WriteLine( "\tCount:    {0}" , myQ.Count);          // 打印队列中的所有值          Console.Write( "Queue values:" );          PrintValues(myQ);          // 打印队列中的第一个元素,并移除          Console.WriteLine( "(Dequeue)\t{0}" , myQ.Dequeue());          // 打印队列中的所有值          Console.Write( "Queue values:" );          PrintValues(myQ);          // 打印队列中的第一个元素,并移除          Console.WriteLine( "(Dequeue)\t{0}" , myQ.Dequeue());          // 打印队列中的所有值          Console.Write( "Queue values:" );          PrintValues(myQ);          // 打印队列中的第一个元素          Console.WriteLine( "(Peek)   \t{0}" , myQ.Peek());          // 打印队列中的所有值          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. 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.

C # Queue

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.