17.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 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 17-3.

Table 17-3. Queue methods and properties

Method or property

Purpose

Count

Public property that gets the number of elements in the Queue

Clear

Removes all objects from the Queue

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. You can also see what is next in the queue (without removing it) using Peek. Example 17-6 illustrates.

Example 17-6. Working with a queue
 Imports System.Collections.Generic Module Module1    Sub Main(  )       Dim empQueue As New Queue(Of Employee)       Dim intQueue As New Queue(Of Integer)       Dim r As New Random(  )       For counter As Integer = 0 To 8          empQueue.Enqueue(New Employee(r.Next(10) + 100))          intQueue.Enqueue(r.Next(10))       Next       '' Display the queues       Console.Write("intQueue: ")       PrintValues(intQueue)       Console.WriteLine(  )       Console.Write("empQueue: ")       PrintValues(empQueue)       Console.WriteLine(  )       '' remove one element from the queues       Console.WriteLine(vbCrLf + "intQueue.Dequeue {0}" + vbTab, _          intQueue.Dequeue(  ))       Console.WriteLine("empQueue.Dequeue {0}" + vbTab, empQueue.Dequeue(  ))       '' Display the queues       Console.Write("intQueue: ")       PrintValues(intQueue)       Console.WriteLine(  )       Console.Write("empQueue: ")       PrintValues(empQueue)       Console.WriteLine(  )       '' remove another element from the queues       Console.WriteLine(vbCrLf + "intQueue.Dequeue {0}" + vbTab, _         intQueue.Dequeue(  ))       Console.WriteLine("empQueue.Dequeue {0}" + vbTab, empQueue.Dequeue(  ))       '' Display the queues       Console.Write("intQueue: ")       PrintValues(intQueue)       Console.WriteLine(  )       Console.Write("empQueue: ")       PrintValues(empQueue)       Console.WriteLine(  )       '' peek at the first element remaining       Console.WriteLine(vbCrLf + "intQueue.Peek {0}" + vbTab, _         intQueue.Peek(  ))       Console.WriteLine("empQueue.Peek {0}" + vbTab, empQueue.Peek(  ))       '' Display the queues       Console.Write("intQueue: ")       PrintValues(intQueue)       Console.WriteLine(  )       Console.Write("empQueue: ")       PrintValues(empQueue)       Console.WriteLine(  )    End Sub    Public Sub PrintValues(ByVal myCollection As IEnumerable(Of Integer))       Dim myEnumerator As IEnumerator(Of Integer) = _         myCollection.GetEnumerator(  )       While myEnumerator.MoveNext(  )          Console.Write("{0} ", myEnumerator.Current)       End While    End Sub    Public Sub PrintValues(ByVal myCollection As IEnumerable(Of Employee))       Dim myEnumerator As IEnumerator(Of Employee) = _         myCollection.GetEnumerator(  )       While myEnumerator.MoveNext(  )          Console.Write("{0} ", myEnumerator.Current)       End While    End Sub End Module Public Class Employee    Implements IComparable(Of Employee)    Private employeeID As Integer    Public Sub New(ByVal theID As Integer)       Me.employeeID = theID    End Sub    Public Overrides Function ToString(  ) As String       Return employeeID.ToString(  )    End Function    Public Property EmpID(  ) As Integer       Get          Return employeeID       End Get       Set(ByVal value As Integer)          employeeID = value       End Set    End Property    Function CompareTo(ByVal rhs As Employee) As Integer _      Implements IComparable(Of WorkingWithQueues.Employee).CompareTo       Return Me.employeeID.CompareTo(rhs.employeeID)    End Function End Class Output: intQueue: 7 2 6 3 6 6 8 6 2 empQueue: 108 101 106 101 100 107 108 103 108 intQueue.Dequeue 7 empQueue.Dequeue 108 intQueue: 2 6 3 6 6 8 6 2 empQueue: 101 106 101 100 107 108 103 108 intQueue.Dequeue 2 empQueue.Dequeue 101 intQueue: 6 3 6 6 8 6 2 empQueue: 106 101 100 107 108 103 108 intQueue.Peek 6 empQueue.Peek 106 intQueue: 6 3 6 6 8 6 2 empQueue: 106 101 100 107 108 103 108 

VB6 Tip: In this example, I've used the traditional VbCrLf constants rather than Environment.NewLine as in previous examples. Visual Basic 2005 supports both. There is no Environment class equivalent to vbTab.


Because the Queue class is enumerable, you can pass it to the PrintValues method, which expects a reference to 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 Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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