Open In App

Python | Queue using Doubly Linked List

Last Updated : 05 Jan, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

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:

Output:
Queue operations using doubly linked list
queue elements are:
4->5->6->7->
first element is  4
Size of the queue is  4
After applying dequeue() two times
queue elements are:
6->7->
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)

Next Article

Similar Reads