ADVERTISEMENT
ADVERTISEMENT

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

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.

Program:

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.