Queue

Another familiar data structure is the queue, which is a first-in, first-out list. That is, the first item put in a queue is the first item retrieved. Queues are common in real life. For example, lines at a bank or fast-food restaurant are queues. In programming, queues are used to hold
such things as the currently executing processes in the system, a list of pending database transactions, or data packets received over the Internet. They are also often used in simulations.
 
The collection class that supports a queue is called Queue. It implements the ICollection, IEnumerable, and ICloneable interfaces. Queue is a dynamic collection that grows as needed to accommodate the elements it must store. When more room is needed, the size of the queue is increased by a growth factor, which, by default, is 2.0.
 
Queue defines the following constructors:

public Queue( )
public Queue (int capacity)
public Queue (int capacity, fl oat growFactor)
public Queue (ICollection col)

In addition to the methods defined by the interfaces that it implements, Queue defines the methods shown in following Table. In general, here is how you use Queue. To put an object in the queue, call Enqueue( ). To remove and return the object at the front of the queue,call Dequeue( ). You can use Peek( ) to return, but not remove, the next object. An InvalidOperationException is thrown if you call Dequeue( ) or Peek( ) when the invoking queue is empty.

Method Description
public virtual void Clear( ) Sets Count to zero, which effectively clears the queue.
public virtual bool Contains(object obj) Returns true if obj is in the invoking queue. If obj is not found, false is returned.
public virtual object Dequeue( ) Returns the object at the front of the invoking queue. The object is removed in the process.
public virtual void Enqueue(object obj) Adds obj to the end of the queue.
public virtual object Peek( ) Returns the object at the front of the invoking queue, but does not remove it.
public static Queue
Synchronized(Queue queue)
Returns a synchronized version of queue.
public virtual object[ ] ToArray( ) Returns an array that contains copies of the elements of the invoking queue.
public virtual void TrimToSize( ) Sets Capacity to Count.

Here is an example that demonstrates Queue:

Example

using System;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void ShowEnq(Queue q, int a)
        {
            q.Enqueue(a);
            Console.WriteLine("Enqueue(" + a + ")");
            Console.Write("queue: ");
            foreach (int i in q)
                Console.Write(i + " ");
            Console.WriteLine();
        }
        static void ShowDeq(Queue q)
        {
            Console.Write("Dequeue -> ");
            int a = (int)q.Dequeue();
            Console.WriteLine(a);
            Console.Write("queue: ");
            foreach (int i in q)
                Console.Write(i + " ");
            Console.WriteLine();
        }

        static void Main()
        {
            Queue q = new Queue();
            foreach (int i in q)
                Console.Write(i + " ");
            Console.WriteLine();
            ShowEnq(q, 22);
            ShowEnq(q, 65);
            ShowEnq(q, 91);
            ShowDeq(q);
            ShowDeq(q);
            ShowDeq(q);
            try
            {
                ShowDeq(q);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Queue empty.");
            }
        }
    }
}