Implementation of Deque using Doubly Linked List in Python Last Updated : 08 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report A deque (double-ended queue) is a linear data structure that allows insertion and deletion from both the front and rear efficiently. A Doubly Linked List (DLL) is a preferred choice for implementing a deque as it enables O(1) insertion and deletion without shifting elements, unlike lists. Node StructureA Doubly Linked List consists of nodes where each node contains:Data (the value stored)Pointer to the next nodePointer to the previous node Python class Node: def __init__(self, key): self.key = key self.next = None self.prev = None Operations Explained1. Insertion at Rear (insert_rear(x)):Creates a new node and inserts it at the rear.Updates pointers accordingly.2. Deletion from Front (delete_front()):Removes the front node and updates pointers.Returns None if the deque is empty.3. Displaying Deque (display()):Traverses the list from front to rear and returns elements.4. Checking Size (get_size()):Returns the number of elements in the deque.5. Checking if Empty (is_empty()):Returns True if the deque is empty, otherwise FalseDeque Implementation Using Doubly Linked List Python class Node: def __init__(self, key): self.key = key self.next = None self.prev = None class MyDeque: def __init__(self): self.front = None self.rear = None self.size = 0 def insert_rear(self, x): temp = Node(x) if self.rear is None: self.front = self.rear = temp else: self.rear.next = temp temp.prev = self.rear self.rear = temp self.size += 1 def delete_front(self): if self.front is None: return None else: res = self.front.key self.front = self.front.next if self.front is None: self.rear = None else: self.front.prev = None self.size -= 1 return res def get_size(self): return self.size def is_empty(self): return self.size == 0 def display(self): temp = self.front elements = [] while temp: elements.append(temp.key) temp = temp.next return elements # Example Usage dq = MyDeque() dq.insert_rear(10) dq.insert_rear(20) dq.insert_rear(30) print("Deque after insertions:", dq.display()) print("Deleted from front:", dq.delete_front()) print("Deque after deletion:", dq.display()) OutputDeque after insertions: [10, 20, 30] Deleted from front: 10 Deque after deletion: [20, 30] Complexity AnalysisOperationTime ComplexityInsert at FrontO(1)Insert at RearO(1)Delete from FrontO(1)Delete from RearO(1)Get SizeO(1)Check if EmptyO(1)Display ElementsO(n) Comment More infoAdvertise with us Next Article Python Program For Deleting A Node In A Doubly Linked List B brijkan3mz4 Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Implementation of Deque Using List in Python A Deque (double-ended queue) is a data structure that allows insertion and deletion from both the front and rear. Deques are widely used in applications requiring efficient access and modification at both ends. While Python provides a built-in collections.deque for optimized performance, a deque can 5 min read Python Program to Implement Stack Using Linked List In Python, creating a stack using a linked list involves implementing a data structure where elements are added and removed in a last-in-first-out (LIFO) manner. This approach uses the concept of nodes interconnected by pointers, allowing efficient insertion and deletion operations. We are given a L 4 min read Implement Stack Using Deque in Python In Python, Stack, and collections. deque is a foundational data structure used for managing elements in a Last-In-First-Out (LIFO) order. In this article, we will learn to create a stack using collections. deque in Python. Example : Input : Stack : 1 2 4 Push(5)Output : Stack : 1 2 4 5Input : Stack 3 min read Python Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes 4 min read Python Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read Python Program For Writing A Function To Delete A Linked List Algorithm For Python:In Python, automatic garbage collection happens, so deleting a linked list is easy. Just need to change head to null. Implementation: Python3 # Python3 program to delete all # the nodes of singly linked list # Node class class Node: # Function to initialize the # node object de 2 min read Like