Queues are a fundamental data structure in computer science, characterized by their First-In-First-Out (FIFO) property. This means that the first element added to the queue will be the first one to be removed. In this section, we will cover the basic operations that can be performed on queues, including enqueue, dequeue, peek, and checking if the queue is empty.

Key Concepts

  1. Enqueue: Adding an element to the end of the queue.
  2. Dequeue: Removing an element from the front of the queue.
  3. Peek: Viewing the element at the front of the queue without removing it.
  4. IsEmpty: Checking if the queue is empty.

Enqueue Operation

The enqueue operation adds an element to the end of the queue. Here is a simple implementation in Python:

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

    def enqueue(self, item):
        self.items.append(item)
        print(f"Enqueued: {item}")

# Example usage
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

Explanation

  • __init__ method: Initializes an empty list to store the queue elements.
  • enqueue method: Appends the new item to the end of the list.

Dequeue Operation

The dequeue operation removes an element from the front of the queue. Here is how you can implement it:

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

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        if not self.is_empty():
            item = self.items.pop(0)
            print(f"Dequeued: {item}")
            return item
        else:
            print("Queue is empty")
            return None

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

# Example usage
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.dequeue()  # This will print "Queue is empty"

Explanation

  • dequeue method: Removes and returns the first item in the list if the queue is not empty. If the queue is empty, it prints a message and returns None.
  • is_empty method: Checks if the list is empty.

Peek Operation

The peek operation allows you to view the element at the front of the queue without removing it:

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

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        if not self.is_empty():
            return self.items.pop(0)
        else:
            return None

    def peek(self):
        if not self.is_empty():
            return self.items[0]
        else:
            return None

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

# Example usage
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(f"Front item: {queue.peek()}")  # This will print "Front item: 1"

Explanation

  • peek method: Returns the first item in the list without removing it if the queue is not empty. If the queue is empty, it returns None.

IsEmpty Operation

The is_empty operation checks if the queue is empty:

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

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        if not self.is_empty():
            return self.items.pop(0)
        else:
            return None

    def peek(self):
        if not self.is_empty():
            return self.items[0]
        else:
            return None

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

# Example usage
queue = Queue()
print(f"Is queue empty? {queue.is_empty()}")  # This will print "Is queue empty? True"
queue.enqueue(1)
print(f"Is queue empty? {queue.is_empty()}")  # This will print "Is queue empty? False"

Explanation

  • is_empty method: Returns True if the list is empty and False otherwise.

Summary

In this section, we covered the basic operations that can be performed on queues:

  • Enqueue: Adding an element to the end of the queue.
  • Dequeue: Removing an element from the front of the queue.
  • Peek: Viewing the element at the front of the queue without removing it.
  • IsEmpty: Checking if the queue is empty.

These operations form the foundation for working with queues and are essential for understanding more complex data structures and algorithms. In the next section, we will explore circular queues and their applications.

© Copyright 2024. All rights reserved