VB.Net program to implement Linear Queue

Here, we are going to learn how to implement Linear Queue in VB.Net?
Submitted by Nidhi, on January 15, 2021 [Last updated : March 08, 2023]

VB.Net – Linear Queue

Here, we will implement a Linear Queue using Array. Linear Queue follows FIFO (First In First Out) property, which means first inserted elements, deleted first. In linear queue there are two pointers are used:

1. FRONT: It points to the location from where we can delete an item from the linear queue.
2. REAR: It points to the location from where we can insert the element into the linear queue.

Program/Source Code:

The source code to implement Linear Queue is given below. The given program is compiled and executed successfully.

VB.Net code to implement Linear Queue

```'VB.Net program to implement Linear Queue.

Imports System.IO

Module Module1
Class LinearQueue
Private ele() As Integer
Private front As Integer
Private rear As Integer
Private max As Integer

Public Sub New(ByVal size As Integer)
ReDim ele(size)
front = 0
rear = -1
max = size
End Sub

Public Sub insert(ByVal item As Integer)
If (rear = max - 1) Then
Console.WriteLine("Queue Overflow")
Else
rear = rear + 1
ele(rear) = item
End If
End Sub

Public Function delete() As Integer
Dim item As Integer = 0

If (front = rear + 1) Then
Console.WriteLine("Queue is Empty")
Return -1
Else
item = ele(front)
Console.WriteLine("deleted element is: " & item)
front = front + 1
Return item
End If

End Function

Public Sub printQueue()
If (front = rear + 1) Then
Console.WriteLine("Queue is Empty")
Else
For i = front To rear Step 1
Console.WriteLine("Item[" & (i + 1) & "]: " & ele(i))
Next
End If
End Sub
End Class

Sub Main()
Dim Q As New LinearQueue(5)

Q.insert(11)
Q.insert(22)
Q.insert(33)
Q.insert(44)
Q.insert(55)

Console.WriteLine("Items are : ")
Q.printQueue()

Q.delete()
Q.delete()

Console.WriteLine("Items are : ")
Q.printQueue()
End Sub
End Module
```

Output

```Items are :
Item[1]: 11
Item[2]: 22
Item[3]: 33
Item[4]: 44
Item[5]: 55
deleted element is: 11
deleted element is: 22
Items are :
Item[3]: 33
Item[4]: 44
Item[5]: 55
Press any key to continue . . .
```

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program, Here, we created a class LinearQueue that contains a default constructor and insert(), delete(), and printQueue() functions.

Here, default constructor is used to initializing the members of LinearQueque class. The insert() function is used to insert the item into the queue. The delete() function is used to remove the item from the queue, and the printQueue() function is used to print the item of the linear queue on the console screen.