Data Structures and Algorithms
BCA 3rd Semester
Unit-3
Morgan International College
Laxman Bhandari
MSc.CSIT , CDCSIT-TU
03/17/2024 By:Laxman Bhandari 1
Unit 3: Queue (3 hrs.)
3.1 Introduction, Queue as an ADT, Primitive
operations in Queue
3.2 Linear and circular queue and their applications,
enqueue and dequeue, priority queue
03/17/2024 By:Laxman Bhandari 2
Contents
• Basic Concept of Queue
• Queue as an ADT
• Primitive Operations in Queue
• Types of Queue:
– Linear Queue
– Circular Queue
– Priority Queue
• Queue Applications
03/17/2024 By:Laxman Bhandari 2 3
Introduction
• A queue is a linear data structure in which elements can be inserted from
one end called rear, and the elements can be deleted from the other end
called the front as shown in figure below.
Front
1 2 3
Rear
Figure: Queue
• The queue data structure follows the FIFO (First
In First Out) principle,
i.e. the element inserted at first, is the first element to be removed from the
queue.
03/17/2024 By:Laxman Bhandari 3 4
Contd…
• Real life examples of Queue: A real-world example of queue can
be a single-lane one-way road, where the vehicle enters first, exits
first.
03/17/2024 By:Laxman Bhandari 5
Contd…
• More real-world examples can be seen as queues at the ticket windows and
bus-stops.
03/17/2024 By:Laxman Bhandari 6
Contd…
• Basic features of Queue:
– Like stack, queue is also an ordered list of elements of similar data
types.
– Queue is a FIFO( First in First Out ) data structure.
– It has two pointers Front and Rear to point the head and tail of a
queue.
– Insertion operation can be done at the rear and deletion operation can
be done at front.
– Insertion in queue is also know as Enqueue operation. In Enqueue
operation rear pointer is incremented
– deletion in queue is also know as Dequeue operation. In
Dequeue
03/17/2024
operation front pointer is By:Laxman
incremented.
Bhandari 7
Contd…
• Enqueue:
03/17/2024 By:Laxman Bhandari 8
Contd…
• Dequeue:
03/17/2024 By:Laxman Bhandari 9
Contd…
• Real life example of Enqueue and Dequeue:
03/17/2024 By:Laxman Bhandari 10
Contd…
• Applications of Queues :
Queue is used when things have to be processed in First In First Out order.
Like:
– Queues are widely used as waiting lists for a single shared resource like printer, disk,
CPU.
– Queues are used to transfer data asynchronously (data not necessarily received at
same rate as sent) between two processes, e.g., file IO.
– Queues are used in operating system for handling interrupts if the interrupts have
to be handled in the order of arrival.When programming a real-time system that can
be interrupted, for example, by a mouse click, it is necessary to process the interrupts
immediately, before proceeding with the current job. If the interrupts have to be
handled in the order of arrival, then a FIFO queue is the appropriate data structure.
Queues are used as buffers on MP3 players and portable CD players, iPod
playlist. Queues are also used in Playlist for jukebox to add songs to the end, play from the
front of the List.
03/17/2024 By:Laxman Bhandari 11
The queue as an ADT
• A queue q of type T is a finite sequence of elements with the operations.
– MakeEmpty(q): To make q as an empty queue.
– IsEmpty(q): To check whether the queue q is empty. Return true if q is empty,
return false otherwise.
– IsFull(q): To check whether the queue q is full. Return true if q is full, return
false otherwise.
– Enqueue(q, x): To Insert an item x at the rear of the queue, if and only if q is
not full.
– Dequeue( q): To delete an item from the front of the queue q if and only if
q
is not empty.
– Traverse(q): To read entire queue that is display the content of the queue.
03/17/2024 By:Laxman Bhandari 12
Primitive Operations in Queue
• Queues can be represented using arrays with front and rear variables to
point the position from where deletions and insertions can be done,
respectively.
• Initially, the value of front and rear is -1 which represents an empty queue.
• Array representation of a queue containing 6 elements along with the
respective values of front and rear, is shown in the following figure.
Front =0 Rear = 5
12 9 7 18 14 36
Figure: Array representation of
queue
03/17/2024 By:Laxman Bhandari 13
Contd…
• Enqueue operation: To add new element in the queue as:
– Check if the queue is already full by comparing rear to max - 1.
if so, then return an overflow Message.
– If the item is to be inserted as the first element in the queue, in that
case set the value of front and rear to 0 and insert the element at
the rear end.
– Otherwise keep increasing the value of rear and insert each
element one by one having rear as the index.
03/17/2024 By:Laxman Bhandari 14
Contd…
• Algorithm for Enqueue operation:
– Step 1: IF REAR = MAX-1
Write OVERFLOW
Goto step 4 [END OF IF]
– Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR =0 ELSE
SET REAR = REAR + 1 [END OF IF]
– Step 3: SET QUEUE[REAR] = NUM
– Step 4: EXIT
03/17/2024 By:Laxman Bhandari 15
Contd…
• Example: Inserting new element 45 in the queue.
Front =0 Rear = 5
12 9 7 18 14 36
Fig: Queue before inserting new element
Front =0 Rear = 6
12 9 7 18 14 36 45
Fig: Queue after inserting new element
03/17/2024 By:Laxman Bhandari 16
Contd…
• Dequeue operation: to delete an element form the queue as:
– If, the value of front is -1 or value of front is greater than rear , write
an underflow message and exit.
– Otherwise, keep increasing the value of front and return the item
stored at the front end of the queue at each time.
• Algorithm for Dequeue operation:
– Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL =
QUEUE[FRONT]
SET FRONT =
FRONT + 1
[END OF IF]
– Step 2: EXIT
03/17/2024 By:Laxman Bhandari 17
Contd…
• Example: Deleting element from the queue.
Front =0 Rear = 6
12 9 7 18 14 36 45
Fig: Queue before deletion of an element
Front =1 Rear = 6
9 7 18 14 36 45
Fig: Queue after deletion of an element
03/17/2024 By:Laxman Bhandari 18
Write a program to implement a linear queue.
03/17/2024 By:Laxman Bhandari 19
Contd…
03/17/2024 By:Laxman Bhandari 20
Contd…
03/17/2024 By:Laxman Bhandari 21
Contd…
03/17/2024 By:Laxman Bhandari 22
Contd…
03/17/2024 By:Laxman Bhandari 23
Contd…
• Sample run and output:
03/17/2024 By:Laxman Bhandari 24
Types of Queue
• A queue data structure can be classified into the following types:
– Linear Queue
– Circular Queue
– Priority Queue
03/17/2024 By:Laxman Bhandari 25
Circular queue
• Problem in linear queue: Consider a linear queue as shown in figure
below:
Front =2 Rear = 9
7 18 14 36 45 34 20 80
• Suppose we want to insert a new element in the queue shown in above
figure. Even though there is space available, the overflow condition still
exists because the condition rear = MAX – 1 still holds true.
• This is a major drawback of a linear queue , although we have
space
available in the array, but we can not insert any more element in the queue.
• This is simply the memory wastage and we need to overcome this problem.
• The best solution to this problem in linear queue is circular queue.
03/17/2024 By:Laxman Bhandari 26
Contd…
• In the circular queue, Conceptually the first index comes right after the last
index as shown in figure below.
Figure: Circular queue
• Circular queue will be full when front = 0 and rear = max-1.
03/17/2024 By:Laxman Bhandari 27
Contd…
• Enqueue operation in circular queue: Algorithm to insert
an element in a circular queue is as follows:
03/17/2024 By:Laxman Bhandari 28
Contd…
• Example: In the following queue Front =0 and Rear = Max-1 is true
so
overflow message is displayed for Enqueue operation on this
queue.
Front =0 Rear = 9
90 20 7 18 14 36 45 34 20 80
• In the following queue Rear!=Max-1 so increment rear= 8 by one so
Rear becomes Rear=9 and then perform insertion on
new Rear.
Front =0 Rear = 8
90 20 7 18 14 36 45 34 20
03/17/2024 By:Laxman Bhandari 29
Contd…
• In the following queue Front!=0 and Rear= Max-1 is true so, queue is
not full . In such case set Rear=0 andthen insert element at there.
Front =2 Rear = 9
7 18 14 36 45 34 20 90
03/17/2024 By:Laxman Bhandari 30
Contd…
• Dequeue Operation in circular queue:
03/17/2024 By:Laxman Bhandari 31
Contd…
• Example: If Front =-1 then queue is empty as shown in figure below.
So, in such case return Underflow message.
Front =Rear=-1
• If the queue is not empty and front = rear, then after deleting the element
at
• the front the queue becomes empty and so front and rear are set to –1.
• Front =Rear = 9
90
03/17/2024 By:Laxman Bhandari 32
Contd…
• If the queue is not empty and front = MAX–1, then after deleting
the
element at the front, front is set to 0.
Rear = 5 Front =9
80 70 7 18 14 36 90
03/17/2024 By:Laxman Bhandari 33
Write a program to implement a circular queue.
03/17/2024 By:Laxman Bhandari 34
Contd…
03/17/2024 By:Laxman Bhandari 35
Contd…
03/17/2024 By:Laxman Bhandari 36
Contd…
03/17/2024 By:Laxman Bhandari 37
Contd…
03/17/2024 By:Laxman Bhandari 38
Contd…
03/17/2024 By:Laxman Bhandari 39
Contd…
• Sample Run and Output:
03/17/2024 By:Laxman Bhandari 40
Priority Queue
• A priority queue is a data structure in which each element is assigned a
priority. The priority of the element will be used to determine the order in
which the elements will be processed.
• The general rules of processing the elements of a priority queue are
– An element with higher priority is processed before an element with a
lower priority.
– Two elements with the same priority are processed on a first-come-
first-served (FCFS) basis.
• Priority queues are widely used in operating systems to execute the highest
priority process first ( e.g. Shortest job first job scheduling).
03/17/2024 By:Laxman Bhandari 41
Contd…
• Efficient implementation of priority queue is by using linked list:
• In linked list implementation of priority queue every node of the list will
have three parts:
– The information or data part
– The priority number of the element, and
– The address of the next element.
• If we are using a sorted linked list, then the element with the higher priority
will precede the element with the lower priority. when two elements have
the same priority the elements are arranged and processed on FCFS
principle. Lower priority number means higher priority value.
03/17/2024 By:Laxman Bhandari 42
Contd…
• Enqueue operation in priority queue:
– When a new element has to be inserted in a priority queue, we have to
traverse the entire list until we find a node that has a priority lower than
that of the new element.
– The new node is inserted before the node with the lower priority.
However, if there exists an element that has the same priority as the
new element, the new element is inserted after that element.
– Example: if we have a new element with data = F and priority number = 2, then
the element will be inserted after B, as both these elements have the same
priority but the insertions are done on FCFS basis as shown in fig.
03/17/2024 By:Laxman Bhandari 43
Contd…
• Dequeue operation in priority queue:
– Deletion is a very simple process in this case. The first
node of the list will be deleted and the data of that node
will be processed first.
03/17/2024 By:Laxman Bhandari 44
Write a program to implement a priority queue.
03/17/2024 By:Laxman Bhandari 45
Contd…
03/17/2024 By:Laxman Bhandari 46
Contd…
03/17/2024 By:Laxman Bhandari 47
Contd…
03/17/2024 By:Laxman Bhandari 48
Contd…
• Sample Run and output:
03/17/2024 By:Laxman Bhandari 49
Assignment-3
• Write C function to display all the items in a circular queue in array
implementation. Write assumptions you need.
• Define stack. How is it different from queue? Write a program to
implement circular queue.
• What is circular queue? Write an algorithm and C function to implement Circular
queue. Write assumptions, you need. ? How is it better than a linear queue?
• Define Queue as an ADT. Write a program for basic operations in Linear
queue in array implementation.
• What is priority queue? How it is best implemented? Explain with suitable example
program.
• Explain the Enqueue and Dequeue operations on linear queue.
• How linear queue is differ from circular queue?
03/17/2024 By:Laxman Bhandari 50
Lab -3
• Write a program to implement a linear queue using array.
• Write a program to implement a circular queue using array.
• Write a program to implement priority queue using linked list.
03/17/2024 By:Laxman Bhandari 51
Thank You
03/17/2024 By:Laxman Bhandari 52