Understanding the Dequeue (DQ) Method in Queues

0

Understanding the Dequeue (DQ) Method in Queues

 

When working with queues, one of the most important operations is dequeue — removing an element from the front of the queue.

 

Let’s go step-by-step and understand how to write the dequeue() method properly, including handling all the edge cases.


🧠 What Is a Queue?

 

A queue follows the FIFO (First In, First Out) principle — the first element inserted is the first to come out.

 

Imagine people standing in line for movie tickets:

  • The first person to join the line gets the ticket first.
  • New people always join at the end of the line.

 

In Python, we can represent this using a linked list where:

  • first points to the front of the queue.
  • last points to the rear of the queue.

🧩 The Goal of dequeue()

 

The dequeue() method removes and returns the first node in the queue.
However, we must handle three scenarios carefully:

  1. Queue has no items (empty).
  2. Queue has only one item.
  3. Queue has two or more items.

🧮 Step-by-Step Implementation

 

Let’s build our dequeue() method piece by piece.

1️⃣ Case 1: Empty Queue

 

If there are no elements to remove, we simply return None.

def dequeue(self):
    if self.length == 0:
        return None

2️⃣ Case 2: Only One Item in Queue

 

If the queue has just one node:

  • We need to remove it.
  • Set both first and last to None.
  • Decrease the length by 1.
temp = self.first
if self.length == 1:
    self.first = None
    self.last = None

 

Here, temp temporarily stores the node that we are removing, so we can return it later.


3️⃣ Case 3: Two or More Items in Queue

 

If more than one element exists:

  • Move the first pointer to the next node.
  • Disconnect the removed node (temp) by setting its next to None.
  • Decrease the queue length by 1.
else:
    self.first = self.first.next
    temp.next = None

 

Finally, we return temp (the removed node).


✅ Complete dequeue() Method

 

Here’s the full code in one place:

def dequeue(self):
    if self.length == 0:
        return None

    temp = self.first

    if self.length == 1:
        self.first = None
        self.last = None
    else:
        self.first = self.first.next
        temp.next = None

    self.length -= 1
    return temp

🧪 Testing the dequeue() Method

 

Let’s test with all three scenarios.

my_queue = Queue()
my_queue.enqueue(1)
my_queue.enqueue(2)

# Case 1: Two or more items
print(my_queue.dequeue().value)  # Output: 1

# Case 2: One item
print(my_queue.dequeue().value)  # Output: 2

# Case 3: Empty queue
print(my_queue.dequeue())        # Output: None

🧩 Visualization

 

Let’s see what’s happening step by step:

Step Queue Before Operation Queue After Returned
1 [1 → 2] dequeue() [2] 1
2 [2] dequeue() [] 2
3 [] dequeue() [] None

💡 Key Takeaways

  • Always handle edge cases: empty queue and single element.
  • Always return the removed node, not just the value.
  • Properly disconnect the removed node to avoid unwanted references.
  • Decrement the length a

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top