📖 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.
Practice
(1/5)
1. What is a dequeue (double-ended queue) implemented using a linked list?
easy
A. A queue where elements can only be added at the rear
B. A list that only allows insertion at the front
C. A stack implemented using arrays
D. A data structure where elements can be added or removed from both ends
Solution
Step 1: Understand dequeue operations
A dequeue allows insertion and deletion from both front and rear ends.
Step 2: Linked list implementation
Using a linked list, nodes can be added or removed from both ends efficiently.
Final Answer:
A data structure where elements can be added or removed from both ends -> Option D
Quick Check:
Dequeue = double-ended queue [OK]
Hint: Dequeue means double-ended queue, both ends allowed [OK]
Common Mistakes:
Confusing dequeue with stack or simple queue
Thinking insertion is only at one end
Assuming array implementation only
2. Which of the following is the correct way to initialize an empty dequeue using a linked list in Python?
easy
A. class Dequeue: def __init__(self): self.front = None; self.rear = None
B. class Dequeue: def __init__(self): self.list = []
C. class Dequeue: def __init__(self): self.head = 0; self.tail = 0
D. class Dequeue: def __init__(self): self.front = 0; self.rear = 0
Solution
Step 1: Identify linked list pointers
For linked list, front and rear pointers are initialized to None to indicate empty.
Step 2: Check options
class Dequeue: def __init__(self): self.front = None; self.rear = None correctly initializes front and rear as None; others use incorrect types or structures.
Final Answer:
class Dequeue: def __init__(self): self.front = None; self.rear = None -> Option A
Quick Check:
Linked list empty pointers = None [OK]
Hint: Empty linked list pointers are None, not zero or list [OK]
Common Mistakes:
Using list instead of linked list
Initializing pointers with 0 instead of None
Confusing front/rear with indices
3. What will be the output after performing these operations on an empty dequeue implemented with linked list? dq.insert_rear(10) dq.insert_front(20) dq.insert_rear(30) dq.delete_front() Print the dequeue from front to rear.
Hint: Track each operation stepwise front to rear [OK]
Common Mistakes:
Forgetting delete_front removes front element
Mixing order of insert_front and insert_rear
Printing rear to front instead of front to rear
4. Identify the error in this dequeue linked list method for deleting from rear:
def delete_rear(self):
if self.rear is None:
return None
temp = self.rear
self.rear = self.rear.next
return temp.data
medium
A. Does not check if dequeue is empty
B. Incorrectly moves rear pointer forward instead of backward
C. Returns data before deleting node
D. Uses wrong variable name for rear
Solution
Step 1: Understand rear pointer movement
In linked list, rear points to last node; to delete rear, rear must move backward (to previous node).
Step 2: Analyze code
Code moves rear to rear.next (forward), which is incorrect for rear deletion.
Final Answer:
Incorrectly moves rear pointer forward instead of backward -> Option B
Quick Check:
Rear deletion moves pointer backward [OK]
Hint: Rear deletion moves pointer backward, not forward [OK]
Common Mistakes:
Moving rear pointer forward instead of backward
Not handling empty dequeue properly
Confusing front and rear pointers
5. You want to implement a dequeue using a doubly linked list to support these operations efficiently: 1. Insert at front 2. Insert at rear 3. Delete from front 4. Delete from rear
Which of the following is the best approach to maintain pointers for O(1) time complexity for all operations?
hard
A. Maintain front and rear pointers; each node has next and prev links
B. Maintain only front pointer; traverse list for rear operations
C. Use singly linked list with only next pointers and rear pointer
D. Use array to simulate linked list with indices
Solution
Step 1: Understand operation requirements
All operations must be O(1), so no traversal allowed.
Step 2: Analyze pointer needs
Doubly linked list with front and rear pointers allows insert/delete at both ends in O(1).
Step 3: Check options
Maintain front and rear pointers; each node has next and prev links matches this approach; others require traversal or use arrays which are not linked lists.
Final Answer:
Maintain front and rear pointers; each node has next and prev links -> Option A
Quick Check:
Doubly linked list + front/rear pointers = O(1) ops [OK]
Hint: Use doubly linked list with front and rear pointers [OK]
Common Mistakes:
Using singly linked list causing O(n) rear operations
Maintaining only one pointer causing traversal
Confusing arrays with linked lists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.