Open In App

Python | Queue using Doubly Linked List

Last Updated : 05 Jan, 2023
Like Article

A Queue is a collection of objects that are inserted and removed using First in First out Principle(FIFO). Insertion is done at the back(Rear) of the Queue and elements are accessed and deleted from first(Front) location in the queue.

Queue Operations:

1. enqueue()     : Adds element to the back of Queue.
2. dequeue()     : Removes and returns the first element from the queue.
3. first()       : Returns the first element of the queue without removing it.
4. size()        : returns the number of elements in the Queue.
5. isEmpty()     : Return True if Queue is Empty else return False.
6. printqueue()  : Print all elements of the Queue.

Below is the implementation of the above-mentioned Queue operations using Doubly LinkedList in Python:


# A complete working Python program to demonstrate all
# Queue operations using doubly linked list
# Node class
class Node:
# Function to initialise the node object
    def __init__(self, data): = data # Assign data = None # Initialize next as null
        self.prev = None # Initialize prev as null
# Queue class contains a Node object
class Queue:
    # Function to initialize head
    def __init__(self):
        self.head = None
# Function to add an element data in the Queue
    def enqueue(self, data):
        if self.last is None:
            self.head =Node(data)
            self.last =self.head
   = Node(data)
            self.last =
# Function to remove first element and return the element from the queue
    def dequeue(self):
        if self.head is None:
            return None
            self.head =
            return temp
# Function to return top element in the queue
    def first(self):
# Function to return the size of the queue
    def size(self):
        while temp is not None:
        return count
# Function to check if the queue is empty or not     
    def isEmpty(self):
        if self.head is None:
            return True
            return False
# Function to print the stack
    def printqueue(self):
        print("queue elements are:")
        while temp is not None:
# Code execution starts here         
if __name__=='__main__':
# Start with the empty queue
  queue = Queue()
  print("Queue operations using doubly linked list")
# Insert 4 at the end. So queue becomes 4->None 
# Insert 5 at the end. So queue becomes 4->5None 
# Insert 6 at the end. So queue becomes 4->5->6->None 
# Insert 7 at the end. So queue becomes 4->5->6->7->None 
# Print the queue
# Print the first element
  print("\nfirst element is ",queue.first())
# Print the queue size
  print("Size of the queue is ",queue.size())
# remove the first element
# remove the first element
# first two elements are removed
# Print the queue
  print("After applying dequeue() two times")
# Print True if queue is empty else False
  print("\nqueue is empty:",queue.isEmpty())


Queue operations using doubly linked list
queue elements are:
first element is  4
Size of the queue is  4
After applying dequeue() two times
queue elements are:
queue is empty: False

Time Complexity for operations:

  • enqueue(): O(1)
  • dequeue():O(1)
  • first(): O(1)
  • size(): O(N)
  • isEmpty():  O(1)
  • printStack():O(N)

Auxiliary Space required for operations:

  • enqueue(): O(1)
  • dequeue():O(1)
  • first(): O(1)
  • size(): O(1)
  • isEmpty():  O(1)
  • printStack():O(1)

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads