15.4 QueuesA queue represents a first-in first-out (FIFO) collection. The classic analogy is a line (or queue if you are British) at a ticket window. The first person to get in 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 15-4. Table 15-4. Queue members
Add elements to your queue with the Enqueue( ) method, and take them off the queue with Dequeue( ) or by using an enumerator. Example 15-4 shows how to use a Queue, followed by the output and a complete analysis. Example 15-4. Implementing the Queue classOption Strict On Imports System Namespace QueueDemo Class Tester Public Sub Run( ) Dim intQueue As New Queue( ) ' populate the array Dim i As Integer For i = 0 To 4 intQueue.Enqueue((i * 5)) Next i ' Display the Queue. Console.WriteLine("intQueue values:") DisplayValues(intQueue) ' Remove an element from the queue. Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( )) ' Display the Queue. Console.WriteLine("intQueue values:") DisplayValues(intQueue) ' Remove another element from the queue. Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( )) ' Display the Queue. Console.WriteLine("intQueue values:") DisplayValues(intQueue) ' View the first element in the ' Queue but do not remove. Console.WriteLine("(Peek) {0}", intQueue.Peek( )) ' Display the Queue. Console.WriteLine("intQueue values:") DisplayValues(intQueue) End Sub 'Run Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable) Dim myEnumerator As IEnumerator = myCollection.GetEnumerator( ) While myEnumerator.MoveNext( ) Console.WriteLine("{0} ", myEnumerator.Current) End While Console.WriteLine( ) End Sub 'DisplayValues Shared Sub Main( ) Dim t As New Tester( ) t.Run( ) End Sub 'Main End Class 'Tester End Namespace 'QueueDemo 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 15-4, the ArrayList from Example 15-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: Dim intQueue As New Queue( ) The queue is populated with integers: For i = 0 To 4 intQueue.Enqueue((i * 5)) Next i The contents of the queue are then displayed using the DisplayValues( ) method. This method takes a collection that implements the IEnumerable interface (as does each of the collections provided by the .NET Framework) and asks that collection for its Enumerator. It then explicitly iterates over the collection, displaying each element in turn . Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable) Dim myEnumerator As IEnumerator = myCollection.GetEnumerator( ) While myEnumerator.MoveNext( ) Console.Write("{0} ", myEnumerator.Current) End While Console.WriteLine( ) End Sub 'DisplayValues
You can avoid all the details of the Enumerator by using the For Each loop instead: Public Shared Sub DisplayValues( _ ByVal myCollection As IEnumerable) Dim o As Object For Each o In myCollection Console.WriteLine(o) Next End Sub 'DisplayValues Either version of DisplayValues( ) will work equally well. You can display the first value in the queue without removing it by calling the Peek( ) method: Console.WriteLine("(Peek) {0}", intQueue.Peek( )) Or, having displayed the values in the For Each loop, you can remove the current value by calling the Dequeue( ) method: Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( )) |