Queues and Stacks using VB.NET
This article is about how to create queues and stacks in VB.Net.
Like the SortedList class, both of Queue( ) and Stack( ) classes come in typed and untyped varieties. Unlike other collections, queue and stack do not use the Ass method to add items or an index to retrieve items. Instead, queues uses the Enqueue and Dequeue methods to add and retrieve items, and stack use Push and Pop methods.
You can think of a queue (pronounced cue) as a line of items waiting to be processed. A queue can be referred to as a first-in, first-out (FIFO) collection. In contrast, a stack is a last-in, first-out (LIFO) collection. There are two examples to illustrate the differences between queue and stack:
Code that uses a Queue
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim nameQueue As New Queue(Of String)
nameQueue.Enqueue("10")
nameQueue.Enqueue("20")
nameQueue.Enqueue("30")
Dim nameQueueString As String = ""
Do While nameQueue.Count > 0
nameQueueString &= nameQueue.Dequeue & vbCrLf
Loop
MessageBox.Show(nameQueueString, "Queue")
Console.ReadLine()
End Sub
End Module
A Dialog box appears to show the output like that:
Code that uses a stack
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim namestack As New Stack(Of String)
namestack.Push("sumit")
namestack.Push("amit")
namestack.Push("ravi")
Dim namestackString As String = ""
Do While namestack.Count > 0
namestackString &= namestack.Pop & vbCrLf
Loop
MessageBox.Show(namestackString, "stack")
Console.ReadLine()
End Sub
End Module
A Dialog box appears to show the output like that: