0
0
DSA Pythonprogramming

Enqueue Operation in DSA Python

Choose your learning style9 modes available
Mental Model
Add a new item to the end of a line so it waits its turn.
Analogy: Like people lining up at a ticket counter, new people join at the back of the line.
front -> 1 -> 2 -> 3 -> null
rear ↑
Dry Run Walkthrough
Input: queue: 1 -> 2 -> 3 -> null, enqueue value 4
Goal: Add value 4 to the end of the queue
Step 1: Create new node with value 4
new_node = 4 -> null
Why: We need a new node to add to the queue
Step 2: Link current rear node (3) to new node (4)
1 -> 2 -> 3 -> [rear->4] -> null
Why: Attach new node at the end of the queue
Step 3: Update rear pointer to new node (4)
front -> 1 -> 2 -> 3 -> 4 -> null
rear ↑
Why: Rear must always point to the last node
Result:
front -> 1 -> 2 -> 3 -> 4 -> null
rear ↑
Annotated Code
DSA Python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, value):
        new_node = Node(value)  # create new node
        if self.rear is None:  # empty queue
            self.front = new_node
            self.rear = new_node
        else:
            self.rear.next = new_node  # link new node at end
            self.rear = new_node       # update rear pointer

    def __str__(self):
        result = []
        current = self.front
        while current:
            result.append(str(current.value))
            current = current.next
        return ' -> '.join(result) + ' -> null'

# Driver code
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(queue)  # before enqueue
queue.enqueue(4)
print(queue)  # after enqueue
new_node = Node(value) # create new node
create a new node to add at the end
if self.rear is None: # empty queue
check if queue is empty to initialize front and rear
self.rear.next = new_node # link new node at end
attach new node after current rear
self.rear = new_node # update rear pointer
move rear pointer to new last node
OutputSuccess
1 -> 2 -> 3 -> null 1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(1) because we add the new node directly at the rear without traversal
Space: O(1) because only one new node is created regardless of queue size
vs Alternative: Compared to adding at front or traversing to end (O(n)), enqueue at rear is faster and constant time
Edge Cases
empty queue
front and rear both point to the new node
DSA Python
if self.rear is None:  # empty queue
When to Use This Pattern
When you see a problem needing to add items in order and process them FIFO, reach for enqueue operation to add at the rear efficiently.
Common Mistakes
Mistake: Not updating the rear pointer after adding the new node
Fix: Always set rear = new_node after linking it
Mistake: Not handling empty queue case, causing front to remain None
Fix: Check if rear is None and set front and rear to new_node
Summary
Adds a new element to the end of the queue.
Use when you want to keep order and add items to the back of a waiting line.
Remember to update the rear pointer and handle empty queue separately.