PROGRAM -06
6.1 Objective: Write a program to implement Circular Queue using array.
6.2 Theory
A Circular Queue is a special version of queue where the last element of the queue is
connected to the first element of the queue forming a circle. The operations are performed
based on FIFO (First In First Out) principle. But instead of ending the queue at the last
position, it again starts from the first position after the last, hence making the queue behave
like a circular data structure. Deletions and insertions can only be performed at front and rear
end respectively just as in queue.
Implementation of a linear queue brings the drawback of memory wastage. When rear pointer
reaches the MaxSize of a queue, there might be a possibility that after a certain number of
dequeue() operations, it will create an empty space at the start of a queue. this newly created
empty space can never be re-utilized as the rear pointer reaches the end of a queue. Hence,
circular queue was introduced to overcome this limitation.
Operations On A Circular Queue:
1. Enqueue- adding an element into the queue.
2. Dequeue- Removing elements from a queue.
3. Front- get the first item from the queue.
4. Rear- get the last item from the queue.
5. isEmpty/isFull- checks if the queue is empty or full.
6.3 Code
#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1, rear = -1;
void insert(int item)
{
if((front = = 0 && rear = = MAX-1) || (front = = rear+1))
23
{
printf("Queue Overflow \n");
return;
}
if(front = = -1)
front = rear = 0;
else
{
if(rear = = MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front = = -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front = = rear)
front = rear = -1;
else
{
if(front = = MAX-1)
front = 0;
else
24
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++;
}
25
}
printf("\n");
}
int main()
{
int choice, item;
do
{
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 : ");
scanf("%d", &item);
insert(item); break;
case 2 :
deletion(); break;
case 3:
display(); break;
case 4: exit(0);
break;
default:
printf("Wrong choice\n");
}
}while(choice!=4);
26
return 0;
}
6.4 Output
Enter your choice : 1
Input the element for insertion in queue : 6
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice : 1
Input the element for insertion in queue : 4
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice : 3
Queue elements
6 4
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice : 2
Element deleted from queue is 6
27