Write a C Program to Implementation of Circular Queue

Aim:

Write a C Program to Implementation of Circular Queue.

Algorithm:

  • Step 1: IF (REAR+1)%MAX = FRONT.
    • Write “OVERFLOW ” Go to step 4. [End OF IF]
  • Step 2: IF FRONT = -1 and REAR = -1.
    • SET FRONT = REAR = 0.
    • ELSE IF REAR=MAX – 1 and FRONT !=SET REAR =0.
    • ELSE. SET REAR = (REAR + 1) % MAX. [END OF IF]
  • Step 3: SET QUEUE[REAR] = VAL.
  • Step 4: EXIT

Program:

#include<stdio.h>
#define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1)  
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)	
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear) 
{
front = -1;
rear=-1;
}
else
{	
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
int main()
{
int choice,item,i;
i=0;
while (i<1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Input the element for insertion in queue : \n");
scanf("%d", &item);
insert(item);
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
i=1;
break;
default:
printf("Wrong choice\n");
}
}
return 0;
}

Execution:

Input:
1 56 1 43 1 67 1 32 1 78 3 2 1 106 3 4


Output:

1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Input the element for insertion in queue : 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Input the element for insertion in queue : 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Input the element for insertion in queue : 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Input the element for insertion in queue : 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Input the element for insertion in queue : 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Queue elements :
56 43 67 32 78 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Element deleted from queue is : 56
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Input the element for insertion in queue : 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : Queue elements :
43 67 32 78 106 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 

Result:

Thus, Implement of the Circular Queue was executed successfully.

Leave a Comment