Log In Sign Up
Day 12

Day 12: Introduction to Queues

12/60 Days

Introduction to Queues #

Welcome to Day 12 of our 60 Days of Coding Algorithm Challenge! Today, we’ll explore queues, another fundamental data structure that follows the First-In-First-Out (FIFO) principle.

What is a Queue? #

A queue is a linear data structure that follows a particular order in which operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first.

Basic Operations of a Queue #

  1. Enqueue: Adds an element to the rear of the queue
  2. Dequeue: Removes the element from the front of the queue
  3. Front: Get the front element from the queue without removing it
  4. Rear: Get the last element from the queue without removing it
  5. isEmpty: Check if the queue is empty

Implementing a Queue in Python #

We can implement a queue using a Python list or create a custom class. Let’s implement both:

Using a Python List #

 1class Queue:
 2    def __init__(self):
 3        self.items = []
 4
 5    def is_empty(self):
 6        return len(self.items) == 0
 7
 8    def enqueue(self, item):
 9        self.items.append(item)
10
11    def …