0
0
DSA Pythonprogramming~30 mins

Dequeue Using Linked List in DSA Python - Build from Scratch

Choose your learning style9 modes available
Dequeue Using Linked List
📖 Scenario: Imagine you are managing a line of people waiting for a ride. Sometimes people join the line at the front, sometimes at the back. Also, people can leave from either end. This is like a double-ended queue, or dequeue, which we will build using a linked list.
🎯 Goal: You will create a dequeue data structure using a linked list. You will add people to the front and back, remove people from the front and back, and finally show the current line.
📋 What You'll Learn
Create a Node class to hold data and a pointer to the next node
Create a Dequeue class with methods to add to front and back
Create methods to remove from front and back
Print the current state of the dequeue after operations
💡 Why This Matters
🌍 Real World
Dequeue is used in real-world scenarios like task scheduling, undo operations in editors, and managing buffers where insertion and deletion happen at both ends.
💼 Career
Understanding dequeue implementation helps in software development roles that require efficient data handling, such as backend development, systems programming, and algorithm design.
Progress0 / 4 steps
1
Create Node and Dequeue Classes
Create a class called Node with an __init__ method that takes data and sets self.data = data and self.next = None. Then create a class called Dequeue with an __init__ method that sets self.front = None and self.rear = None.
DSA Python
Hint

Think of Node as a box holding a person and a pointer to the next box. The Dequeue keeps track of the first and last boxes.

2
Add Methods to Insert at Front and Rear
In the Dequeue class, add a method add_front(self, data) that creates a new Node with data and adds it to the front. Also add a method add_rear(self, data) that adds a new Node to the rear. Handle the case when the dequeue is empty by setting both front and rear to the new node.
DSA Python
Hint

When adding to front, point new node's next to current front. When adding to rear, point current rear's next to new node.

3
Add Methods to Remove from Front and Rear
In the Dequeue class, add a method remove_front(self) that removes the node from the front and returns its data. Also add a method remove_rear(self) that removes the node from the rear and returns its data. Handle empty dequeue by returning None. When removing the rear, traverse the list to find the node before rear.
DSA Python
Hint

To remove from front, move front pointer to next node. To remove from rear, find the node before rear and update rear pointer.

4
Print the Current Dequeue
Add a method print_dequeue(self) in the Dequeue class that prints the data of all nodes from front to rear separated by -> and ending with null. Then create a Dequeue object, add 10 to front, add 20 to rear, add 5 to front, remove one from rear, and finally print the dequeue.
DSA Python
Hint

Traverse from front to rear, collect data, and print with arrows. Follow the steps exactly to get the final output.