×

VB.Net Programs

VB.Net program to implement Circular Queue using Structure

By Nidhi Last Updated : November 15, 2024

VB.Net – Circular Queue using Structure

Here, we will implement a Circular Queue using Array and structure. 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 using structure is given below. The given program is compiled and executed successfully.

VB.Net code to implement Circular Queue using Structure

'VB.Net program to implement Circular Queue 
'using Structure.

Imports System.IO

Module Module1
    Structure 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 Structure

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

        Q.insert(10)
        Q.insert(15)
        Q.insert(20)
        Q.insert(25)
        Q.insert(30)

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

        Q.delete()
        Q.delete()

        Q.insert(35)
        Q.insert(40)

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

Output

Items are :
Item[1]: 10
Item[2]: 15
Item[3]: 20
Item[4]: 25
Item[5]: 30
deleted element is: 10
deleted element is: 15
Items are :
Item[3]: 20
Item[4]: 25
Item[5]: 30
Item[1]: 35
Item[2]: 40
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 structure CircularQueue that contains a default constructor and insert(), delete(), and printQueue() functions.

Here, default constructor is used to initializing the members of the CircularQueque structure. 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 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.