Skip to content

Commit 31e1643

Browse files
committed
New four
1 parent 3a2d6d7 commit 31e1643

File tree

4 files changed

+697
-0
lines changed

4 files changed

+697
-0
lines changed

Queue.ipynb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"id": "47cb86f8",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"class Queue:\n",
11+
" def __init__(self):\n",
12+
" self.items = []\n",
13+
" \n",
14+
" def enqueue(self,item):\n",
15+
" self.items.append(item)\n",
16+
" \n",
17+
" def dequeue(self):\n",
18+
" return self.items.pop(0)\n",
19+
" \n",
20+
" def is_empty(self):\n",
21+
" if self.items == []:\n",
22+
" return True\n",
23+
" return False"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 8,
29+
"id": "59c03cd5",
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"name": "stdout",
34+
"output_type": "stream",
35+
"text": [
36+
"1\n",
37+
"2\n",
38+
"3\n"
39+
]
40+
}
41+
],
42+
"source": [
43+
"if __name__ == \"__main__\":\n",
44+
" q = Queue()\n",
45+
" q.enqueue(1)\n",
46+
" q.enqueue(2)\n",
47+
" q.enqueue(3) \n",
48+
" \n",
49+
" while not q.is_empty():\n",
50+
" item = q.dequeue()\n",
51+
" print(item)\n",
52+
" "
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "fa749177",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": []
62+
}
63+
],
64+
"metadata": {
65+
"kernelspec": {
66+
"display_name": "Python 3 (ipykernel)",
67+
"language": "python",
68+
"name": "python3"
69+
},
70+
"language_info": {
71+
"codemirror_mode": {
72+
"name": "ipython",
73+
"version": 3
74+
},
75+
"file_extension": ".py",
76+
"mimetype": "text/x-python",
77+
"name": "python",
78+
"nbconvert_exporter": "python",
79+
"pygments_lexer": "ipython3",
80+
"version": "3.9.7"
81+
}
82+
},
83+
"nbformat": 4,
84+
"nbformat_minor": 5
85+
}

SinglyLinkedList.ipynb

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "eda3e6f6",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"class Node:\n",
11+
" def __init__(self, data):\n",
12+
" self.data = data\n",
13+
" self.ref = None\n",
14+
" "
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"id": "8f79d536",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"class Linked_list:\n",
25+
" def __init__(self):\n",
26+
" self.head = None\n",
27+
" def print_list(self):\n",
28+
" if self.head is None:\n",
29+
" print(\"Empty list\")\n",
30+
" else:\n",
31+
" n = self.head\n",
32+
" while n is not None:\n",
33+
" print(n.data, \"--->\", end = \" \")\n",
34+
" n = n.ref\n",
35+
" \n",
36+
" def add_begin(self, data):\n",
37+
" new_node = Node(data)\n",
38+
" new_node.ref = self.head\n",
39+
" self.head = new_node\n",
40+
" \n",
41+
" def add_end(self,data):\n",
42+
" new_node = Node(data)\n",
43+
" if self.head is None:\n",
44+
" self.head = new_node\n",
45+
" else:\n",
46+
" n = self.head\n",
47+
" while n.ref is not None:\n",
48+
" n = n.ref\n",
49+
" n.ref = new_node\n",
50+
" \n",
51+
" def add_after(self,data,x):\n",
52+
" n = self.head\n",
53+
" while n is not None:\n",
54+
" if x == n.data:\n",
55+
" break\n",
56+
" n = n.ref\n",
57+
" if n is None:\n",
58+
" print(\"No data found\")\n",
59+
" else:\n",
60+
" new_node = Node(data)\n",
61+
" new_node.ref = n.ref\n",
62+
" n.ref = new_node\n",
63+
" \n",
64+
" def add_before(self,data,x):\n",
65+
" n = self.head\n",
66+
" while n.ref is not None:\n",
67+
" if n.ref.data == x:\n",
68+
" break\n",
69+
" n = n.ref\n",
70+
" if n.ref is None:\n",
71+
" print(\"No data before\")\n",
72+
" else:\n",
73+
" new_node = Node(data)\n",
74+
" new_node.ref = n.ref\n",
75+
" n.ref = new_node\n",
76+
" \n",
77+
" def remove_begin(self):\n",
78+
" if self.head is None:\n",
79+
" print(\"Nothing to delete\")\n",
80+
" else:\n",
81+
" self.head = self.head.ref\n",
82+
" \n",
83+
" def remove_end(self):\n",
84+
" if self.head is None:\n",
85+
" print(\"Can't delete\")\n",
86+
" elif self.head.ref is None:\n",
87+
" self.head = None\n",
88+
" else:\n",
89+
" n = self.head\n",
90+
" while n.ref.ref is not None:\n",
91+
" n = n.ref\n",
92+
" n.ref = None \n",
93+
" \n",
94+
" def remove_by_value(self,x):\n",
95+
" if self.head is None:\n",
96+
" print(\"Nothing to do\")\n",
97+
" elif x == self.head.data:\n",
98+
" self.head = self.head.ref\n",
99+
" else:\n",
100+
" n = self.head\n",
101+
" while n.ref is not None:\n",
102+
" if x == n.ref.data:\n",
103+
" break\n",
104+
" n = n.ref\n",
105+
" if n.ref is None:\n",
106+
" print(\"Node is not present\")\n",
107+
" else:\n",
108+
" n.ref = n.ref.ref\n",
109+
" \n",
110+
" "
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 3,
116+
"id": "0d2e3823",
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"70 ---> 100 ---> 50 ---> 90 ---> 300 ---> "
124+
]
125+
}
126+
],
127+
"source": [
128+
"LL1 = Linked_list()\n",
129+
"LL1.add_begin(100)\n",
130+
"LL1.add_begin(70)\n",
131+
"LL1.add_end(300)\n",
132+
"LL1.add_after(50,100)\n",
133+
"LL1.add_before(90,300)\n",
134+
"LL1.print_list()\n"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 5,
140+
"id": "35762703",
141+
"metadata": {},
142+
"outputs": [
143+
{
144+
"name": "stdout",
145+
"output_type": "stream",
146+
"text": [
147+
"100 ---> 50 ---> 90 ---> "
148+
]
149+
}
150+
],
151+
"source": [
152+
"LL1.remove_begin()\n",
153+
"LL1.print_list()"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 6,
159+
"id": "027504a4",
160+
"metadata": {},
161+
"outputs": [
162+
{
163+
"name": "stdout",
164+
"output_type": "stream",
165+
"text": [
166+
"60 ---> 100 ---> 50 ---> 90 ---> "
167+
]
168+
}
169+
],
170+
"source": [
171+
"LL1.add_begin(60)\n",
172+
"LL1.print_list()"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 7,
178+
"id": "3047aa1b",
179+
"metadata": {},
180+
"outputs": [
181+
{
182+
"name": "stdout",
183+
"output_type": "stream",
184+
"text": [
185+
"60 ---> 100 ---> 50 ---> "
186+
]
187+
}
188+
],
189+
"source": [
190+
"LL1.remove_end()\n",
191+
"LL1.print_list()"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 5,
197+
"id": "163b01cf",
198+
"metadata": {},
199+
"outputs": [
200+
{
201+
"name": "stdout",
202+
"output_type": "stream",
203+
"text": [
204+
"Node is not present\n",
205+
"70 ---> 50 ---> 90 ---> 300 ---> "
206+
]
207+
}
208+
],
209+
"source": [
210+
"LL1.remove_by_value(100)\n",
211+
"LL1.print_list()"
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": null,
217+
"id": "f5c57a60",
218+
"metadata": {},
219+
"outputs": [],
220+
"source": []
221+
}
222+
],
223+
"metadata": {
224+
"kernelspec": {
225+
"display_name": "Python 3 (ipykernel)",
226+
"language": "python",
227+
"name": "python3"
228+
},
229+
"language_info": {
230+
"codemirror_mode": {
231+
"name": "ipython",
232+
"version": 3
233+
},
234+
"file_extension": ".py",
235+
"mimetype": "text/x-python",
236+
"name": "python",
237+
"nbconvert_exporter": "python",
238+
"pygments_lexer": "ipython3",
239+
"version": "3.9.7"
240+
}
241+
},
242+
"nbformat": 4,
243+
"nbformat_minor": 5
244+
}

0 commit comments

Comments
 (0)