0
0
DSA Pythonprogramming

Peek Front Element of Queue in DSA Python

Choose your learning style9 modes available
Mental Model
A queue is like a line where the first person in line is the next to be served. Peeking means looking at that first person without removing them.
Analogy: Imagine waiting in line at a coffee shop. Peeking is like glancing at the person at the front of the line to see who will be served next, without letting them leave the line.
front -> [1] -> 2 -> 3 -> null
          ↑peek
Dry Run Walkthrough
Input: queue: 1 -> 2 -> 3, peek front element
Goal: To see the value of the front element without removing it from the queue
Step 1: Access the front pointer of the queue
front -> [1] -> 2 -> 3 -> null
          ↑peek
Why: We need to look at the first element to know what is at the front
Step 2: Read the value at the front node
front -> [1] -> 2 -> 3 -> null
          ↑peek
Why: Reading the value gives us the front element without changing the queue
Result:
front -> 1 -> 2 -> 3 -> null
peeked value = 1
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)
        if not self.rear:
            self.front = self.rear = new_node
            return
        self.rear.next = new_node
        self.rear = new_node

    def peek(self):
        if not self.front:
            return None
        return self.front.value

    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)
print("Peeked value =", queue.peek())
if not self.front:
check if queue is empty to avoid errors
return self.front.value
return the value at the front without removing it
OutputSuccess
1 -> 2 -> 3 -> null Peeked value = 1
Complexity Analysis
Time: O(1) because peeking only reads the front element without traversal
Space: O(n) because the queue stores n elements; peek uses no extra space
vs Alternative: Peeking is faster than dequeue which removes the element; peek does not change the structure
Edge Cases
empty queue
peek returns None indicating no front element
DSA Python
if not self.front:
When to Use This Pattern
When you need to check the next item to process without removing it, use the peek operation on a queue.
Common Mistakes
Mistake: Trying to peek when the queue is empty without checking causes errors
Fix: Always check if the front is None before accessing its value
Summary
Peek returns the value at the front of the queue without removing it.
Use peek when you want to see the next element to be processed without changing the queue.
The key insight is that peek reads the front pointer's value without modifying the queue structure.