Unit 6
Unit 6
· A Queue is like a line waiting to purchase tickets, where the first person in line
is the first person served. (i.e. First Come First Serve).
· Position of the entry in a queue ready to be served, that is, the first entry that
will be removed from the queue, is called the front of the
queue(sometimes, head of the queue). Similarly, the position of the last entry in
the queue, that is, the one most recently added, is called the rear (or the tail) of
the queue.
4. Queue Operations
Enqueue Operation: Adds an element at the position indicated by the rear pointer and then
increments the rear pointer.
Inserts an element at the end of the queue i.e. at the rear end.
The following steps should be taken to enqueue (insert) data into a queue:
Check if the queue is full.
If the queue is full, return overflow error and exit.
If the queue is not full, increment the rear pointer to point to the next empty space.
Add the data element to the queue location, where the rear is pointing.
return success.
Dequeue Operation: Removes the element at the position indicated by the front pointer and
then increments the front pointer.
This operation removes and returns an element that is at the front end of the queue.
The following steps are taken to perform the dequeue operation:
Check if the queue is empty.
If the queue is empty, return the underflow error and exit.
If the queue is not empty, access the data where the front is pointing.
Increment the front pointer to point to the next available data element.
The Return success.
Peek Operation: Retrieves the element at the front without removing it.
Size Operation: Determines the current number of elements in the queue.
5. Circular Queue and Its Advantages
In a circular queue, the last position connects back to the first position to form a circle.
Advantages:
o More efficient memory usage, as positions freed up by dequeuing can be reused by
enqueuing.
o Avoids the need to shift elements, unlike a basic linear array implementation.
Key Operations:
o Enqueue and Dequeue with a modulo operation for circular linking.
6. Multi-queues
Multi-queues are multiple queues stored within the same data structure.
Useful in situations requiring multiple separate queues, such as task management for different
priority levels.
Each queue can operate independently or prioritize operations based on certain criteria.
The deque stands for Double Ended Queue. Deque is a linear data structure where the
insertion and deletion operations are performed from both ends. We can say that deque is a
generalized version of the queue.
Types:
o Input-Restricted Deque: Insertion allowed only at one end (usually the rear),
deletion from both.
o Output-Restricted Deque: Deletion allowed only at one end (usually the front),
insertion from both.
Deques are useful in applications requiring flexible insertion/deletion options, such as
implementing undo operations in software.
9. Priority Queue
A priority queue is a type of queue that arranges elements based on their priority
values. Elements with higher priority values are typically retrieved or removed
before elements with lower priority values. Each element has a priority value
associated with it. When we add an item, it is inserted in a position based on its
priority value.
When a new element is inserted in a priority queue, it moves to the empty slot from
top to bottom and left to right. However, if the element is not in the correct place
then it will be compared with the parent node. If the element is not in the correct
order, the elements are swapped. The swapping process continues until all the
elements are placed in the correct position.
As you know that in a max heap, the maximum element is the root node. And it will
remove the element which has maximum priority first. Thus, you remove the root
node from the queue. This removal creates an empty slot, which will be further
filled with new insertion. Then, it compares the newly inserted element with all the
elements inside the queue to maintain the heap invariant.
This operation helps to return the maximum element from Max Heap or the
minimum element from Min Heap without deleting the node from the priority
queue.
In a priority queue, elements are assigned priorities, and elements with higher priority are
processed before those with lower priority.
Does not strictly follow FIFO order if priorities differ.
Types:
o Ascending Priority Queue: Lower values represent higher priorities, so elements
with the smallest values are dequeued first.
Applications include task scheduling, where tasks of higher priority must be executed before
lower-priority tasks.