(5) Queues
Queues
Objectives
Introduce the main concepts of the queue.
Examine various queue operations.
Learn how to implement a queue as an array.
Learn how to implement a queue as a linked list.
Discover queue applications.
Introduction
A queue is a data structure that follows a specific order for processing elements.
Real Life Example
The queue is modeled like people waiting in line to be served.
Definition
Queue: A data structure modeled after a line of people waiting to be served.
Rule: The first item added to the queue is the first one to be removed.
Queue adheres to the First In First Out (FIFO) principle.
In contrast, a Stack follows a Last In First Out (LIFO) principle.
Queue Properties:
Elements are added at one end (rear).
Elements are removed from the other end (front).
Middle elements in the queue are not accessible.
Logical Representation
Visualization of a queue:
Front: The end from which elements are removed.
Rear: The end at which elements are added.
Queue Operations
Four primary operations for queue manipulation:
Dequeue(): Deletes the first added element (from the front).
Enqueue(int v): Adds an element v to the end of the queue (towards the rear).
Front(): Returns the first element in the queue.
Rear(): Returns the last element added to the queue.
Example of Enqueue and Dequeue Operations
Initial empty queue scenario:
X
Enqueue(A): X → A
Enqueue(K): A → K
Dequeue: K is removed
Enqueue(N): A → K → N
Dequeue: K is removed again, and repeat.
Application of Queues
Queues have various applications:
Waiting for a printer: Managing print jobs in a queue.
Access to disk storage: Managing requests to disk operations from multiple clients.
Hospital Systems: Managing patient appointments and treatments.
Order Processing in Restaurants or Government Systems: Handling orders efficiently.
Implementation of Queues
1. Array-Based Queues
Array-based queues maintain a fixed size and use indices to keep track of the front and rear positions.
Queue ADT (Abstract Data Type)
template <class Type>
class queueADT {
public:
virtual bool isEmptyQueue() const = 0;
virtual bool isFullQueue() const = 0;
virtual void initializeQueue() = 0;
virtual Type front() const = 0;
virtual Type back() const = 0;
virtual void addQueue(const Type& queueElement) = 0;
virtual void deleteQueue() = 0;
private:
int maxSize;
int front;
int rear;
Type * list;
};
Variables in Array-Based Queue
Queue Variables:
Four member variables are important:
An array to store queue elements.
queueFront: Index of the first element in the queue.queueRear: Index of the last element added.maxQueueSize: Maximum size of the queue.
Examples of accesses with pointers:
After enqueueing and dequeuing, the corresponding indices will update.
Queue Operations
Two key operations on the queue:
addQueue (to add elements).
deleteQueue (to remove elements).
Additional operations include:
initializeQueue,isEmptyQueue,isFullQueue,front,backto track current status.
Add and Delete Operations Example
Starting with an empty queue:
count = 0;maxSize = 20;list[]: empty
After various operations:
Enqueue A, B, C,
Count, front, rear indices update accordingly.
Queue Overflow Problem
Occurs when trying to add an element to a full queue:
Conditions that lead to an overflow include:
Queue appears full despite having empty spaces at the front due to the linear structure.
Solutions involve shifting elements or using circular arrays.
Solution to Non-Real Overflow
First Solution: Shift elements back when inserting into a full queue.
Second Solution: Assume a circular array.
Circular Queue Implementation
Logical representation:
Formed by wrapping around the array to use all spaces efficiently.
Pointers are manipulated using modulo operation % to keep within bounds.
Example of adding to a circular queue ensures no element is left out.
Implementation of Circular Queues Code Example
if (rear == maxSize - 1 && count < maxSize) {
rear = 0;
} else {
rear = (rear + 1) % maxSize;
}
list[rear] = newElement;
count++;
Managing Circular Queue States
Maintain and identify conditions for:
Empty Queue:
count == 0.Full Queue:
count == maxQueueSize.
Linked Representation of Queue
Node Structure
Achieved through dynamically allocated memory using nodes;
template <class Type>
struct nodeType {
Type info;
nodeType<Type>* link;
};
Linked Queue ADT
Definition of a linked queue which includes: front and rear pointers, and required operations (similar to array-based).
Adding and Deleting Nodes in Linked Queue
Node links and memory management practices:
Dynamically create node on addition.
Properly link nodes when adding to the end.
Remove nodes safely, ensuring no memory leaks.
Priority Queues
Queue structures allowing higher priority items to be handled differently:
Ordinary linked list can be formed to order items from highest to lowest priority.
Treelike structures can be used for efficiency.