Skip to content

Commit d1faf5b

Browse files
committed
1st
1 parent 31e1643 commit d1faf5b

File tree

1 file changed

+305
-0
lines changed

1 file changed

+305
-0
lines changed

DoublyLinkedList.ipynb

+305
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 7,
6+
"id": "1626d253",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"class Node:\n",
11+
" def __init__(self,data):\n",
12+
" self.data = data \n",
13+
" self.prev = None\n",
14+
" self.next = None\n",
15+
" "
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 8,
21+
"id": "7cc8ade7",
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"class DoublyLL:\n",
26+
" def __init__(self):\n",
27+
" self.head = None\n",
28+
" \n",
29+
" def print_LL(self):\n",
30+
" if self.head is None:\n",
31+
" print(\"List is empty\")\n",
32+
" else:\n",
33+
" n = self.head\n",
34+
" while n is not None:\n",
35+
" print(n.data,\"--->\",end = \" \")\n",
36+
" n = n.next\n",
37+
" \n",
38+
" def print_LL_reverse(self):\n",
39+
" print()\n",
40+
" print()\n",
41+
" if self.head is None:\n",
42+
" print(\"List is empty\")\n",
43+
" else:\n",
44+
" n = self.head\n",
45+
" while n.next is not None:\n",
46+
" n = n.next\n",
47+
" while n is not None:\n",
48+
" print(n.data, \"<---\", end = \" \")\n",
49+
" n = n.prev\n",
50+
" \n",
51+
" def add_begin(self,data):\n",
52+
" new_node = Node(data)\n",
53+
" if self.head is None:\n",
54+
" self.head = new_node\n",
55+
" else:\n",
56+
" new_node.next = self.head\n",
57+
" self.head.prev = new_node\n",
58+
" self.head = new_node\n",
59+
" \n",
60+
" def add_end(self,data):\n",
61+
" new_node = Node(data)\n",
62+
" if self.head is None:\n",
63+
" self.head = new_node\n",
64+
" else:\n",
65+
" n = self.head\n",
66+
" while n.next is not None:\n",
67+
" n = n.next\n",
68+
" n.next = new_node\n",
69+
" new_node.prev = n\n",
70+
" \n",
71+
" def add_after(self,data,x):\n",
72+
" new_node = Node(data)\n",
73+
" if self.head is None:\n",
74+
" print(\"List is empty\")\n",
75+
" else:\n",
76+
" n = self.head\n",
77+
" while n is not None:\n",
78+
" if x == n.data:\n",
79+
" break\n",
80+
" n = n.next\n",
81+
" if n is None:\n",
82+
" print(\"x is not here\")\n",
83+
" else:\n",
84+
" new_node = Node(data)\n",
85+
" new_node.next = n.next\n",
86+
" new_node.prev = n\n",
87+
" if n.next is not None:\n",
88+
" n.next.prev = new_node\n",
89+
" n.next = new_node\n",
90+
" \n",
91+
" def add_before(self,data,x):\n",
92+
" new_node = Node(data)\n",
93+
" if self.head is None:\n",
94+
" print(\"List is empty\")\n",
95+
" else:\n",
96+
" n = self.head\n",
97+
" while n is not None:\n",
98+
" if x == n.data:\n",
99+
" break\n",
100+
" n = n.next\n",
101+
" if n is None:\n",
102+
" print(\"x is not here\")\n",
103+
" else:\n",
104+
" new_node = Node(data)\n",
105+
" new_node.next = n\n",
106+
" new_node.prev = n.prev\n",
107+
" if n.prev is not None:\n",
108+
" n.prev.next = new_node\n",
109+
" else:\n",
110+
" self.head = new_node\n",
111+
" n.prev = new_node \n",
112+
" \n",
113+
" def delete_begin(self):\n",
114+
" if self.head is None:\n",
115+
" (\"Empty list can't delete\")\n",
116+
" return\n",
117+
" if self.head.next is None:\n",
118+
" self.head = None\n",
119+
" print(\"Empty after delete\")\n",
120+
" return\n",
121+
" else:\n",
122+
" self.head = self.head.next\n",
123+
" self.head.prev = None\n",
124+
" return\n",
125+
" \n",
126+
" def delete_end(self):\n",
127+
" if self.head is None:\n",
128+
" (\"Empty list can't delete\")\n",
129+
" return\n",
130+
" if self.head.next is None:\n",
131+
" self.head = None\n",
132+
" print(\"Empty after deleting\")\n",
133+
" else:\n",
134+
" n = self.head\n",
135+
" while n.next is not None:\n",
136+
" n = n.next\n",
137+
" n.prev.next = None\n",
138+
" return\n",
139+
" \n",
140+
" def delete_by_value(self,x):\n",
141+
" if self.head is None:\n",
142+
" print(\"Empty list can't delete\")\n",
143+
" if self.head.next is None:\n",
144+
" if x == self.head.data:\n",
145+
" self.head == None\n",
146+
" print(\"After deleting list is empty\")\n",
147+
" return\n",
148+
" if x == self.head.data:\n",
149+
" self.head = self.head.next\n",
150+
" self.head.prev = None\n",
151+
" return\n",
152+
" n = self.head\n",
153+
" while n.next is not None:\n",
154+
" if x == n.data:\n",
155+
" break\n",
156+
" n = n.next\n",
157+
" if n.next is not None:\n",
158+
" n.next.prev = n.prev\n",
159+
" n.prev.next = n.next\n",
160+
" else:\n",
161+
" if x == n.data:\n",
162+
" n.prev.next = None\n",
163+
" else:\n",
164+
" print(\"x is not present\")\n",
165+
" return\n",
166+
" \n",
167+
" \n",
168+
" \n",
169+
" \n",
170+
" "
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 9,
176+
"id": "a2654a03",
177+
"metadata": {},
178+
"outputs": [
179+
{
180+
"name": "stdout",
181+
"output_type": "stream",
182+
"text": [
183+
"24 ---> 40 ---> "
184+
]
185+
}
186+
],
187+
"source": [
188+
"DBLT = DoublyLL()\n",
189+
"DBLT.add_begin(24)\n",
190+
"DBLT.add_end(40)\n",
191+
"DBLT.print_LL()\n",
192+
"#DBLT.print_LL_reverse()"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 10,
198+
"id": "6856f7b2",
199+
"metadata": {},
200+
"outputs": [],
201+
"source": [
202+
"DBLT.add_begin(50)\n",
203+
"DBLT.add_end(70)"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 14,
209+
"id": "93316423",
210+
"metadata": {},
211+
"outputs": [
212+
{
213+
"name": "stdout",
214+
"output_type": "stream",
215+
"text": [
216+
"x is not here\n",
217+
"24 ---> 40 ---> 66 ---> 66 ---> 70 ---> \n",
218+
"\n",
219+
"70 <--- 66 <--- 66 <--- 40 <--- 24 <--- "
220+
]
221+
}
222+
],
223+
"source": [
224+
"DBLT.add_after(90,50)\n",
225+
"DBLT.add_before(66,70)\n",
226+
"DBLT.print_LL()\n",
227+
"DBLT.print_LL_reverse()\n"
228+
]
229+
},
230+
{
231+
"cell_type": "code",
232+
"execution_count": 38,
233+
"id": "93f1ff49",
234+
"metadata": {},
235+
"outputs": [
236+
{
237+
"name": "stdout",
238+
"output_type": "stream",
239+
"text": [
240+
"50 ---> 90 ---> 24 ---> 40 ---> 66 ---> \n",
241+
"\n",
242+
"66 <--- 40 <--- 24 <--- 90 <--- 50 <--- "
243+
]
244+
}
245+
],
246+
"source": [
247+
"#DBLT.delete_begin()\n",
248+
"DBLT.delete_end()\n",
249+
"DBLT.print_LL()\n",
250+
"DBLT.print_LL_reverse()"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": 13,
256+
"id": "222f5fc6",
257+
"metadata": {},
258+
"outputs": [
259+
{
260+
"name": "stdout",
261+
"output_type": "stream",
262+
"text": [
263+
"24 ---> 40 ---> 66 ---> 70 ---> \n",
264+
"\n",
265+
"70 <--- 66 <--- 40 <--- 24 <--- "
266+
]
267+
}
268+
],
269+
"source": [
270+
"DBLT.delete_by_value(70)\n",
271+
"DBLT.print_LL()\n",
272+
"DBLT.print_LL_reverse()"
273+
]
274+
},
275+
{
276+
"cell_type": "code",
277+
"execution_count": null,
278+
"id": "9bd62e84",
279+
"metadata": {},
280+
"outputs": [],
281+
"source": []
282+
}
283+
],
284+
"metadata": {
285+
"kernelspec": {
286+
"display_name": "Python 3 (ipykernel)",
287+
"language": "python",
288+
"name": "python3"
289+
},
290+
"language_info": {
291+
"codemirror_mode": {
292+
"name": "ipython",
293+
"version": 3
294+
},
295+
"file_extension": ".py",
296+
"mimetype": "text/x-python",
297+
"name": "python",
298+
"nbconvert_exporter": "python",
299+
"pygments_lexer": "ipython3",
300+
"version": "3.9.7"
301+
}
302+
},
303+
"nbformat": 4,
304+
"nbformat_minor": 5
305+
}

0 commit comments

Comments
 (0)