Queue Intro

0

Understanding Queues in Data Structures (FIFO Explained)

🧠 What Is a Queue?

 

A queue is a data structure that follows the FIFO principle — First In, First Out.
This means the first element added to the queue will be the first one to be removed.

 

You can imagine it like waiting in line at a ticket counter:

  • The first person in line gets served first.
  • New people join at the end of the line.
  • No one can skip ahead — fair and organized!

 

So in simple terms:

  • Enqueue → means add an element to the end of the queue.
  • Dequeue → means remove an element from the front of the queue.

🎯 Example: Real-Life Analogy

 

Imagine a line of people at a movie ticket counter:

Operation Description Resulting Queue
Enqueue(A) Person A joins [A]
Enqueue(B) Person B joins [A, B]
Enqueue(C) Person C joins [A, B, C]
Dequeue() Person A gets ticket & leaves [B, C]

 

Here, A was first to enter and first to leave — that’s FIFO in action!


⚙️ How Queues Work in Data Structures

 

A queue allows:

  • Insertion at one end (called rear or last)
  • Deletion at the other end (called front or first)

 

This makes it very useful in situations where you need orderly processing — for example:

  • Print queue (documents printed in order received)
  • CPU task scheduling
  • Message queues in distributed systems

🧩 Queue Using Lists

 

Let’s analyze how a list can act as a queue.

 

In Python:

queue = []
queue.append(10)   # Enqueue
queue.append(20)
queue.pop(0)       # Dequeue

 

Here’s the problem:

  • append() (add at end) → O(1) (fast)
  • pop(0) (remove from front) → O(n) (slow, because it shifts all elements left)

 

That means using a list as a queue is not always efficient for large data — one end is fast, the other is slow.


⚙️ Queue Using Linked List

 

Now let’s look at a linked list.

 

In a singly linked list:

  • Adding (enqueue) at the end → O(1)
  • Removing (dequeue) from the front → O(1)

 

That’s perfect for queues! 🎯

Operation Linked List Complexity
Enqueue (add at end) O(1)
Dequeue (remove from front) O(1)
Any other operation Possibly O(n)

 

This is why linked lists are better suited for implementing queues.


🧱 Key Terms

Term Meaning
Enqueue Add an element to the end of the queue
Dequeue Remove an element from the front of the queue
First The front node (similar to “head”)
Last The rear node (similar to “tail”)

 

In queues, we rename the head and tail of linked lists to first and last to make the concept more intuitive.


💡 Summary

Feature List Linked List
Enqueue Complexity O(1) O(1)
Dequeue Complexity O(n) O(1)
Ideal For Small queues Large, dynamic queues

🧩 Quick Python Implementation (Using Linked List)

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class Queue:
    def __init__(self, value):
        new_node = Node(value)
        self.first = new_node
        self.last = new_node
        self.length = 1

    def enqueue(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.first = new_node
            self.last = new_node
        else:
            self.last.next = new_node
            self.last = new_node
        self.length += 1

    def dequeue(self):
        if self.length == 0:
            return None
        temp = self.first
        self.first = self.first.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.last = None
        return temp.value

Example:

q = Queue(10)
q.enqueue(20)
q.enqueue(30)
print(q.dequeue())  # Output: 10
print(q.dequeue())  # Output: 20

🏁 Final Thoughts

 

Queues are simple yet powerful.
They form the basis for many real-world systems — from ticket booking to background task scheduling.

 

Remember the golden rule of a queue:

“First In, First Out — FIFO!”

 

Master this, and you’re ready to move on to more complex data structures like priority queues and circular queues 🚀

Leave a Reply

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

Scroll to Top