Lab 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Program-6

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
Vandana U Support the program with appropriate functions for each of the
Dept. of AI-DS above operations
About the Experiment

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

Option
deleted at front end of the queue. The queue is considered as a circular queue when the
Option
positions 0 and MAX 1 are adjacent. Any position before front is also after rear. A circular
Option

queue looks like [7]


[0]

Option [1]

Option
[6]

[5] [2]
Option

[4] [3]
Algorithm:
Step 1: Start.
Step 2: Initialize queue size to MAX.
Step 3: Insert the elements into circular
queue. If queue is full give a message as
"queue is overflow”.
Step 4: Delete an element from the circular
queue. If queue is empty give a message as
"queue is underflow‟.
Step 5: Display the contents of the queue.
Step 6: Stop.
while(1)
#include<stdio.h>
{
#include<stdlib.h> printf("Enter your choice:");
scanf("%d",&ch);
#define max 10
switch(ch)
int q[10],front=0,rear= -1; {
case 1:
void main()
insert();
{ break;
case 2:
int ch; Switch
delet();
/*FUNCTION PROTOTYPE */ break; case for
different
void insert(); case 3:
Function declaration for operations
display();
void delet(); insert, delete and
break;
display operations
case 4:
void display();
exit(1);
printf("\nCircular Queue operations\n"); default:
printf("Invalid option\n");
Printf("1.insert\n2.delete\n3.display\n4.exit\n");
}}}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
To check if the queue is FULL
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0) Condition where rear is the last index but front is somewhere in
{ the middle. This means the beginning of the queue is still empty,
rear=0; so set rear =0 so that an element can be inserted.
q[rear]=x;
}
else
{
Under normal condition front and rear = 0 and -1
if((front==0&&rear==-1)||(rear!=front-1))
respectively and when rear!=front –1 , increment
q[++rear]=x; the rear by 1 and insert the element
}}}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
Check whether the queue is empty
printf("Queue is underflow\n");
exit(1);
}
if(front==rear)
If there is only one element in the queue,
{
then after deletion rear is set to -1 and front
a=q[front];
=0
rear=-1;
front=0;
}
else if(front==max-1)
{ Function declaration for insert, delete and
a=q[front]; display operations
front=0;
}
Under normal conditions increase the front
else
by 1 , so that previous index no longer exists
a=q[front++];
and element gets deleted from that position
printf("Deleted element is:%d\n",a);
}
void display() printf("\nrear is at %d\n",q[rear]);
{ printf("\nfront is at %d\n",q[front]);
int i,j; }
if(front==0&&rear==-1) printf("\n");
{ }
printf("Queue is underflow\n");
exit(1); This loop iterates through the elements of the
} queue from the position of front to rear.Also, Inside
if(front>rear) the loop, this printf statement prints the value of the
{ element at index i in the queue array q[].
for(i=0;i<=rear;i++) This loop iterates through the queue array q[]
printf("\t%d",q[i]); starting from the front index up to the last possible
for(j=front;j<=max-1;j++) index, max-1 (since arrays are zero-indexed, max-1 is
printf("\t%d",q[j]); the last valid index).
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]); This prints the value of the element at the rear position in
} the queue after the loop completes.Also,This prints the
else value of the element at the front position in the queue.
{
for(i=front;i<=rear;i++) This loop is part of a queue traversal. It prints or processes each
{ element from the front of the queue to the rear, assuming front
printf("\t%d",q[i]); } <= rear (the normal case without a wrap-around).
Circular Queue operations
1.insert
2.delete
Expected Output 3.display
4.exit
Enter your choice:1
Enter element to be insert:10
Enter your choice:1
Enter element to be insert:20
Enter your choice:1
Enter element to be insert:30
Enter your choice:3
10 20 30
rear is at 30
front is at 10
Enter your choice:2
Deleted element is:10
Enter your choice:3
20 30
rear is at 30
front is at 20
Enter your choice:4
Thank you

You might also like