DSA
DSA
Key Operations
3. Peek/Front: Getting the front element of the queue without removing it.
Types of Queues
1. Simple Queue: The basic type where elements are added to the rear and removed from the
front.
2. Circular Queue: The rear of the queue wraps around to the front when it reaches the end,
making efficient use of memory.
3. Priority Queue: Elements are removed based on priority rather than the order of insertion.
4. Double-Ended Queue (Deque): Elements can be added or removed from both the front and
rear.
Applications of Queue
The Simple Queue, also known as a Linear Queue, is the basic form of a queue data structure. In this
type, elements are inserted at one end, called the rear, and removed from the other end, called the
front. It strictly follows the First-In-First-Out (FIFO) principle, meaning the first element added is the
first one to be removed.
Key Characteristics
1. Insertion at Rear: New elements are always added at the rear end of the queue.
2. Deletion from Front: Elements are removed from the front end of the queue.
3. FIFO Behavior: The order in which elements enter the queue is the same order in which they
are removed.
1. Enqueue (Insertion): Adds an element to the rear of the queue. If the queue is full, this
operation cannot be performed (known as "queue overflow").
2. Dequeue (Deletion): Removes an element from the front of the queue. If the queue is empty,
this operation cannot be performed (known as "queue underflow").
Limitations
• Wasted Space: In a simple queue implemented using an array, after several dequeue
operations, the front portion of the array may become unusable, leading to wasted memory.
Example
Array Representation
• An array can be used to implement a simple queue, where the front and rear pointers indicate
where elements are added and removed.
• When the queue is empty, both front and rear point to -1 or null.
• A linked list can also be used, where each node represents an element in the queue, and
pointers link each node in the correct order.
The simple queue's straightforward nature makes it suitable for tasks where order needs to be
maintained, such as scheduling tasks, managing resources, or simulating real-life scenarios like queues
in a supermarket.
Algorithm to insert in Array representation of Linear Queue
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR→DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT→ NEXT = REAR →NEXT = NULL
ELSE
SET REAR→ NEXT = PTR
SET REAR = PTR
SET REAR →NEXT = NULL
[END OF IF]
Step 4: END
Applications of Deques
• Palindromic Checking: Can be used to check whether a string is a palindrome.
• Undo Operations in Software: Maintaining a history of operations.
• Scheduling Algorithms: Deques can be used in scheduling algorithms that require both FIFO
and LIFO order.