Menu Close

circular queue program in c

Code program of Circular QUEUE:

#include<stdio.h>
#define SIZE 5
int cqueue[SIZE];
int front=-1, rear=-1;
void enqueue(int);
int dequeue(void);
void display(void);
int underflow(void);
int overflow(void);
void main()
{
	int ch,item;
	printf("***Circular Queue Operations***\n");
	while(1)
	{
		printf("1. Insert \n");
		printf("2. Delete \n");
		printf("3. Display \n");
		printf("4. Exit \n");
		printf("Enter your choice : ");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1    :	if(!overflow())
					{
						printf("Enter item to insert : ");
						scanf("%d",&item);
						enqueue(item);
					}
					else
					{
						printf("Queue is Full\n\n");
					}
					break;

			case 2    :	if(!underflow())
					{
						int ele = dequeue();
				   		printf("Deleted : %d\n\n",ele);
					}
					else
					{
						printf("Queue is Empty\n\n");
					}
					break;

			case 3    :	if(!underflow())
					{
						printf("The queue is : \n");
						display();
					}
					else
					{
						printf("No elements to display \n");
					}
					break;

			case 4    :	exit(1);
			default   :	printf("Your choice is wrong \n\n");
		}
	}
}


int underflow()
{
	if((front==-1)&&(rear==-1))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


int overflow()
{
	if(((front==0)&&(rear==SIZE-1))||(front==rear+1))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void enqueue(int item)
{
	if((front==-1)&&(rear==-1))
	{
		front=0;
		rear=0;
	}
	else if(rear==SIZE-1)
	{
		rear=0;
	}
	else
	{
		rear++;
	}
	cqueue[rear]=item;
	printf("Inserted : %d\n\n",item);
}

int dequeue()
{
	int item ;
	item = cqueue[front];
	if(front==rear)
	{
		front=-1;
		rear=-1;
	}
	else if(front==SIZE-1)
	{	
		front=0;
	}
	else
	{
		front=front+1;
	}
	return item ;
}

void display()
{
	int i;
	if(front<=rear)
	{
		for(i=front ; i<=rear ; i++)
		{
			printf("Element %d : %d \n", i+1, cqueue[i]);
		}
	}
	else
	{
		for(i=front ; i<=SIZE-1; i++)
		{
			printf("Element %d : %d \n",i+1,cqueue[i]);
		}
		for(i=0 ; i<=rear ; i++)
		{
			printf("Element %d : %d \n",i+1,cqueue[i]);
		}
	}
}

Note:

  • 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]);
   	 }
}