Lab Program 6.Docx
Lab Program 6.Docx
Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last
node is connected back to the first node to make a circle. Circular linked list fallow the First In
First Out principle. Elements are added at the rear end and the elements are deleted at front end
of the queue. The queue is considered as a circular queue when the positions 0 and MAX-1 are
adjacent. Any position before front is also after rear.
PROGRAM CODE :
#include <stdio.h>
#include <stdlib.h>
#define size 5
char queue[size];
void insertq( char);
void deleteq( );
void display( );
int front = - 1;
int rear = - 1;
int main()
{
int choice;
char item,ch;
while(1)
{
printf("\n\n Circular Queue Operations\n");
printf(" 1. Insert \n 2. Delete \n 3. Display\n 4. Exit");
printf(" Enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
ch=getchar();
printf("enter the item to be inserted\n");
scanf("%c",&item);
insertq(item);
break;
case 2:
deleteq( );
break;
case 3:
display( );
break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
}
}
void display( )
{
int i;
printf("\n");
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%c\t", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%c\t", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%c\t", queue[i]);
}
}
void deleteq( )
{
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)// only one element in queue
{
printf("%c deleted\n", queue[front]);
front = - 1;
rear = - 1;
}
else
if(front> rear&& front==size-1)
{
}
else { printf("%c deleted\n", queue[front]);
front++;
}
}