dsa3(1)
dsa3(1)
PROGRAM
Introduction
In this lab, we will implement a circular queue using an array and perform various operations
such as enqueue, dequeue, display, and peek. A circular queue is an advanced form of a linear
queue that efficiently utilizes memory by allowing wrap-around insertion and deletion. The
program will be menu-driven, allowing the user to interactively perform these operations.
THEORY
A Circular Queue is another way of implementing a normal 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 the FIFO (First In First Out) principle. It is also called ‘Ring Buffer’. In a
normal Queue, we can insert elements until the queue becomes full. However once the queue
becomes full, we cannot insert the next element even if there is a space in front of the queue.
The operations which are performed in circular queue is as follows:
1. Enqueue Operation
Algorithm ENQUEUE(queue, front, rear, MAX_SIZE, value)
1. If (rear + 1) % MAX_SIZE == front then
Print "Queue is Full! Overflow."
Return
2. If front == -1 then
Set front = 0
Set rear = 0
3. Else
Set rear = (rear + 1) % MAX_SIZE
4. Set queue[rear] =value
5. Print "Enqueued:", value
6. Return
2. Dequeue Operation
Algorithm DEQUEUE:
1. If front == -1 then
Print "Queue is Empty! Underflow."
Return
2. Set value = queue[front]
3. If front == rear then
Set front = -1
Set rear = -1
4. Else
Set front = (front + 1) % MAX_SIZE
5. Print "Dequeued:", value
6. Return value
TASK PERFORMED
Write a program to implement circular queue using array in c programming language.
SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
int i, front=-1, rear=-1, value, queue[MAXSIZE],a;
void menu(){ //Function to display menu
printf("The operations and there number is as follows:\n");
printf("1) Create a empty Queue\n");
printf("2) Enqueue\n");
printf("3) Dequeue\n");
printf("4) Display\n");
printf("5) Displayfront\n");
printf("6) Exit\n");
printf("The menu function is called\n");
}
int isFull(){ //Function to check if queue is full
if((rear+1)%MAXSIZE==front)
return 1;
else
return 0;
}
int isEmpty(){ //Function to check if queue is empty
if(front==-1)
return 1;
else
return 0;
}
void create(){ //Function to create empty queue
front=rear=-1;
printf("Empty queue created\n");
}
void enqueue(){ //Function to enqueue value
if(isFull())
printf("Queue is full\n");
else{
if(front==-1) //First element in the queue
front=0;
rear=(rear+1)%MAXSIZE;
printf("Enter value to to enqueue: ");
scanf("%d",& queue[rear]);
}
}
void dequeue(){ //Function to dequeue value
if(isEmpty())
printf("Queue is empty\n");
else{
int value=queue[front];
if(front==rear) //Single element check
front=rear=-1;
else
front=(front+1)%MAXSIZE;
printf("Dequeued %d from queue\n", value);
}
}
void display(){ //Function to display circular queue
if(isEmpty()){
printf("Queue is empty\n");
}
else{
printf("The data in queue is: ");
i=front;
while(i!=rear){
printf("%d\t", queue[i]);
i=(i+1)%MAXSIZE;
}
printf("%d\n",queue[rear]);
}
}
void displayfront(){ //Function to display front value of queue
if(isEmpty())
printf("Queue is empty\n");
else{
printf("Front value of queue: %d\n",queue[front]);
}
}
int main(){
while(1){
menu();
printf("Enter your choice: ");
scanf("%d",&a);
switch(a){
case 1:
create();
break;
case 2:
enqueue();
break;
case 3:
dequeue();
break;
case 4:
display();
break;
case 5:
displayfront();
break;
case 6:
printf(" ! Thank you !\n");
exit(0);
break;
default:
printf("Invalid choice\n");
}}
return 0;
}
OUTPUT
Menu
Front
Invalid choice
DISCUSSION
Firstly, for this we will declare all the variables required to Along with global variables for
queue, front, maxsize, rear which can be retrieve by all the functions. In queue
implementation we required function to perform operation here we will discuss about all the
functions and as well as operation:
Menu() function is being used to display the menu through which user can see the menu
again and again until the program is executed fully. Firstly we will create_new_queue() by
setting the value of front and rear = -1. After this we have a function to check whether my
queue is full or not if the rear value reaches up to (rear+1) %MAXSIZE==front and after that
we cannot insert data so it shows overflow and similarly we have a function for checking if
queue isempty() function. If the front ==-1because after we reach at a condition in which we
will have single element in dequeue we will set front and rear = -1 so we can continue from
front while enquing. isfull() is used in enqueue while another is being used in dequeue.
After this we will perform enqueue and dequeue operation. For enqueue we will check if
queue is full but if it is not then we will perform (rear+1) %MAXSIZE then we will ask user
to enqueue the data into queue it can be continue until my queue is full. Similarly, for
dequeue operation we will check whether the queue is empty or not if it is then we will
display that queue is empty otherwise we will display the value of front and perform
front=(front+1) %MAXSIZE.And then we will display the removed data.
After this we will display the data up to the rear value and it will start from front index of
queue to rear. I have created a function called which will just display the data in the front or
we can say queue[front]. And we have a default statement to display if user inputs invalid
value. We have enclosed this by while and switch operation to continue all the time.
CONCLUSION
Through this lab, we were able to know the use of circular queue. The circular queue acts like
a loop because we can remove and add data according to max size but no any place will be
empty since, we rotated in a circular way.
Through this lab, we gained a deeper understanding of circular queues and their efficient
memory utilization. Unlike linear queues, the circular queue optimally utilizes available
space by wrapping around when elements are dequeued. Implementing this using array and a
menu-driven approach helped reinforce key concepts like front and rear pointers, overflow,
and underflow conditions. By handling these cases effectively, we improved our problem-
solving skills and learned how circular queues can be applied in real-world scenarios such as
scheduling and buffering.