0
0
Data-structures-theoryConceptBeginner · 3 min read

Operations on Queue: Definition, Examples, and Usage

The main operations on a queue are enqueue (adding an item), dequeue (removing the front item), peek (viewing the front item without removing it), and isEmpty (checking if the queue has no items). These operations follow the First-In-First-Out (FIFO) principle, where the first item added is the first to be removed.
⚙️

How It Works

A queue is like a line of people waiting for a service, such as at a ticket counter. The first person to get in line is the first one to be served and leave the line. This is called First-In-First-Out (FIFO).

The main operations on a queue help manage this line: enqueue adds a new person to the end of the line, dequeue removes the person at the front who is being served, peek lets you see who is next without removing them, and isEmpty checks if the line is empty.

These operations keep the order fair and predictable, making queues useful in many real-life and computing scenarios.

💻

Example

This example shows a simple queue implemented using a list in Python, demonstrating the main operations.

python
class Queue:
    def __init__(self):
        self.items = []

    def enqueue(self, item):
        self.items.append(item)  # Add to the end

    def dequeue(self):
        if not self.is_empty():
            return self.items.pop(0)  # Remove from the front
        return None

    def peek(self):
        if not self.is_empty():
            return self.items[0]  # View front item
        return None

    def is_empty(self):
        return len(self.items) == 0

# Using the queue
queue = Queue()
queue.enqueue('apple')
queue.enqueue('banana')
queue.enqueue('cherry')
print(queue.peek())    # Output: apple
print(queue.dequeue()) # Output: apple
print(queue.dequeue()) # Output: banana
print(queue.is_empty()) # Output: False
print(queue.dequeue()) # Output: cherry
print(queue.is_empty()) # Output: True
Output
apple apple banana False cherry True
🎯

When to Use

Queues are useful whenever you need to manage tasks or items in the order they arrive. For example, print jobs sent to a printer are handled in the order they come in, so a queue ensures fairness.

Other common uses include managing requests in web servers, scheduling tasks in operating systems, and handling events in user interfaces. Whenever the order of processing matters and the first item added should be the first processed, a queue is the right choice.

Key Points

  • Enqueue: Add an item to the back of the queue.
  • Dequeue: Remove the item from the front of the queue.
  • Peek: Look at the front item without removing it.
  • IsEmpty: Check if the queue has no items.
  • Queues follow the FIFO principle, ensuring order is preserved.

Key Takeaways

A queue operates on a First-In-First-Out (FIFO) basis with key operations like enqueue and dequeue.
Enqueue adds items to the back, dequeue removes from the front, and peek views the front item.
Queues are ideal for managing ordered tasks such as print jobs, requests, or event handling.
Checking if a queue is empty helps avoid errors when removing or viewing items.
Understanding queue operations helps in designing fair and efficient processing systems.