0% found this document useful (0 votes)
79 views9 pages

Experiment No-S05

Uploaded by

pranilmali1131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views9 pages

Experiment No-S05

Uploaded by

pranilmali1131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment No.

:05

Title : Program to implement circular queue using arrays.


Objective : To study Circular Queue representations using Array.
To study Circular Queue operations, insert & delete.
To implement Queue operations using Array
.

A] Theory:
• The queues are implemented in a circular form rather than in a straight line.
• Circular queues overcome the problem of unutilized space in linear queue implemented as an array.
• In the array implementation there is a possibility that the queue is reported full even though slots of the
queue are empty.
• A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out) principle
except that the last position is connected to the first position in a circular queue that forms a circle.
• It is also known as a Ring Buffer

• The rear is at the last position of the Queue and front is pointing somewhere rather than the 0th position.
In the above array, there are only two elements and other three positions are empty. The rear is at the
last position of the Queue; if we try to insert the element then it will show that there are no empty
spaces in the Queue. There is one solution to avoid such wastage of memory space by shifting both
the elements at the left and adjust the front and rear end accordingly.
• It is not a practically good approach because shifting all the elements will consume lots of time.
• The efficient approach to avoid the wastage of the memory is to use the circular queue data structure.

Operations on Circular Queue:

The following are the operations that can be performed on a circular queue:
• enQueue(value): This function is used to insert the new value in the Queue. The new element is
always inserted from the rear end.
• deQueue(): This function deletes an element from the Queue. The deletion in a Queue always takes
place from the front end.
• Front: It is used to get the front element from the Queue.
• Rear: It is used to get the rear element from the Queue.

Enqueue operation:
The steps of enqueue operation are given below:
• First, we will check whether the Queue is full or not.
• Initially the front and rear are set to -1. When we insert the first element in a Queue, front and rear
both are set to 0.
• When we insert a new element, the rear gets incremented, i.e., rear=rear+1.

Scenarios for inserting an element:


There are two scenarios in which queue is not full:
• If rear != max - 1, then rear will be incremented to mod(maxsize) and the new value will be inserted
at the rear end of the queue.
• If front != 0 and rear = max - 1, it means that queue is not full, then set the value of rear to 0 and
insert the new element there.

There are two cases in which the element cannot be inserted:


• When front ==0 && rear = max-1, which means that front is at the first position of the Queue and
rear is at the last position of the Queue.
• front== rear + 1;

Dequeue Operation
The steps of dequeue operation are given below:
• First, we check whether the Queue is empty or not. If the queue is empty, we cannot perform the
dequeue operation.
• When the element is deleted, the value of front gets decremented by 1.
• If there is only one element left which is to be deleted, then the front and rear are reset to -1.
Applications of Circular Queue:

The circular Queue can be used in the following scenarios:

o Memory management: The circular queue provides memory management. As we have already seen
that in linear queue, the memory is not managed very efficiently. But in case of a circular queue, the
memory is managed efficiently by placing the elements in a location which is unused.
o CPU Scheduling: The operating system also uses the circular queue to insert the processes and then
execute them.
o Traffic system: In a computer-control traffic system, traffic light is one of the best examples of the
circular queue. Each light of traffic light gets ON one by one after every jnterval of time. Like red light
gets ON for one minute then yellow light for one minute and then green light. After green light, the
red light gets ON.

B] Algorithm:
Algorithm to insert an element in a circular queue

Step 1:
IF (REAR+1)%MAX = FRONT
Write " OVERFLOW "
Goto step 4
[End OF IF]

Step 2:
IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF
REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]

Step 3: SET QUEUE[REAR] = VAL

Step 4: EXIT
Algorithm to delete an element from the circular queue

Step 1:

IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2:

SET VAL = QUEUE[FRONT]

Step 3:

IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT

C] Instructions:
1. Read above algorithm carefully.
2. Implement the program using algorithms.
Experiment No.:05
Title : Program to implement circular queue using arrays.

Program to implement Circular Queue using Array

#include <stdio.h>

#define MAX 6

int queue[MAX]; // Array declaration

int front = -1; // Front of the queue

int rear = -1; // Rear of the queue

// Function declarations

void enqueue(int element);

int dequeue();

void display();

int main()

int choice = 1, x; // Variables declaration

while (choice != 0) // Menu-driven loop

printf("\nPress 1: Insert an element");

printf("\nPress 2: Delete an element");

printf("\nPress 3: Display the elements");

printf("\nPress 0: Exit");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice)

{
case 1:

printf("Enter the element to be inserted: ");

scanf("%d", &x);

enqueue(x);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 0:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice, please choose again.\n");

return 0;

// Function to insert an element in a circular queue

void enqueue(int element)

if ((rear + 1) % MAX == front) // Condition to check if the queue is full

printf("Queue is full (Overflow).\n");

else
{

if (front == -1) // Queue is empty

front = 0;

rear = (rear + 1) % MAX; // Circular increment

queue[rear] = element;

printf("Inserted %d\n", element);

// Function to delete an element from the queue

int dequeue()

if (front == -1) // Queue is empty

printf("Queue is empty (Underflow).\n");

return -1;

else

int element = queue[front];

if (front == rear) // Queue has only one element

front = rear = -1; // Reset the queue

else

front = (front + 1) % MAX; // Circular increment

}
printf("Dequeued element: %d\n", element);

return element;

// Function to display the elements of a queue

void display()

if (front == -1) // Queue is empty

printf("Queue is empty.\n");

else

printf("Elements in the queue are: ");

int i = front;

while (1)

printf("%d ", queue[i]);

if (i == rear) // Stop when the rear is reached

break;

i = (i + 1) % MAX; // Circular increment

printf("\n");

}
Output :-

D] Conclusion:
In this experiment, we implemented a circular queue using an array in C, which allowed efficient insertion
and deletion operations by utilizing the modulo operator for circular indexing. The key operations enqueue,
dequeue , and display were successfully implemented to handle various edge cases such as queue overflow
and underflow.

You might also like