Working with Simple Queues
Operations on Simple Queues
So, we've learned what a Queue is and the key terminologies related to it. We're now ready to roll up our sleeves and start working with Simple Queues. Remember our coffee shop line? Let's think of our Simple Queue in the same way.
The primary operations that we can perform on a Simple Queue are: Enqueue (add an element to the end of the Queue), and Dequeue (remove an element from the front). There's also Peek or Front (get the value of the front item without removing it), and IsEmpty (check if the Queue is empty).
Enqueue Operation
The Enqueue operation is about adding an element at the rear of the Queue. The step is simple: we take the item we want to add, go to the end of the line (rear of the Queue), and place it there. But wait, how do we know if there's enough space in our Queue to add a new element? Good question! Before we add a new item, we must check if our Queue is already full. If it's full, we can't add a new item - we call this situation Queue Overflow.
Dequeue Operation
The Dequeue operation is about removing an element from the front of the Queue. So, we go to the front of our Queue, serve the first customer (remove the first element), and adjust our front to point to the next customer. But here too, we need to be careful. Before we remove an item, we must check if there are any items to remove! If the Queue is empty, we can't remove anything - we call this situation Queue Underflow.
Peek and IsEmpty Operations
Peek (or Front) and IsEmpty are straightforward operations. Peek lets us see the first element in the Queue (like calling the next customer without serving them), and IsEmpty lets us know if our Queue is empty (like checking if there are any customers left to serve).
Simple Queue in Action
In this section, we're going to look at some examples of the above mentioned operations and how they work in a program.
But before we jump into coding, let's consider how we're going to structure our Queue. A simple way is to implement a queue using a LinkedList. We can use a Node structure to represent each element of the Queue. Each Node will have a variable to store the data and a Node pointer which will be pointing to the next element of the queue. In the Queue class, we will have two pointers: one to the front Node of the Queue, and one to the rear Node. Here is the description of each element of the queue class:
-
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 initializes an empty queue where both
frontandrearare set tonull, andsizeis set to 0.
- The constructor initializes an empty queue where both
-
Enqueue Operation (
enqueuemethod):- This method adds an element to the end of the queue.
- A new node is created with the given data.
- If the queue is empty (
rear == null), bothfrontandrearpoint to the new node. - If the queue is not empty, the new node is added after
rear, and thenrearis updated to point to this new node. - The
sizeof the queue is incremented.
-
Dequeue Operation (
dequeuemethod):- This method removes an element from the front of the queue.
- If the queue is empty (
front == null), the method returnsnull. - The data from the
frontnode is saved. frontis updated to point to the next node in the queue.- If the queue becomes empty after this operation (
front == null),rearis also set tonull. - The
sizeof the queue is decremented. - The method returns the saved data.
-
Peek Operation (
peekmethod):- This method returns the data from the front node without removing it.
- If the queue is empty (
front == null), it returnsnull. - Otherwise, it returns the data of the
frontnode.
-
Utility Methods:
isEmpty: Checks if the queue is empty (i.e.,size == 0).size: Returns the number of elements in the queue.
Here's a program to implement the queue. This implementation of a queue is a classic example of a FIFO (First-In-First-Out) data structure, where elements are added to the rear and removed from the front, ensuring that the order in which elements are added is the order in which they are removed.
class Queue<T> {
private Node<T> front, rear;
private int size;
// Node class for storing data and the reference to the next node
private static class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}
public Queue() {
front = rear = null;
size = 0;
}
// Add an element to the queue
public void enqueue(T data) {
Node<T> newNode = new Node<>(data);
if (rear == null) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
size++;
}
// Remove an element from the queue
public T dequeue() {
if (front == null) {
return null;
}
T data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
size--;
return data;
}
// Check the front element of the queue
public T peek() {
if (front == null) {
return null;
}
return front.data;
}
// Check if the queue is empty
public boolean isEmpty() {
return size == 0;
}
// Get the size of the queue
public int size() {
return size;
}
}
// Example usage
public class Solution {
public static void main(String[] args) {
Queue<Integer> queue = new Queue<>();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println("Front element: " + queue.peek());
System.out.println("Dequeued: " + queue.dequeue());
System.out.println("Front element: " + queue.peek());
System.out.println("Queue size: " + queue.size());
}
}
Time Complexity of Queue Operations
🤖 Don't fully get this? Learn it with Claude
Stuck on Working with Simple Queues? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Working with Simple Queues** (DSA) and want to truly understand it. Explain Working with Simple Queues from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Working with Simple Queues** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Working with Simple Queues** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Working with Simple Queues** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.