Queues are linear data structures like array. They support:
Enqueue: Adding an element at the back of the queue.
Dequeue: Removing an element from the front of the queue.
Types of Queues:
Simple Queue: This is the most basic type. Elements are inserted at the rear and removed from the front, following the FIFO principle.
Deque (Double Ended Queue): Here, elements can be added or removed from both ends of the Queue.
Circular Queue: the last element points to the first element, making a loop!
Priority Queue: In this type, not all elements are equal. Some are considered more important and get to jump the line.
Affinity Queue: In this type, every element has an affinity & is placed with an element having the same affinity; otherwise placed at the rear.
Operations on Simple Queues:
Enqueue: Adding an element at the back of the queue.
Dequeue: Removing an element from the front of the queue.
Peek: see the first element in the Queue
IsEmpty: check if queue is empty
Node
Node
Node
Node
Queue
Implementing a Simple Queue:
We will be implementing a simple Queue, of finite size using a Linked List.
It will be implemented in 2 parts:
Node Class: This is an inner class used to represent each element in the queue. Each node contains the data and a reference to the next node in the list.
Queue Class Attributes:
front
: A reference to the first node in the queue.rear
: A reference to the last node in the queue.size
: An integer tracking the number of elements in the queue.Constructor:
front
and rear
are set to null
, and size
is set to 0.class Node {
// Node class for storing data and
// the reference to the next node
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
// Queue class using linked list
constructor() {
this.front = null;
this.rear = null;
this.size = 0;
}
}
Enqueue Operation (enqueue method):
// Add an element to the rear of the queue
enqueue(data) {
let newNode = new Node(data);
if (this.rear === null) { // queue is empty
this.front = this.rear = newNode;
} else {
// adding the new node in the next of current rear node
this.rear.next = newNode;
//. changing the current rear node with the new one
this.rear = newNode;
}
this.size++;
}
Dequeue Operation (dequeue method):
// Remove an element from the front of the queue
dequeue() {
if (this.front === null) {
return null;
}
let temp = this.front;
this.front = this.front.next;
/** when queue has only 1 element */
if (this.front === null) {
this.rear = null;
}
this.size--;
return temp.data;
}
Utility Methods:
peek:
This method returns the data from the front node without removing it.
If the queue is empty (front == null), it returns null.
isEmpty: Checks if the queue is empty (i.e., size == 0).
size: Returns the number of elements in the queue.
// Get the front element of the queue
peek() {
/** when queue is empty */
if (this.front === null) {
return null;
}
return this.front.data;
}
// Check if the queue is empty
isEmpty() {
return this.size === 0;
}
// Get the number of elements in the queue
getSize() {
return this.size;
}
Implementing a Circular Queue
We will be implementing a simple Queue, of finite size using a Linked List.
It will be implemented in 2 parts:
Node Class: This is an inner class used to represent each element in the queue. Each node contains the data and a reference to the next node in the list.
Queue Class Attributes:
front: A reference to the first node in the queue.
rear: A reference to the last node in the queue.
size: An integer tracking the number of elements in the queue.
Constructor:
The constructor initialises an empty queue where both front and rear are set to null, and size is set to 0.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class CircularQueue {
constructor(capacity) {
this.capacity = capacity;
this.head = null;
this.tail = null;
this.size = 0;
}
}
Step-by-step solution walkthrough:
Problem 1: Reverse a Queue
Problem 4: Palindrome Check using Queue
Problem 4: Palindrome Check using Queue
Problem 4: Palindrome Check using Queue
Problem 4: Palindrome Check using Queue
Problem 4: Palindrome Check using Queue
Problem 4: Palindrome Check using Queue
Problem 4: Palindrome Check using Queue
Problem 5: Zigzag Iterator
Problem 6: Max of All Subarrays of Size 'k'
Problem 6: Max of All Subarrays of Size 'k'