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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment -2.4
Student Name: Abhishek UID: 21BCS3967
Branch: CSE Section/Group: ON21BCS620-A
Semester: 3rd Date of Performance: 20/10/22
Subject Name: Data Structure Subject Code: 21CSH-211

Aim of the practical: Write a menu driven program to implement circular


queue using array.

Objective: To learn and implement circular queue using array

Algorithm:
Step 1 - Start

Step 2 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.

Step 3 - Declare all user defined functions used in circular queue implementation.

Step 4 - Create a one dimensional array with above defined SIZE (int cQueue[SIZE])

Step 5 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int
front = -1, rear = -1)

Step 6 - Implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on circular queue.

Step 7 – Choice is given 1 then enquqe operation will be done

Step 8 - Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front ==
rear+1))

Step 9 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
Step 10 - If it is NOT FULL, then check rear == SIZE - 1 && front != 0 if it is TRUE,
then set rear = -1.

Step 11 - Increment rear value by one (rear++), set queue[rear] = value and check
'front == -1' if it is TRUE, then set front = 0.

Step 12 – Now if choice==2 then deque operation is performed

Step 13 - Check whether queue is EMPTY. (front == -1 && rear == -1)

Step 14 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not


possible!!!" and terminate the function.

Step 15 - If it is NOT EMPTY, then display queue[front] as deleted element and


increment the front value by one (front ++). Then check whether front == SIZE, if it
is TRUE, then set front = 0. Then check whether both front - 1 and rear are equal (front
-1 == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).

Step 16 – if choice==3 then display operation will be performed

Step 17- Check whether queue is EMPTY. (front == -1)

Step 18 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the


function.

Step 19 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.

Step 20 - Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and
increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.

Step 21 - If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value
by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.

Step 22 - Set i to 0.

Step 23 - Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the
same until 'i <= rear' becomes FALSE.

Step 24 -if we want to quit enter choice==4 to quit

Step 25 -Stop

Program code:
#include <stdio.h>

# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration
printf("Name-Abhishek\nUID-21BCS3967\n");
while(choice<4 && choice!=0) // while loop
{
printf("\nPress 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nPress 4: Quit");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
case 4:
break;
default:
printf("Wrong choice\n");

}
return 0;
}
Output:

Learning Outcomes:
1.Arranges the data in a circular order where the rear end is connected with the
front end.

2.Insertion and deletion are not fixed and it can be done in any position.

3.In the case of circular queue, the order of operations performed on an element
may change.

You might also like