9.6 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 in line ought to be the first person to come off the line to buy a ticket.

A queue 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 only handle 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, as shown in Table 9-4.

Table 9-4. Queue methods and properties

Method or property

Purpose

Synchronized( )

Public static method that returns a Queue wrapper that is thread-safe.

Count

Public property that gets the number of elements in the Queue.

IsSynchronized

Public property to get a value indicating if the Queue is synchronized.

SyncRoot

Public property that returns an object that can be used to synchronize access to the Queue.

Clear( )

Removes all objects from the Queue.

Clone( )

Creates a shallow copy.

Contains( )

Determines if an element is in the Queue.

CopyTo( )

Copies the Queue elements to an existing one-dimensional array.

Dequeue( )

Removes and returns the object at the beginning of the Queue.

Enqueue( )

Adds an object to the end of the Queue.

GetEnumerator( )

Returns an enumerator for the Queue.

Peek( )

Returns the object at the beginning of the Queue without removing it.

ToArray( )

Copies the elements to a new array.

Add elements to your queue with the Enqueue command and take them off the queue with Dequeue or by using an enumerator. Example 9-15 illustrates.

Example 9-15. Working with a queue
namespace Programming_CSharp {    using System;    using System.Collections;       public class Tester    {       static void Main( )       {                    Queue intQueuee = new Queue( );          // populate the array          for (int i = 0;i<5;i++)          {                       intQueuee.Enqueue(i*5);          }          // Display the Queue.          Console.Write( "intQueuee values:\t" );          PrintValues( intQueuee );          // Remove an element from the queue.          Console.WriteLine(              "\n(Dequeue)\t{0}", intQueuee.Dequeue( ) );          // Display the Queue.          Console.Write( "intQueuee values:\t" );          PrintValues( intQueuee );          // Remove another element from the queue.          Console.WriteLine(              "\n(Dequeue)\t{0}", intQueuee.Dequeue( ) );          // Display the Queue.          Console.Write( "intQueuee values:\t" );          PrintValues( intQueuee );          // View the first element in the           // Queue but do not remove.          Console.WriteLine(              "\n(Peek)   \t{0}", intQueuee.Peek( ) );          // Display the Queue.          Console.Write( "intQueuee values:\t" );          PrintValues( intQueuee );       }       public static void PrintValues( IEnumerable myCollection )         {          IEnumerator myEnumerator =              myCollection.GetEnumerator( );          while ( myEnumerator.MoveNext( ) )             Console.Write( "{0} ",myEnumerator.Current );          Console.WriteLine( );       }    } } Output: intQueuee values:       0 5 10 15 20 (Dequeue)       0 intQueuee values:       5 10 15 20 (Dequeue)       5 intQueuee values:       10 15 20 (Peek)          10 intQueuee values:       10 15 20

In this example the ArrayList is replaced by a Queue. I've dispensed with the Employee class to save room, but of course you can Enqueue user-defined objects as well.

The output shows that queuing objects adds them to the Queue, and calls to Dequeue return the object and also remove them from the Queue. The Queue class also provides a Peek( ) method that allows you to see, but not remove, the first element.

Because the Queue class is enumerable, you can pass it to the PrintValues method, which is provided as an IEnumerable interface. The conversion is implicit. In the PrintValues method you call GetEnumerator, which you will remember is the single method of all IEnumerable classes. This returns an IEnumerator, which you then use to enumerate all the objects in the collection.



Programming C#
C# Programming: From Problem Analysis to Program Design
ISBN: 1423901460
EAN: 2147483647
Year: 2003
Pages: 182
Authors: Barbara Doyle

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net