Python Queued List
Queue is a linear data structure in Python which stores the elements in First-in, first-out (FIFO) order. In the queue, the item or element which is inserted first in the queue are removed first. In simple terms, both inserting and deleting elements in queue are in same direction. Inserting elements in queue is termed as enqueue and deleting elements is termed as dequeue.
Different functionalities associated with Queue
- Enqueue: This function adds an item to the queue. It leads to overflow condition if the queue is full.
- Dequeue: This function removes an item from the queue. The elements which are inserted first are deleted first in the same order. It leads to an underflow condition if the queue is empty.
- Front: This function is used to get the front item from queue.
- Rear: This function is used to get the last item from queue.
There are various ways for implementing Queue in Python.
- List
- collection.deque
- queue.Queue
Implementing Queue using List
List is a structure of Python which can be used to implement queue. For enqueuing the elements in queue append()
is used, whereas for dequeuing pop()
is used in list. The disadvantage of using list is, it is somewhat slow process as inserting and deleting of an element at the beginning requires shifting of all other elements in the queue one by one.
Python Code:
# Creating an empty queue
queue = []
# Adding elements to the queue
queue.append('Summer')
queue.append('Monsoon')
queue.append('Winter')
print("Seasons in Queue are: ",queue)
# Removing elements from the queue
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print("\nQueue after removing elements")
print(queue)
Output:
Seasons in Queue are: [‘Summer’, ‘Monsoon’, ‘Winter’]
Elements dequeued from queue
Summer
Monsoon
Queue after removing elements
[‘Winter’]
Implementing Queue using collection.dequeue
Queue can also be created by using deque class from collection module. Deque means double ended queue. It is better than list implementation as it is quicker in the append and pop operations than list. It provides O(1) time complexity for the insert and delete operations, whereas list consumes O(n) time.
Python Code:
from collections import deque
queue = deque()
# Adding elements to the queue
queue.append('Summer')
queue.append('Monsoon')
queue.append('Winter')
print("Seasons in Queue are: ",queue)
# Removing elements from the queue
print("\nElements dequeued from queue")
print(queue.popleft())
print(queue.popleft())
print("\nQueue after removing elements")
print(queue)
Output:
Seasons in Queue are: deque([‘Summer’, ‘Monsoon’, ‘Winter’])
Elements dequeued from queue
Summer
Monsoon
Queue after removing elements
deque([‘Winter’])
Implementation of Queue using queue.Queue
Queue class in queue module uses FIFO order. It uses put()
function in order to insert or push an element into the Queue. get()
function is used to pop or delete the recently added element from the Queue.
Python Code:
from queue import Queue
queue = Queue()
# Adding elements to the queue
queue.put('Summer')
queue.put('Monsoon')
queue.put('Winter')
print("Seasons in Queue are: ",queue)
# Removing elements from the queue
print("\nElements dequeued from queue")
print(queue.get(0))
print(queue.get(0))
print("\nQueue after removing elements")
print(queue)
Output:
Seasons in Queue are: <queue.Queue object at 0x000001D464BD9C88>
Elements dequeued from queue
Summer
Monsoon
Queue after removing elements
<queue.Queue object at 0x000001D464BD9C88>