Module 8 - Basic ADTS (Queue Data Structures)
Module 8 - Basic ADTS (Queue Data Structures)
Module 8 - Basic ADTS (Queue Data Structures)
8.1 Queue
A Queue is an abstract data structure and a linear list of elements in which
deletion can take place only at one end, called the Front, and insertions can
take place only at the other end, called the Rear. The term “front” and “rear”
are used in describing a linear list only when it is implemented as a queue.
Queue is also called first-in-first-out (FIFO) lists since the first element in a
queue will be the first element out of the queue. In other words, the order in
which elements enters a queue is the order in which they leave. This contrasts
with stacks, which are last-in first-out (LIFO) lists.
Queue can also be defined as the list or collection in which the insertion is
done from one end known as the rear end or the tail of the queue, whereas
the deletion is done from another end known as the front end or the head of
the queue.
The major drawback of using a Linear Queue is that insertion is done only
from the rear end. If the first three elements are deleted from the Queue, we
cannot insert more elements even though the space is available in a Linear
Queue. In this case, the linear Queue shows the overflow condition as the rear
is pointing to the last element of the Queue.
Insertion in priority queue takes place based on the arrival, while deletion in
the priority queue occurs based on the priority. Priority queue is mainly used
to implement the CPU scheduling algorithms.
The above figure shows the queue of characters forming the English word
"HELLO". Since, no deletion is performed in the queue till now, therefore the
value of front remains -1. However, the value of rear increases by one every
time an insertion is performed in the queue. After inserting an element into
the queue shown in the above figure, the queue will look like Figure below.
The value of rear will become 5 while the value of front remains same.
After deleting an element, the value of front will increase from -1 to 0 and the
queue will look something like Figure below.
(a) Enqueue: The Enqueue operation is used to insert the element at the
rear end of the queue. It returns void.
8.5 Deque
Deque or Double-ended Queue (pronounced as ‘deck’ or ‘dequeue’) is
abstract data structure in which insertion and deletion can be done from both
ends of the queue either from the front or rear. It means that we can insert
and delete elements from both front and rear ends of the queue. Deque can
Deque can be used both as stack and queue as it allows the insertion and
deletion operations on both ends. Deque can be considered as stack because
stack follows the LIFO (Last In First Out) principle in which insertion and
deletion can be performed only from one end. And in deque, it is possible to
perform both insertion and deletion from one end, and Deque does not follow
the FIFO principle. The representation of the deque is shown in the Figure
below:
The peek () operations can be performed along side along with the operations
listed above. Through peek operation, we can get the deque's front and rear
elements of the deque. So, in addition to the above operations, the following
operations are also supported in deque:
▪ If the queue is empty, both rear and front are initialized with 0. Now,
both will point to the first element.
▪ Otherwise, check the position of the front if the front is less than 1 (front
< 1), then reinitialize it by front = n - 1, i.e., the last index of the array.
The diagram below shows insertion at the front of Dequeue take places.
Insertion_at_front()
[ The algorithm insert an item at the front of Dequeue
Step-3: return
▪ If the deque has only one element, set rear = -1 and front = -1.
▪ Else if front is at end (that means front = size - 1), set front = 0.
▪ Else increment the front by 1, (i.e., front = front + 1).
Deletion_at_Rear ()
[ The algorithm delete an item at the rear of Dequeue]
A Priority queue is a collection of elements such that each element has been
assigned a priority and such that the order in which elements arc deleted and
processed comes from the following rules:
(a) An element of higher priority is processed before any element of
lower priority.
(b) Two elements with the same priority arc processed according to the
order in which they were added to the queue.
The priority queue supports only comparable elements, which means that the
elements are either arranged in an ascending or descending order.