C# program to implement the Circular Queue using structure

Here, we are going to learn how to implement the Circular Queue using structure in C#?
Submitted by Nidhi, on November 05, 2020 [Last updated : March 27, 2023]

Circular Queue Using Structure

Here, we will implement a Circular Queue using structure.

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, and then it moves to the 0th location of the array.

Circular Queue Implementation Using Structure in C#

The source code to implement Circular Queue using structure is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to implement the Circular Queue using the structure

using System;

struct CircularQueue
{
    private int[]items;

    private int FRONT;
    private int REAR;

    private int MAX;
    private int COUNT;

    public CircularQueue(int size)
    {
        items = new int[size];
        FRONT = 0;
        REAR = -1;
        MAX = size;
        COUNT = 0;
    }

    public void InsertItem(int item)
    {
        if (COUNT == MAX)
        {
            Console.WriteLine("Queue Overflow");
            return;
        }
        else
        {
            REAR = (REAR + 1) % MAX;
            items[REAR] = item;

            COUNT++;
        }
    }

    public void DeleteItem()
    {
        if (COUNT == 0)
        {
            Console.WriteLine("Queue is Empty");
        }
        else
        {
            Console.WriteLine("deleted element is: " + items[FRONT]);

            FRONT = (FRONT + 1) % MAX;

            COUNT--;
        }
    }

    public void PrintCircularQueue()
    {
        int i = 0;
        int j = 0;

        if (COUNT == 0)
        {
            Console.WriteLine("Queue is Empty");
            return;
        }
        else
        {
            for (i = FRONT; j < COUNT; )
            {
                Console.WriteLine("\tItem[" + (i + 1) + "]: " + items[i]);

                i = (i + 1) % MAX;
                j++;

            }
        }
    }
}

class Demo
{
    static void Main()
    {
        CircularQueue Q = new CircularQueue(5);

        Q.InsertItem(101);
        Q.InsertItem(202);
        Q.InsertItem(302);
        Q.InsertItem(406);
        Q.InsertItem(534);

        Console.WriteLine("Circular Queue Items are : ");
        Q.PrintCircularQueue();

        Q.DeleteItem();
        Q.DeleteItem();

        Q.InsertItem(786);
        
        Console.WriteLine("Circular Queue Items are : ");
        Q.PrintCircularQueue();
    }
}

Output

Circular Queue Items are :
        Item[1]: 101
        Item[2]: 202
        Item[3]: 302
        Item[4]: 406
        Item[5]: 534
deleted element is: 101
deleted element is: 202
Circular Queue Items are :
        Item[3]: 302
        Item[4]: 406
        Item[5]: 534
        Item[1]: 786
Press any key to continue . . .

Explanation

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, and then it moves to the 0th location of the array.

In the above program, we created a structure CircularQueue that contains an array of elements, FRONT, REAR, MAX, and COUNT data members.

The CircularQueue structure contains a constructor to initialize data members and InsertItem(), DeleteItem(), and PrintCircularQueue() method.

The InsertItem() method is used to insert an item from the rear end. The DeleteItem() method is used to delete the item from the front end, and the PrintCircularQueue() method is used to print the Queue items on the console screen.

Now look the Demo class that contains the Main() method, The Main() method is the entry point for the program. Here we created the reference of CircularQueue structure and perform insert and delete operations.

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





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