You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 03_data_structures/02_linked_list/02_linked_list/02_doubly_linked_list/readme.md
+272Lines changed: 272 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -112,3 +112,275 @@ class DoublyLinkedList:
112
112
113
113
```
114
114
---
115
+
116
+
# **Inserting a Node at the Beginning of a Doubly Linked List** ➕
117
+
118
+
In a **doubly linked list**, each node holds:
119
+
120
+
***`data`**
121
+
***`next`** pointer → next node
122
+
***`prev`** pointer → previous node
123
+
124
+
We maintain two pointers in the list object:
125
+
126
+
***`head`** → first node
127
+
***`tail`** → last node
128
+
***`count`** → number of nodes
129
+
130
+
Below, we’ll walk through **three key figures** illustrating insertion into an **empty list** and **non-empty list**, then present and explain the Python code, and finally show a usage example with output.
131
+
132
+
## 🖼️ Figures & Their Explanations
133
+
134
+
*(Carousel: Figures 4.20, 4.21, and 4.22)*
135
+
136
+
1.**Figure 4.20: Inserting into an *Empty* List**
137
+
138
+
<divalign="center">
139
+
<imgsrc="./images/03.jpg"alt=""width="400px"/>
140
+
</div>
141
+
142
+
* Both **`head`** and **`tail`** are `None` → list is empty.
143
+
* When we create a single `new_node`, we set:
144
+
145
+
```plaintext
146
+
head → new_node ← tail
147
+
new_node.next = None
148
+
new_node.prev = None
149
+
```
150
+
* Now both pointers reference the same sole node.
151
+
152
+
1. **Figure 4.21: Inserting into a *Non-Empty* List**
153
+
154
+
<div align="center">
155
+
<img src="./images/04.jpg" alt="" width="500px"/>
156
+
</div>
157
+
158
+
* Initial list: A ⇄ B ⇄ C
159
+
160
+
```plaintext
161
+
head → A ⇄ B ⇄ C ← tail
162
+
```
163
+
* We’ll insert `new_node` before A (at the start).
164
+
165
+
1. **Figure 4.22: Step-by-Step Link Updates**
166
+
The three dotted arrows show the order of pointer updates when inserting at the front:
167
+
168
+
<div align="center">
169
+
<img src="./images/05.jpg" alt="" width="500px"/>
170
+
</div>
171
+
172
+
1. **`new_node.next = head`**
173
+
174
+
* Link the new node **forward** to the old head.
175
+
2. **`head.prev = new_node`**
176
+
177
+
* Link the old head **backward** to the new node.
178
+
3. **`head = new_node`**
179
+
180
+
* Update the list’s head pointer to the new node.
181
+
182
+
## 📝 Python Code: `append at end or append_at_start`
183
+
184
+
```python
185
+
class DoublyLinkedList:
186
+
def __init__(self):
187
+
self.head = None # First node
188
+
self.tail = None # Last node
189
+
self.count = 0 # Number of nodes
190
+
191
+
def append(self, data):
192
+
# Append an item at the end of the list.
193
+
new_node = Node(data, None, None)
194
+
if self.head is None:
195
+
self.head = new_node
196
+
self.tail = new_node
197
+
else:
198
+
new_node.prev = self.tail
199
+
self.tail.next = new_node
200
+
self.tail = new_node
201
+
self.count += 1
202
+
203
+
204
+
def append_at_start(self, data):
205
+
"""
206
+
Insert a new node containing `data` at the beginning of the list.
207
+
Runs in O(1) time.
208
+
"""
209
+
# 1️⃣ Create the new standalone node
210
+
new_node = Node(data, next=None, prev=None)
211
+
212
+
# 2️⃣ Empty list? Initialize both head and tail.
213
+
if self.head is None:
214
+
self.head = new_node
215
+
self.tail = new_node
216
+
217
+
# 3️⃣ Non-empty list? Insert before current head.
218
+
else:
219
+
new_node.next = self.head # ➡️ Link forward to old head
220
+
self.head.prev = new_node # ⬅️ Link old head backward to new_node
221
+
self.head = new_node # 🔄 Update head to new_node
222
+
223
+
# 4️⃣ Maintain node count
224
+
self.count += 1
225
+
```
226
+
227
+
# Doubly Linked List Operations
228
+
229
+
This document provides an in-depth guide to building and manipulating a **Doubly Linked List** in Python. We cover:
230
+
231
+
1.**Node Definition**
232
+
2.**Figures** illustrating insertion scenarios
233
+
3.**Append at Start** (Insert at Head) — detailed breakdown
234
+
4.**Append at End** (Insert at Tail) — detailed breakdown
235
+
5.**Usage Example**
236
+
237
+
## 1. Node Definition
238
+
239
+
Each node in a doubly linked list must store:
240
+
241
+
***`data`**: The payload or value.
242
+
***`next`**: A reference to the **next** node in the list (or `None`).
243
+
***`prev`**: A reference to the **previous** node in the list (or `None`).
0 commit comments