C# program to implement circular queue using array

In this C# program, we will learn about circular queue implementation. Here, we are implementing circular queue using array.
Submitted by IncludeHelp, on November 21, 2017 [Last updated : March 27, 2023]

Circular Queue Using Array

Circular Queue follows FIFO (First In First Out) property, it means first inserted elements, deleted first. In Circular queue there are two pointers are used:

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

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

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

Circular queue implementation using array in C#

using System;

namespace ConsoleApplication1
{
    class CircularQueue
    {
        private int []ele;
        private int front;
        private int rear;
        private int max;
        private int count;

        public CircularQueue(int size)
        {
            ele = new int[size];
            front = 0 ;
            rear  = -1;
            max   = size;
            count = 0;
        }

        public void insert(int item)
        {
            if (count == max)
            {
                Console.WriteLine("Queue Overflow");
                return;
            }
            else
            {
                rear = (rear + 1) % max;
                ele[rear] = item;

                count++;
            }
        }

        public void delete()
        { 
            if(count == 0)
            {
                Console.WriteLine("Queue is Empty");
            }
            else
            {
                Console.WriteLine("deleted element is: " + ele[front]);

                front = (front + 1) % max;

                count--;
            }
        }

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

            if (count==0)
            {
                Console.WriteLine("Queue is Empty");
                return;
            }
            else
            {
                for (i = front;j<count;)
                {
                    Console.WriteLine("Item[" + (i + 1) + "]: " + ele[i]);

                    i = (i+1)%max;
                    j++;
                    
                }
            }
        }
    }

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

            Q.insert(10);
            Q.insert(20);
            Q.insert(30);
            Q.insert(40);
            Q.insert(50);

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

            Q.insert(60);
            Q.insert(70);

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

        }
    }
}

Output

Items are :
Item[1]: 10
Item[2]: 20
Item[3]: 30
Item[4]: 40
Item[5]: 50
deleted element is: 10
deleted element is: 20
Items are :
Item[3]: 30
Item[4]: 40
Item[5]: 50
Item[1]: 60
Item[2]: 70

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





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