VB.NET Enqueue, Dequeue and Peek
In this article you will learn how to use Enqueue, Dequeue and Peek in VB.NET.
Queues are one of the common classic data structures found in computer science. A queue is simply a First-In-First-Out ( FIFO) buffer where new items are added to the end of the list and the item at the front of the list is processed first. We can use Enqueue to add items in Queue, Dequeue to remove from Queue and Peek to get the reference of first item added in Queue. Lets see an example to use Enqueue, Dequeue and Peek in VB.NET.
Imports System
Imports System.Collections
Class Tester
Public Sub Run()
End Sub
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
Shared Sub Main()
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:")
' 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)
Console.ReadLine()
End Sub
End Class
OUTPUT: