coding180 icon
Coding180
Python Queues: queue, enqueue and dequeue operations: examples and exercises

Python Queues: queue, enqueue and dequeue operations: examples and exercises

Home > Tutorials > Python for beginners > Data structure


In computer science, a queue is a data structure that allows you to store and manipulate a collection of elements. A queue is a first-in, first-out (FIFO) data structure, meaning the first element added to the queue will be the first element removed from it.

Queues are widely used in computer systems, including operating systems, communication protocols, and network management applications. In this article, we'll introduce you to the basics of queues, including enqueue and dequeue operations and different queue implementations.

Enqueue and Dequeue Operations

The two primary operations you can perform on a queue are enqueue and dequeue. Enqueue adds an element to the back of the queue, while dequeue removes the element at the front of the queue. Here's an example of how these operations work:

queue = []

# enqueue
queue.append(1)
queue.append(2)
queue.append(3)

# dequeue
queue.pop(0)  # returns 1
queue.pop(0)  # returns 2
queue.pop(0)  # returns 3

In the above example, we create an empty list called queue. We then add three elements to the queue using the append method, which adds each element to the end of the list. Finally, we remove each element from the front of the queue using the pop(0) method.

Queue Implementations

There are many ways to implement a queue in Python. Here are three examples:

List Implementation

One simple way to implement a queue is to use a Python list. As we saw in the previous example, you can append elements to the end of the list and remove them from the start of the list.

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

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

    def dequeue(self):
        return self.items.pop(0)

In the above code, we define a Queue class that has an items attribute initialized as an empty list. The enqueue method adds an element to the end of the list using the append method, and the dequeue method removes the first element from the list using the pop(0) method.

deque Implementation

Python's built-in deque class is another useful implementation for queues. A deque (short for "double-ended queue") is a data structure that supports adding and removing elements from both ends.

from collections import deque

class Queue:
    def __init__(self):
        self.items = deque()

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

    def dequeue(self):
        return self.items.popleft()

In the above code, we use Python's deque class to implement the queue. We initialize the items attribute as a deque object, which allows us to add elements to the end of the queue using the append method and remove them from the front using the popleft method.

Queue module Implementation

The queue module in Python provides an implementation of the queue data structure that has additional features for synchronization between threads.

import queue

q = queue.Queue()

q.put(1)
q.put(2)
q.put(3)

while not q.empty():
    print(q.get())

In the above code, we use the Queue class from the queue module to create a queue object. We then add elements to the queue using the put method and remove them using the get method.

Exercises

Here are some exercises to help you practice working with queues:

  1. Implement a function reverse_queue that takes a queue as an argument and returns the same queue in reverse order.
  2. Implement a function sum_queue that takes a queue of integers as an argument and returns the sum of all the elements in the queue.
  3. Implement a function is_palindrome that takes a string as an argument and returns True if the string is a palindrome (meaning it reads the same forwards and backwards), and False otherwise. You can use a queue to help solve this problem.

Conclusion

Queues are an essential data structure used in many computer systems. In this article, we introduced you to the basics of queues, including enqueue and dequeue operations and different queue implementations. We also provided some exercises to help you practice working with queues. With this knowledge, you can start building more complex applications that utilize queues to their full potential.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel