DS – Circular Queue Program in Other way

Another Logic of Circular Queue:

  • The circular queue works according to the below two conditions.
  • The SIZE indicates the maximum number of items the queue can consist of.
    • rear = (rear +1 ) % SIZE;
    • front = (front + 1) % SIZE;
  • And the implementation as follows :
#include <stdio.h>
#define SIZE 5
int cqueue[SIZE];
int front=-1, rear=-1;
int isFull(void);
int isEmpty(void);
void enQueue(int);
int deQueue(void);
void display(void);
int main()
{
            deQueue();
            enQueue(10);
            enQueue(20);
            enQueue(30);
            enQueue(40);
            enQueue(50);
            display();
            deQueue();
            deQueue();
            enQueue(60);
            display();
            enQueue(70);
            display();
            return 0;
}
int isFull()
{
            if((front == rear + 1) || (front == 0 && rear == SIZE-1))
                        return 1;
            else
                        return 0;
}
int isEmpty()
{
            if(front == -1)
                        return 1;
            else
                        return 0;
}
void enQueue(int data)
{
            if(isFull())
                        printf(“Queue is Overflow\n”);
            else
            {
                        if(front == -1) front = 0;
                        rear = (rear + 1) % SIZE;
                        cqueue[rear] = data;
                        printf(“Inserted : %d \n”, data);
            }
}
int deQueue()
{
            int data;
            if(isEmpty())
            {
            printf(“Queue is Underflow\n”);
            return(-1);
            }
            else
            {
            data = cqueue[front];
            if (front == rear)
                        {
                                    front = -1;
                        rear = -1;
                        }
                        else
                        {
                                    front=(front + 1)%SIZE;  
            }
            printf(“Deleted data : %d \n”, data);
            return(data);
            }
}
void display()
{
            int i;
            if(isEmpty())
            {
                        printf(“Empty Queue\n”);
            }
            else
            {
            printf(“Elements are : “);
            for( i = front; i!=rear; i=(i+1)%SIZE)
                        {
                                    printf(“%d “,cqueue[i]);
            }
            printf(“%d \n”,cqueue[i]);
             }
}
Scroll to Top