0% found this document useful (0 votes)
31 views5 pages

Unit 6

Uploaded by

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

Unit 6

Uploaded by

shrutinimbekar05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit 6 Queues

1. Basic Concept of Queue


 A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning
the first element added is the first one to be removed.
 Common real-world examples of queues are lines in a bank or a waiting list.
 Queues are widely used in scenarios requiring sequential processing, such as task scheduling
and buffering in operating systems.

2. Queue as an Abstract Data Type (ADT)


 Queue ADT defines the set of operations without specifying the underlying implementation.
 Key operations:
o Enqueue: Adds an item to the rear of the queue.
o Dequeue: Removes an item from the front of the queue.
o Peek/Front: Retrieves the item at the front without removing it.
o IsEmpty: Checks if the queue is empty.
o IsFull (for fixed-size queues): Checks if the queue is full.

3. Representation of Queue Using Sequential Organization


 A queue can be implemented using arrays (sequential representation).
 Array-based queue has limitations, such as needing to shift elements after every dequeue or
growing the array if full (in fixed-size).
 Front and rear pointers are used to track the start and end of the queue.

· 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.

7. Linked Queue and Operations


 A linked queue uses a linked list for its underlying storage, with nodes linked from front to
rear.
 Operations:
o Enqueue: Adds a new node at the rear.
o Dequeue: Removes the node at the front.
o No fixed size constraint as in array-based queues, making it dynamic in size.

8. Deque (Double-Ended Queue)


 A deque allows insertion and deletion from both the front and the rear.

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.

Operations of a Priority Queue:


A typical priority queue supports the following operations:

1) Insertion in a Priority Queue

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.

2) Deletion in a Priority Queue

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.

3) Peek in a Priority Queue

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.

As the name suggests, in ascending order priority queue, the element


with a lower priority value is given a higher priority in the priority list.
For example, if we have the following elements in a priority queue
arranged in ascending order like 4,6,8,9,10. Here, 4 is the smallest
number, therefore, it will get the highest priority in a priority queue and
so when we dequeue from this type of priority queue, 4 will remove from
the queue and dequeue returns 4.

o Descending Priority Queue: Higher values represent higher priorities, so elements


with the largest values are dequeued first.

The root node is the maximum element in a max heap, as you


may know. It will also remove the element with the highest
priority first. As a result, the root node is removed from the
queue. This deletion leaves an empty space, which will be filled
with fresh insertions in the future. The heap invariant is then
maintained by comparing the newly inserted element to all other
entries in the queue.

 Applications include task scheduling, where tasks of higher priority must be executed before
lower-priority tasks.

You might also like