VB.Net program to implement Circular Queue

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

VB.Net – Circular Queue

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

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

In the Circular queue, we remove the problem of the linear queue, In the Linear queue, we cannot insert the element at the deleted location because the front is moved ahead after deletion.

Here (by using circular queue), we move rear and front pointers circularly, if the front or rear reached at end of the array then it moves to the 0th location of the array.

Program/Source Code:

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

VB.Net code to implement Circular Queue

'VB.Net program to implement Circular Queue.

Imports System.IO

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

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

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

                count = count + 1
            End If
        End Sub

        Public Sub delete()
            If (count = 0) Then
                Console.WriteLine("Queue is Empty")
            Else
                Console.WriteLine("deleted element is: " & ele(front))
                front = (front + 1) Mod max
                count = count - 1
            End If
        End Sub


        Public Sub printQueue()
            Dim i As Integer = 0
            Dim j As Integer = 0
            If (count = 0) Then
                Console.WriteLine("Queue is Empty")
            Else
                i = front
                While j < count
                    Console.WriteLine("Item[" & (i + 1) & "]: " & ele(i))
                    i = (i + 1) Mod max
                    j = j + 1
                End While
            End If
        End Sub
    End Class

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

        Q.insert(2)
        Q.insert(4)
        Q.insert(6)
        Q.insert(7)
        Q.insert(9)

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

        Q.delete()
        Q.delete()

        Q.insert(3)
        Q.insert(5)

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

Output

Items are :
Item[1]: 2
Item[2]: 4
Item[3]: 6
Item[4]: 7
Item[5]: 9
deleted element is: 2
deleted element is: 4
Items are :
Item[3]: 6
Item[4]: 7
Item[5]: 9
Item[1]: 3
Item[2]: 5
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 CircularQueue that contains a default constructor and insert(), delete(), and printQueue() functions.

Here, default constructor is used to initializing the members of CircularQueque 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 circular queue on the console screen.

VB.Net Data Structure Programs »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.