Data Structures and
Algorithms
DSE2101
Dr.Chirag Joshi
Email Id: [email protected]
Department of Data Science and Engineering, SISDS
Manipal University Jaipur
09/09/20 1
25
Queue
09/09/2025 2
Content
• Queue
• Basic principles
• Operation of queue
• Queue using Array
• Queue using Linked List
• Applications of queue
• Type of Queue
09/09/2025 3
Queue
09/09/2025 4
Basic Idea
• Queue is an abstract data structure, somewhat similar to Stacks.
Unlike stacks, a queue is open at both its ends. One end is always
used to insert data (enqueue) and the other is used to remove data
(dequeue).
09/09/2025 5
Queue Representation
• As in stacks, a queue can also be implemented using Arrays, Linked-
lists, Pointers and Structures.
09/09/2025 6
enqueue
dequeue
create
QUEUE
isempty
size
09/09/2025 7
QUEUE: First-In-First-Out (LIFO)
void enqueue (queue *q, int element);
/* Insert an element in the queue */
int dequeue (queue *q);
/* Remove an element from the queue */
queue *create();
/* Create a new queue */
int isempty (queue *q);
/* Check if queue is empty */
int size (queue *q);
/* Return the no. of elements in queue */
Assumption: queue contains integer elements!
09/09/2025 8
Queue using Linked List
09/09/2025 9
Basic Idea
• Basic idea:
• Create a linked list to which items would be added to one end and
deleted from the other end.
• Two pointers will be maintained:
• One pointing to the beginning of the list (point from where
elements will be deleted).
• Another pointing to the end of the list (point where new
elements will be inserted).
Rear
Front DELETION INSERTION
09/09/2025 10
Queue: Linked List Structure
ENQUEUE
front rear
09/09/2025 11
Queue: Linked List Structure
DEQUEUE
front rear
09/09/2025 12
Example :Queue using Linked List
struct qnode
{
int val;
struct qnode *next;
};
struct queue
{
struct qnode *qfront, *qrear;
};
typedef struct queue QUEUE;
void enqueue (QUEUE *q,int element)
{
struct qnode *q1;
q1=(struct qnode *)malloc(sizeof(struct qnode));
q1->val= element;
q1->next=q->qfront;
q->qfront=q1;
}
09/09/2025 13
Example :Queue using Linked List
int size (queue *q)
{
queue *q1;
int count=0;
q1=q;
while(q1!=NULL) int dequeue (queue *q)
{ {
q1=q1->next; int val;
count++; queue *q1,*prev;
} q1=q;
return count; while(q1->next!=NULL)
} {
prev=q1;
q1=q1->next;
}
val=q1->val;
int peek (queue *q)
prev->next=NULL;
{
free(q1);
queue *q1;
return (val);
q1=q;
}
while(q1->next!=NULL)
q1=q1->next;
return (q1->val);
}
09/09/2025 14
Problem With Array Implementation
• The size of the queue depends on the number and order of enqueue
and dequeue.
• It may be situation where memory is available but enqueue is not
possible.
ENQUEUE DEQUEUE
Effective queuing storage area of array gets reduced.
0 N
front front rear rear
Use of circular array indexing
09/09/2025 15
Applications of Queues
• Direct applications:-
• Waiting lists
• Access to shared resources (e.g., printer)
• Multiprogramming
• Indirect applications:-
• Auxiliary data structure for algorithms
• Component of other data structures
09/09/2025 16
Type of Queue
09/09/2025 17
Input restricted Queue:
• In this type of Queue, the input can be taken from one
side only(rear) and deletion of elements can be done
from both sides(front and rear).
• This kind of Queue does not follow FIFO(first in first
out). This queue is used in cases where the
consumption of the data needs to be in FIFO order but if
there is a need to remove the recently inserted data for
some reason and one such case can be irrelevant data,
performance issue, etc.
09/09/2025 18
Input restricted Queue:
09/09/2025 19
Input restricted Queue:
Advantages of Input restricted Queue:
• Prevents overflow and overloading of the queue by
limiting the number of items added
• Helps maintain stability and predictable performance of
the system
Disadvantages of Input restricted Queue:
• May lead to resource wastage if the restriction is set too
low and items are frequently discarded
• May lead to waiting or blocking if the restriction is set
too high and the queue is full, preventing new items
from being added.
09/09/2025 20
Output restricted Queue
• In this type of Queue, the input can be taken
from both sides(rear and front) and the deletion
of the element can be done from only one
side(front).
• This queue is used in the case where the inputs
have some priority order to be executed, and the
input can be placed even in the first place so
that it is executed first.
09/09/2025 21
Output restricted Queue
09/09/2025 22
Circular Queue
A Circular Queue is an extended version of a
normal queue where the last element of the queue is
connected to the first element of the queue forming
a circle.
09/09/2025 23
Circular Queue
A Circular Queue is an extended version of a
normal queue where the last element of the queue is
connected to the first element of the queue forming
a circle.
09/09/2025 24
Circular Array
• Use a 1D array queue.
queue[]
• Circular view of array.
[2] [3]
[1] [4]
09/09/2025
[0] [5] 25
Circular Array
• Possible configuration with 3 elements.
[2] [3]
A B
[1] C [4]
[0] [5]
09/09/2025 26
Circular Array
• Another possible configuration with 3
elements.
[2] [3]
C
[1] [4]
B A
[0] [5]
09/09/2025 27
Circular Array
• Use integer variables front and rear.
– front is one position counterclockwise from
first element
– rear gives position of last element
[2] [3] [2] [3]
A B
rear rear
front front
C C
[1] [4] [1] [4]
B A
09/09/2025
[0] [5] [0] [5] 28
Add An Element
• Move rear one clockwise.
[2] [3]
A B
rear
front
[1] C [4]
[0] [5]
09/09/2025 29
Add An Element
• Move rear one clockwise.
• Then put into queue[rear].
[2] [3]
A B
front
[1] C [4]
D
[0] [5]
09/09/2025
rear 30
Delete An Element
• Move front one clockwise.
[2] [3]
A B
rear
front
[1] C [4]
[0] [5]
09/09/2025 31
Delete An Element
• Move front one clockwise.
• Then extract from queue[front].
[2] [3]
front
A B
rear
[1] C [4]
[0] [5]
09/09/2025 32
Moving rear Clockwise
• rear++;
if (rear = = capacity) rear = 0;
[2] [3]
A B
front rear
[1] C [4]
[0] [5]
• rear = (rear + 1) % capacity;
09/09/2025 33
Empty That Queue
[2] [3]
rear
front
C
[1] [4]
B A
[0] [5]
09/09/2025 34
Empty That Queue
[2] [3]
rear
C
[1] [4]
B
[0] [5]
front
09/09/2025 35
Empty That Queue
[2] [3]
rear
C
[1] [4]
[0] [5]
front
09/09/2025 36
Empty That Queue
[2] [3]
rear
[1] [4]
front
[0] [5]
• When a series of removes causes the
queue to become empty, front = rear.
• When a queue is constructed, it is
empty.
• So initialize front = rear = 0.
09/09/2025 37
A Full Tank Please
[2] [3]
rear
front
C
[1] [4]
B A
[0] [5]
09/09/2025 38
A Full Tank Please
[2] [3]
rear
D
front
C
[1] [4]
B A
[0] [5]
09/09/2025 39
A Full Tank Please
[2] [3] rear
D E
front
C
[1] [4]
B A
[0] [5]
09/09/2025 40
A Full Tank Please
[2] [3]
D E
front
C F
[1] [4]
B A rear
[0] [5]
• When a series of adds causes the queue to
become full, front = rear.
• So we cannot distinguish between a full
queue and an empty queue!
09/09/2025 41
Remedies.
• Don’t let the queue get full.
• When the addition of an element will cause
the queue to be full, increase array size.
• This is what the text does.
• Define a boolean variable lastOperationIsAddQ.
• Following each AddQ set this variable to true.
• Following each DeleteQ set to false.
• Queue is empty iff (front == rear) && !
lastOperationIsAddQ
• Queue is full iff (front == rear) &&
lastOperationIsAddQ
09/09/2025 42
Remedies (continued).
• Define an integer variable size.
• Following each AddQ do size++.
• Following each DeleteQ do size--.
• Queue is empty iff (size == 0)
• Queue is full iff (size == arrayLength)
• Performance is slightly better when
first strategy is used.
09/09/2025 43
Implement Circular Queue using
Array:
1.Initialize an array queue of size n, where n is the maximum number
of elements that the queue can hold.
2.Initialize two variables front and rear to -1.
3.Enqueue: To enqueue an element x into the queue, do the
following:
1. Increment rear by 1.
1. If rear is equal to n, set rear to 0.
2. If front is -1, set front to 0.
3. Set queue[rear] to x.
4.Dequeue: To dequeue an element from the queue, do the following:
1. Check if the queue is empty by checking if front is -1.
1. If it is, return an error message indicating that the queue is empty.
2. Set x to queue[front].
3. If front is equal to rear, set front and rear to -1.
4. Otherwise, increment front by 1 and if front is equal to n, set front to 0.
5. Return x.
09/09/2025 44
09/09/2025 45
09/09/2025 46
09/09/2025 47
C Program to Implement Circular
Queue using Linked list
09/09/2025 48
C Program to Implement Circular
Queue using Linked list
09/09/2025 49
C Program to Implement Circular
Queue using Linked list
09/09/2025 50
C Program to Implement Circular
Queue using Linked list
09/09/2025 51
09/09/2025 52
Priority Queues
Two kinds of priority queues:
• Min priority queue.
• Max priority queue.
Min Priority Queue
• Collection of elements.
• Each element has a priority or key.
• Supports following operations:
empty
size
insert an element into the priority queue
(push)
get element with min priority (top)
remove element with min priority (pop)
Max Priority Queue
• Collection of elements.
• Each element has a priority or key.
• Supports following operations:
empty
size
insert an element into the priority queue (push)
get element with max priority (top)
remove element with max priority (pop)
Complexity Of Operations
Use a heap or a leftist tree (both
are defined later).
empty, size, and top => O(1) time
insert (push) and remove (pop) =>
O(log n) time where n is the size
of the priority queue
Applications
Sorting
• use element key as priority
• insert elements to be sorted into a priority
queue
• remove/pop elements in priority order
if a min priority queue is used, elements are
extracted in ascending order of priority (or key)
if a max priority queue is used, elements are
extracted in descending order of priority (or
key)
Sorting Example
Sort five elements whose keys are 6, 8,
2, 4, 1 using a max priority queue.
Insert the five elements into a max
priority queue.
Do five remove max operations placing
removed elements into the sorted array
from right to left.
After Inserting Into Max Priority
Queue
8 4 6
1
Max
2 Priority
Queue
Sorted Array
After First Remove Max
Operation
4 6
1
Max
2 Priority
Queue
8
Sorted Array
After Second Remove Max
Operation
4
1
Max
2 Priority
Queue
6 8
Sorted Array
After Third Remove Max
Operation
1
Max
2 Priority
Queue
4 6 8
Sorted Array
After Fourth Remove Max
Operation
1
Max
Priority
Queue
2 4 6 8
Sorted Array
After Fifth Remove Max
Operation
Max
Priority
Queue
1 2 4 6 8
Sorted Array