0% found this document useful (0 votes)
45 views25 pages

It 11200222017 Saideepmukhopadhyay DS Ca2

The document provides an overview of circular queues, highlighting their structure, operations, and advantages over linear queues. It explains the FIFO principle, dynamic resizing, and applications in memory management and process scheduling. Additionally, it includes implementation details and a sample program in C for managing a circular queue using arrays.

Uploaded by

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

It 11200222017 Saideepmukhopadhyay DS Ca2

The document provides an overview of circular queues, highlighting their structure, operations, and advantages over linear queues. It explains the FIFO principle, dynamic resizing, and applications in memory management and process scheduling. Additionally, it includes implementation details and a sample program in C for managing a circular queue using arrays.

Uploaded by

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

GOVERNMEN

T COLLEGE
OF
NAME-SAIDEEP
MUKHOPADHYAY
ENGINEERIN
STREAM- G
INFORMATION AND
TECHNOLOGY
LEATHER
ROLL NO-
11200222017
TECHNOLOG
CUMMULATIVE
Y
ASSESSMENT-2
SUBJECT-DATA STRUCTURE
AND

ALGORITHMS
SUBJECT CODE-PCC-CS301
SEMESTER-3RD
YEAR-2ND YEAR

CIRCULAR QUEUE DATA


STRUCTURE
A data structure called a circular
queue enables cyclical insertion
and removal of components. In a
circular queue, which is connected
back to the first node at the end to
form a circle, components are
added at the back and removed at
the front according to the FIFO
principle.
A circular queue may be
dynamically enlarged, as opposed
to a linear queue, which has a set
size. This increases its adaptability
and effectiveness for certain
applications.
The Double Ended
Queue is a similar data structure
to the Queue in that insertion and
deletion operations are carried out
at both ends (front and rear),
allowing us to insert at both the
front and rear locations and
remove from both the front and
rear positions.

CIRCULAR QUEUE V/S LINEAR


QUEUE
LINEAR QUEUE CIRCULARQUEUE
Arranging the data Arranging
in the
a linear pattern. data
in circular
order where
the rear end is
connected with
the front end.
The insertion and deletion
Insertion and
Operations are fixed deletion
i.e, are not
done at the rear and fixed
frontand it can be
end respectively. done in any
position.
Linear queue It requires less
requires memory space.
more memory space.
In a linear queue, the
The sequence of
element that was actions carried ou
inserted on an element
in the first place will
may
be alter in a circular
the element that isqueue.
removed. Any
operation
on an element is
executed
in a defined
sequence,
or FIFO.
It is inefficient in It is more efficient
Comparison to a in comparison to
circular queue. linear queue.
In a linear queue, weIn a circular
can easily fetch outqueue,
the peek value. we cannot fetch
out
the peek value
easily.
Application- PeopleApplication-
standing for the bus.Computer-
Cars lined on a controlled traffic
bridge. signal
In CPU scheduling
and memory
management.

Good for applications


Good for
where overflow is not
applications
a concern. where efficient
use
of memory is
important.
Can lead to overflow
Rear wraps
if around
the rear reaches the
to the beginning
end of
of the array. the array,
preventing
overflow.

DEFINITION
We can keep adding components
to a queue until it is full in a typical
queue data structure. However,
once the queue is full, we are
unable to add any more items until
all of the existing ones have been
removed.
For example consider the queue
below...
After inserting all the elements
into the queue.
25 30 51 60 85 45 88 90 75 95

FRONT
REAR
Queue is Full
Now consider the following
situation after deleting three
elements from the queue...
Queue is Full (Even three elements
are deleted)
60 85 45 88 90 75 95
FRONT
REAR
Due to the fact that "rear" is still in
the last place, this condition also
indicates that the queue is full and
that we cannot add a new
element. Even though there are
vacant spaces in the queue in the
aforementioned scenario, we are
unable to use them to insert new
elements. The main issue with the
typical queue data structure is
this. We employ a circular queue
data structure to solve this issue.
GRAPHICAL REPRESENTATION
OF
CIRCULAR
QUEUE
APPLICATIONS OF CIRCULAR
QUEUE
 Memory management: circular
queue is used in memory
management.
 Process Scheduling: A CPU uses
a queue to schedule processes.
 Traffic Systems: Queues are also
used in traffic systems.

OPERATIONS OF CIRCULAR
QUEUE
Enqueue- adding an element in the
queue if there is space in the
queue.
Dequeue- Removing elements
from a queue if there are any
elements in the queue
Front- get the first item from the
queue.
Rear- get the last item from the
queue.
isEmpty/isFull- checks if the queue
is empty or full.
Implementation of Circular
Queue
Prior to implementing real
operations, we first complete the
following steps to construct a
circular queue data structure using
an array:-
Step 1:-Including all the header
files which are used in the
program and define a constant
'SIZE' with specific value.
Step 2:- Declaring all user defined
functions used in circular queue
implementation.
Step 3:- Creating a one
dimensional array with above
defined SIZE (int cQueue[SIZE])
Step 4:-Defining two integer
variables ‘front’and ‘rear’and
initialize both with '-1'. (int front =
-1, rear = -1)
Step 5:- Implementing main
method by displaying menu of
operations list and make suitable
function calls to perform operation
selected by the user on circular
queue.
EnQueue(value) - Inserting
value into the
Circular
Queue
In a circular queue, the new
element is always placed at the
rear position. The enQueue()
method accepts one integer value
as an argument and inserts that
value into the circular queue.
Step 1: Checking whether queue is
FULL. ((rear == SIZE-1 && front
== 0) || (front == rear+1)).
Step 2: If it is FULL, then display
"Queue is FULL!!! Insertion is not
possible!!!" and terminate the
function.
Step 3: If it is NOT FULL, then
check rear == SIZE - 1 && front !=
0 if it is TRUE, then set rear = -1.
Step 4: Incrementing rear value by
one (rear++), set queue[rear] =
value and check 'front == -1' if it
is TRUE, then set front = 0.
DeQueue() - Deleting a value
from the Circular
Queue
DeQueue() is a function that may
be used to remove an element
from a circular queue. The element
is always eliminated from the front
position in a circular queue. The
deQueue() function does not
accept an argument with any
value. The procedures listed below
can be used to remove a
component from the circular
queue.
Step 1: Check whether queue is
EMPTY. (front == -1 && rear == -
1) .
Step 2: If it is EMPTY, then display
"Queue is EMPTY!!! Deletion is not
possible!!!" and terminate the
function.
Step 3: 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).
Display() - Displays the
elements of a Circular
Queue
We can use the following steps to
display the elements of a circular
queue
Step 1: Check whether queue is
EMPTY. (front == -1).
Step 2: If it is EMPTY, then display
"Queue is EMPTY!!!" and terminate
the function.
Step 3: If it is NOT EMPTY, then
define an integer variable 'i' and
set 'i = front'.
Step 4: 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 5: 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 6: Set i to 0.
Step 7: Again display 'cQueue[i]'
value and increment i value by one
(i++). Repeat the same until 'i <=
rear' becomes FALSE.
PROGRAM TO IMPLEMENT
CIRCULAR QUEUE USING
ARRAY
#include <stdio.h>
#include <conio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int cQueue[SIZE], front = -1, rear
= -1;
void enQueue(int value) {
if((front == 0 && rear == SIZE -
1) || (front == rear+1)) {
printf("Circular Queue is Full.
Insertion not possible");
} else
{
if(rear == SIZE-1 && front != 0)
rear = -1;
cQueue[++rear] = value;
printf("\nInsertion Success!!!\n");
if(front == -1)
front = 0;
}}
void deQueue()
{
if(front == -1 && rear == -1)
printf("\nCircular Queue is Empty!
Deletion is not
possible!!!\n");
else {
printf("\nDeleted element %d\
n",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}
void display()
{
if(front == -1)
printf("Circular Queue is Empty.");
Else
{
int i = front;
printf("Circular Queue Elements
are");
if(front <= rear)
{
while(i <= rear)
printf("%d",cQueue[i++]);
}
else
{
while(i <= SIZE - 1)
printf("%d", cQueue[i++]);
i = 0;
while(i <= rear)
printf("%d",cQueue[i++]);
}}}

You might also like