Horje
Creating Queue Using Collections.Deque In Python

In Python, Queue and Deque are the data structures used for managing collections of elements in a first-in, first-out (FIFO) manner. In this article, we will learn to implement queues using collections.deque in Python.

Create Queue Using Collections.deque In Python

Below, is the Implementation of Creating Queue Using Collections.Deque in Python:

Step 1: Importing collections.deque

In the below step, we import the deque class from the collections module using the statement from the collections import deque. This allows us to access the functionality of the deque data structure.

Python3
from collections import deque

Step 2: Defining the Queue Class

In the below step, we define the Queue class with methods to manipulate a queue data structure. The __init__ method initializes an empty deque to store queue elements. The enqueue, dequeue, peek, is_empty, and size methods handle adding, removing, peeking at the front, checking emptiness, and getting the size of the queue, respectively, using the deque’s operations.

Python3
class Queue:
    def __init__(self):
        self.queue = deque()

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

    def dequeue(self):
        if not self.is_empty():
            return self.queue.popleft()
        else:
            raise IndexError("dequeue from an empty queue")

    def peek(self):
        if not self.is_empty():
            return self.queue[0]
        else:
            raise IndexError("peek from an empty queue")

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

    def size(self):
        return len(self.queue)

Step 3: Using the Queue

In the below step, we perform how to use the Queue class we defined. We create a queue instance named queue, enqueue elements 10, 20, and 30 into the queue, dequeue an element from the queue, peek at the front element, check if the queue is empty, and get its size.

Python3
# Create a queue instance
queue = Queue()

# Enqueue elements into the queue
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)

# Dequeue an element from the queue
dequeued_item = queue.dequeue()
print("Dequeued item:", dequeued_item)

# Peek at the front of the queue
front_item = queue.peek()
print("Front item:", front_item)

# Check if the queue is empty
print("Is queue empty?", queue.is_empty())

# Get the size of the queue
print("Queue size:", queue.size())

Complete Code

Below, is the complete code for creating a queue using collections.deque in Python.

main.py

Python3
from collections import deque


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

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

    def dequeue(self):
        if not self.is_empty():
            return self.queue.popleft()
        else:
            raise IndexError("dequeue from an empty queue")

    def peek(self):
        if not self.is_empty():
            return self.queue[0]
        else:
            raise IndexError("peek from an empty queue")

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

    def size(self):
        return len(self.queue)


# Using the Queue
queue = Queue()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
dequeued_item = queue.dequeue()
print("Dequeued item:", dequeued_item)
front_item = queue.peek()
print("Front item:", front_item)
print("Is queue empty?", queue.is_empty())
print("Queue size:", queue.size())

Output
Dequeued item: 10
Front item: 20
Is queue empty? False
Queue size: 2



Reffered: https://www.geeksforgeeks.org


Python

Related
What is the Difference Between pip and Conda? What is the Difference Between pip and Conda?
How to Learn Python Basics With ChatGPT How to Learn Python Basics With ChatGPT
How to Round Floating Value to Two Decimals in Python How to Round Floating Value to Two Decimals in Python
How to Parallelize a While loop in Python? How to Parallelize a While loop in Python?
Python Program to Print all Happy Numbers Between 1 and 100 Python Program to Print all Happy Numbers Between 1 and 100

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13