C # queue (Queue)

Queue (Queue) represents a FIFO collection of objects. When you need access to the FIFO, then use the queue. When you add a list, called into the team , when you remove the item from the list, called a team .

Queue class of methods and properties

The following table lists Queue some common class attributes :

Attributes description
Count Get the number of elements contained in the Queue.

The following table lists Queue some common class method :

No. Methods Name & Description
1 public virtual void Clear ();
removing all the elements from the Queue.
2 public virtual bool Contains (object obj)
; determining whether an element in the Queue.
3 public virtual object Dequeue ();
remove the object and returns at the beginning of Queue.
4 public virtual void Enqueue (object obj)
; add an object to the end of the Queue.
5 public virtual object [] ToArray ()
; Copy Queue to a new array.
6 public virtual void TrimToSize ();
set a capacity of the actual number of elements in the Queue.

Examples

The following example demonstrates the use of a queue (Queue) is:

Examples

using System;
using System.Collections;

namespace CollectionsApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         Queue q = new Queue();

         q.Enqueue('A');
         q.Enqueue('M');
         q.Enqueue('G');
         q.Enqueue('W');
         
         Console.WriteLine("Current queue: ");
         foreach (char c in q)
            Console.Write(c + " ");
         Console.WriteLine();
         q.Enqueue('V');
         q.Enqueue('H');
         Console.WriteLine("Current queue: ");        
         foreach (char c in q)
            Console.Write(c + " ");
         Console.WriteLine();
         Console.WriteLine("Removing some values ");
         char ch = (char)q.Dequeue();
         Console.WriteLine("The removed value: {0}", ch);
         ch = (char)q.Dequeue();
         Console.WriteLine("The removed value: {0}", ch);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Current queue: 
A M G W 
Current queue: 
A M G W V H 
Removing values
The removed value: A
The removed value: M

 

 

Published 224 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/xulong5000/article/details/104970393