|
|
16.3 QueuesA 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.
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 classusing 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.
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 );
|
|
|