Queue Data Structure in Python

In many scenarios where we need to manage objects in an orderly fashion, we turn to a fundamental data structure known as the queue. Similar to the queue at a supermarket checkout, items (or data elements) are served in a first-come, first-served basis. This characteristic is commonly referred to as "First In, First Out" (FIFO). Let's dive into the queue data structure and its implementation in Python.
What is a Queue?
A queue is a linear structure which follows a particular order in which the operations are performed. The order is FIFO, a good example of this is any queue of consumers for a resource where the consumer that came first is served first.
Operations of Queue
The queue data structure allows the following operations:
Enqueue: Add an item to the end of the queue.
Dequeue: Remove the item from the front of the queue and return it.
IsEmpty: Check if the queue is empty.
Size: Return the number of items in the queue.
Implementing a Queue in Python
Python does not have a built-in queue data structure, but it’s simple to implement one using a list. Here is a basic implementation:
class Queue:
"""
Queue Implementation in Python
"""
def __init__(self):
"""Initialize an empty queue"""
self.items = []
def enqueue(self, item):
"""Add an item to the end of the queue"""
self.items.append(item)
def dequeue(self):
"""Remove the item from the front of the queue and return it"""
if not self.is_empty():
return self.items.pop(0)
else:
return "Queue is empty"
def is_empty(self):
"""Check if the queue is empty"""
return len(self.items) == 0
def get_queue(self):
"""Get all the items in the queue"""
return self.items
def size(self):
"""Get the size of the queue"""
return len(self.items)
In this code, we use a Python list to store the elements of the queue. The enqueue method appends an item to the list, which represents adding it to the back of the queue. The dequeue method removes the first item of the list, simulating the removal of the front item of the queue.
Example Usage
if __name__ == '__main__':
queue = Queue()
queue.enqueue('Max')
queue.enqueue('Lewis')
queue.enqueue('Lando')
queue.enqueue('Perez')
print(f'Items in Queue: {queue.get_queue()}')
print(f"{queue.dequeue()} removed from the queue")
print(f"{queue.dequeue()} removed from the queue")
print(queue.get_queue())
Output:
Items in Queue: ['Max', 'Lewis', 'Lando', 'Perez']
Max removed from the queue
Lewis removed from the queue
['Lando', 'Perez']
In this example, we add several items to our queue and then dequeue them, ensuring that the order is maintained as FIFO. The output shows the dynamics of the queue as items are added and removed.
Conclusion
The queue is a versatile data structure used in various areas of computing, such as task scheduling, breadth-first search in graphs, and buffering data streams. Its FIFO nature makes it ideal for processes that require fairness and order.
With Python's flexibility, implementing and using a queue is straightforward. Knowing how to work with queues is a valuable skill, helping programmers tackle a wide range of problems efficiently.
Please visit the github repo for entire code. Thank You
niranjanblank
https://github.com/niranjanblank/Data_Structures/blob/master/Queue/Queue.py