Visual Basic Queues

Queues in Visual Basic are similar to both arrays and lists, with their ability to hold multiple values of the same type, however, the order of the elements in a queue is set by the order they are added to the queue and cannot be altered. A queue can be compared to a real-life queue of people waiting to be served at a shop checkout. People join the back of the queue, whilst the next person to be served is always the person at the front. Due to this behaviour, a queue is known as a First-In First-Out, or FIFO, data structure.

Below is an example of how a queue, called ‘fname’, that contains string values, can be declared.

Dim fname As Queue(Of String) = New Queue(Of String)()

In order to add items to a queue, the ‘Enqueue’ method must be used.

fname.Enqueue("Bob")
fname.Enqueue("George")
fname.Enqueue("Fred")
fname.Enqueue("Alan")

Here, four names have been added to the queue.

It is possible to see how many items there are in the queue using the ‘Count’ property of the queue itself.

Console.WriteLine(fname.Count)

In order to see the items in a queue, without actually removing them, a ‘For Each’ loop can be used in a similar way as with arrays and lists.

For Each name As String In fname
    Console.WriteLine(name)
Next

This will display each name in the queue on a separate line in the console.

Bob
George
Fred
Alan

If it is required to see just the next item in the queue without removing it, then the ‘Peek’ method can be used.

Console.WriteLine(fname.Peek())

As well as being able to see the next item, there is also the functionality to check to see if a particular value exists in a queue. This is achieved using the ‘Contains’ method.

Console.WriteLine(fname.Contains("George"))

The above example checks to see if the value, ‘George’, exists in the queue and returns a Boolean True or False value.

In order to remove individual items from a queue, the ‘Dequeue’ method can be utilised. The example below assigns the number of items in the queue to a variable. This value is then used to limit the number of iterations in a ‘For’ loop. Within the loop the ‘Dequeue’ method is used to remove an item from the queue, display it in the console and output the number of items left in the queue.

Dim fname As Queue(Of String) = New Queue(Of String)()

fname.Enqueue("Bob")
fname.Enqueue("George")
fname.Enqueue("Fred")
fname.Enqueue("Alan")

Dim queueLength As Integer = fname.Count

Console.WriteLine("Items in queue: {0}", queueLength)
Console.WriteLine()

For i = 1 To queueLength

    Console.WriteLine("Next item from queue: {0}", fname.Dequeue())
    Console.WriteLine("Items left in queue: {0}", fname.Count)
    Console.WriteLine()
	
Next

The resulting output is shown below.

Items in queue: 4

Next item from queue: Bob
Items left in queue: 3

Next item from queue: George
Items left in queue: 2

Next item from queue: Fred
Items left in queue: 1

Next item from queue: Alan
Items left in queue: 0

Finally, if it is necessary to remove all items in a queue in one go, instead of individually with the ‘Dequeue’ method, there is a ‘Clear’ method to accomplish this.

fname.Clear()