DekGenius.com
Team LiB   Previous Section   Next Section

16.3 Queues

A queue represents a first-in, first-out (FIFO) collection. The classic analogy is to a line (or queue if you are British) at a ticket window. The first person to join the line ought to be the first person to come off the line to buy a ticket.

The Queue class is a good collection to use when you are managing a limited resource. For example, you might want to send messages to a resource that can handle only one message at a time. You would then create a message queue so that you can say to your clients: "Your message is important to us. Messages are handled in the order in which they are received."

The Queue class has a number of member methods and properties, the most important of which are shown in Table 16-4.

Table 16-4. Queue members

Method or property

Purpose

Count

Public property that gets the number of elements in the Queue

Clear()

Method that removes all objects from the Queue

Contains()

Method that determines if an element is in the Queue

CopyTo()

Method that copies the Queue elements to an existing one-dimensional array

Dequeue()

Method that removes and returns the object at the beginning of the Queue

Enqueue()

Method that adds an object to the end of the Queue

GetEnumerator()

Method that returns an enumerator for the Queue

Peek()

Method that returns the object at the beginning of the Queue without removing it

ToArray()

Method that copies the elements to a new array

Add elements to your queue with the Enqueue() method, and take them off the queue with Dequeue() or by using an enumerator. Example 16-3 shows an example of using a Queue, followed by the output and a complete analysis.

Example 16-3. Implementing the Queue class
using System;
using System.Collections;

namespace QueueDemo
{
   class Tester
   {
      public void Run()
      {
          Queue intQueue = new Queue();

          // populate the array
          for (int i = 0;i<5;i++)
          {
              intQueue.Enqueue(i*5);
          }

          // Display the Queue.
          Console.Write( "intQueue values:\t" );
          DisplayValues( intQueue );

          // Remove an element from the Queue.
          Console.WriteLine( 
              "\n(Dequeue)\t{0}", intQueue.Dequeue() );

          // Display the Queue.
          Console.Write( "intQueue values:\t" );
          DisplayValues( intQueue );

          // Remove another element from the queue.
          Console.WriteLine( 
              "\n(Dequeue)\t{0}", intQueue.Dequeue() );

          // Display the Queue.
          Console.Write( "intQueue values:\t" );
          DisplayValues( intQueue );

          // View the first element in the 
          // Queue but do not remove.
          Console.WriteLine( 
              "\n(Peek)   \t{0}", intQueue.Peek() );

          // Display the Queue.
          Console.Write( "intQueue values:\t" );
          DisplayValues( intQueue );
      }
       public static void DisplayValues( IEnumerable myCollection )  
       {
           IEnumerator myEnumerator = 
               myCollection.GetEnumerator();
           while ( myEnumerator.MoveNext() )
               Console.Write( "{0} ",myEnumerator.Current );
           Console.WriteLine();
       }

      [STAThread]
      static void Main()
      {
         Tester t = new Tester();
         t.Run();
      }
   }
}
Output:
intQueue values:       0 5 10 15 20

(Dequeue)       0
intQueue values:       5 10 15 20

(Dequeue)       5
intQueue values:       10 15 20

(Peek)          10
intQueue values:       10 15 20

In Example 16-3, the ArrayList from Example 16-2 is replaced by a Queue. I've dispensed with the Employee class and enqueued integers to save room in the book, but of course you can enqueue user-defined objects as well.

The program begins by creating an instance of a Queue, called intQueue:

Queue intQueue = new Queue();

The queue is populated with integers:

for (int i = 0;i<5;i++)
{
    intQueue.Enqueue(i*5);
}

The contents of the queue are then displayed using the DisplayValues() method. This method takes a collection that implements the IEnumerable interface and asks that collection for its Enumerator.

Each of the collections in the .NET Framework implements IEnumerable. It then explicitly iterates over the collection, displaying each element in turn.

public static void DisplayValues( IEnumerable myCollection ) 
{
    IEnumerator myEnumerator = 
        myCollection.GetEnumerator();
    while ( myEnumerator.MoveNext() )
        Console.Write( "{0} ",myEnumerator.Current );
    Console.WriteLine();
}

You can avoid all the details of the Enumerator by using the foreach loop instead:

public static void DisplayValues( IEnumerable myCollection ) 
{
   foreach (object o in myCollection)
   {
       Console.WriteLine(o);
   }
}

Either version of DisplayValues() works equally well.

Display the first value in the queue without removing it by calling the Peek() method:

Console.WriteLine( 
    "\n(Peek)   \t{0}", intQueue.Peek() );

Or, having displayed the values in the foreach loop, remove the current value by calling the Dequeue() method:

Console.WriteLine( 
    "\n(Dequeue)\t{0}", intQueue.Dequeue() )
intQueue );
    Team LiB   Previous Section   Next Section