├── README.md ├── .gitignore ├── LinkedList ├── main.py └── linked_list.py └── List.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # DataStructures-python -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | .virtual_documents -------------------------------------------------------------------------------- /LinkedList/main.py: -------------------------------------------------------------------------------- 1 | from linked_list import LinkedList 2 | 3 | ll = LinkedList() 4 | ll.append(10) 5 | ll.append(20) 6 | ll.append(30) 7 | ll.append(40) 8 | ll.delete_node(30) 9 | ll.prepend(30) 10 | ll.print_list() -------------------------------------------------------------------------------- /LinkedList/linked_list.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class LinkedList: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def append(self, data): 12 | new_node = Node(data) 13 | 14 | if not self.head: 15 | self.head = new_node 16 | return 17 | 18 | current_node = self.head 19 | while current_node.next: 20 | current_node = current_node.next 21 | current_node.next = new_node 22 | 23 | def prepend(self, data): 24 | if not self.head: 25 | self.head = Node(data) 26 | return 27 | 28 | new_node = Node(data) 29 | new_node.next = self.head 30 | self.head = new_node 31 | 32 | def insert_after_node(self, prev_node, data): 33 | if not prev_node.next: 34 | prev_node.next = Node(data) 35 | return 36 | 37 | new_node = Node(data) 38 | new_node.next = prev_node.next 39 | prev_node.next = new_node 40 | 41 | def delete_node(self, key): 42 | if not self.head: 43 | return False 44 | 45 | if self.head.data == key: 46 | 47 | self.head = self.head.next 48 | return True 49 | 50 | current_node = self.head 51 | while current_node.next: 52 | if current_node.next.data == key: 53 | current_node.next = current_node.next.next 54 | return True 55 | current_node = current_node.next 56 | return False 57 | 58 | def delete_at_pos(self, pos): 59 | return 60 | 61 | def search(self, key): 62 | return 63 | 64 | def get_length(self): 65 | return 66 | 67 | def reverse(self): 68 | return 69 | 70 | def print_list(self): 71 | current_node = self.head 72 | 73 | while current_node: 74 | print(current_node.data, end=" -> ") 75 | current_node = current_node.next 76 | print("None") 77 | -------------------------------------------------------------------------------- /List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "fd2c6fed-f133-4e10-9144-e3adc8db5a7c", 6 | "metadata": {}, 7 | "source": [ 8 | "# Python Lists" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "c42331d8-5867-4ce0-9b71-6b1c30abce9e", 14 | "metadata": {}, 15 | "source": [ 16 | "**Lists are used to store multiple items in a single variable. A list can contain different data types.**" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "732f2086-da96-4153-88c6-eaced6c2d396", 22 | "metadata": {}, 23 | "source": [ 24 | "- items are ordered\n", 25 | "- items are indexed (starts with 0)\n", 26 | "- items are changeable\n", 27 | "- allow duplicates" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "id": "829143ee-a8ad-4ecc-9816-30cfe782f9b7", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "[10, 2.2, 3, 'Wasik', True]\n", 41 | "\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "# Lists are created using square brackets\n", 47 | "my_list = [10, 2.2, 3, 'Wasik', True]\n", 48 | "print(my_list)\n", 49 | "\n", 50 | "print(type(my_list)) # type of my_list\n", 51 | "# lists are defined as objects with the data type 'list'" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "id": "3f321043-3fd3-4764-ba9d-ba76192fd69b", 57 | "metadata": { 58 | "jp-MarkdownHeadingCollapsed": true 59 | }, 60 | "source": [ 61 | "**Creating list using `list()` constructor**" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 2, 67 | "id": "06ba172e-1527-4dce-9635-eb299ed3183a", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "[1, 2, 3, 4, 5]\n", 75 | "\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "my_list2 = list((1, 2, 3, 4, 5))\n", 81 | "print(my_list2)\n", 82 | "print(type(my_list2))" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "id": "dffa34b6-97f5-49a2-a5f6-bf8a8f12663f", 88 | "metadata": {}, 89 | "source": [ 90 | "### Accessing items" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 3, 96 | "id": "14f8d73c-956b-4fa4-85d0-ce73a798ad9c", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "[10, 20, [30, 40, 50], 60, 70, 80, 90, 100]\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "list = [10, 20, [30, 40, 50], 60, 70, 80, 90, 100]\n", 109 | "print(list)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "id": "0ab42be5-45a7-4826-a722-2e707025b4ad", 115 | "metadata": {}, 116 | "source": [ 117 | "**refering to index number**" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 4, 123 | "id": "72139810-d991-477c-abb3-d185da96db02", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "10\n", 131 | "40\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "print(list[0])\n", 137 | "print(list[2][1])" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "ddfa8927-51c6-4b74-813e-3a0ae14cf4a2", 143 | "metadata": {}, 144 | "source": [ 145 | "**negative indexing** (start from the end)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 5, 151 | "id": "9a48cf70-2023-4a84-97bc-df3c80920e50", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "100\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "print(list[-1])\n", 164 | "# negative indexing starts from -1" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "id": "f658705c-5c70-4a8d-9826-0e091db1139c", 170 | "metadata": {}, 171 | "source": [ 172 | "**index range**" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 6, 178 | "id": "332de599-4ee7-402b-8b81-49b06347da1e", 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "[20, [30, 40, 50], 60, 70]\n", 186 | "[10, 20]\n", 187 | "[[30, 40, 50], 60, 70, 80, 90, 100]\n", 188 | "[80, 90]\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "print(list[1:5]) # starts from index 1 and ends at index 4\n", 194 | "print(list[:2]) # strats from index 0 and ends at index 1\n", 195 | "print(list[2:]) # starts from index 2 and ends at the last index\n", 196 | "print(list[-3:-1]) # starts from index -3 and ends at index -2 (negative indexing)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "id": "e43eebe3-d307-482a-9474-883f645f1c0c", 202 | "metadata": {}, 203 | "source": [ 204 | "### Adding items" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 7, 210 | "id": "a09fd262-ea8a-4a06-a961-a13e2db66ae7", 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "['w', 'a', 's', 'i']\n" 218 | ] 219 | } 220 | ], 221 | "source": [ 222 | "list = ['w', 'a', 's', 'i']\n", 223 | "print(list)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "id": "76c7c636-dc1c-4b2a-ba75-5f0bccb8c707", 229 | "metadata": {}, 230 | "source": [ 231 | "**using `append()` method** (adds an item to end of the list)" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 8, 237 | "id": "3c8349ac-f5b6-4060-9629-93231d3c532f", 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "['w', 'a', 's', 'i', 'k']\n" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "list.append('k')\n", 250 | "print(list)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "id": "609e3083-2f3c-46dd-8f09-6b860df6dbba", 256 | "metadata": {}, 257 | "source": [ 258 | "**using `insert()` method** (inserts an item at a specified index)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 9, 264 | "id": "edbc70c5-4ed2-4fe8-8a5b-29c9874ed950", 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "['My name is', 'w', 'a', 's', 'i', 'k']\n" 272 | ] 273 | } 274 | ], 275 | "source": [ 276 | "list.insert(0, \"My name is\") # inserts at index 0. Pushes other items to the right\n", 277 | "print(list)" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "id": "759505fb-a1f5-444a-ac9c-6b7eb0e161d4", 283 | "metadata": {}, 284 | "source": [ 285 | "**using `extend()` method** (append items from another list)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 10, 291 | "id": "8e2b8579-16ba-49cc-97c6-8c5ba647a2af", 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "['I', 'Love', 'c', 'o', 'd', 'i', 'n', 'g']\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "second_list = ['I', 'Love', 'c', 'o', 'd', 'i', 'n', 'g']\n", 304 | "print(second_list)" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 11, 310 | "id": "0e77538f-c022-4497-9b28-87dd8d277577", 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "name": "stdout", 315 | "output_type": "stream", 316 | "text": [ 317 | "['My name is', 'w', 'a', 's', 'i', 'k', 'I', 'Love', 'c', 'o', 'd', 'i', 'n', 'g']\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "list.extend(second_list) # extending second_list at the end of list\n", 323 | "print(list)" 324 | ] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "id": "abb43327-08fa-4245-87e1-e35d2aa4bc50", 329 | "metadata": {}, 330 | "source": [ 331 | "### Removing elements" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 12, 337 | "id": "8b09b899-9e94-4146-893d-e52ab36021f4", 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "name": "stdout", 342 | "output_type": "stream", 343 | "text": [ 344 | "[10, 20, 30, 40, 'a', 'p', 'o', 'n']\n" 345 | ] 346 | } 347 | ], 348 | "source": [ 349 | "mylist = [10, 20, 30, 40, 'a', 'p', 'o', 'n']\n", 350 | "print(mylist)" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "id": "05360930-9ebe-4a30-9f93-4c4d350138e5", 356 | "metadata": {}, 357 | "source": [ 358 | "**`remove()` method is used to remove an specified element**" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 13, 364 | "id": "2c0e179f-fccb-43c7-ae68-b16383065f8d", 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "name": "stdout", 369 | "output_type": "stream", 370 | "text": [ 371 | "[20, 30, 40, 'a', 'p', 'o', 'n']\n" 372 | ] 373 | } 374 | ], 375 | "source": [ 376 | "mylist.remove(10)\n", 377 | "print(mylist)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "id": "c6d2e478-74b0-4220-80f5-0d202817ff3e", 383 | "metadata": {}, 384 | "source": [ 385 | "**`pop()` method is used to remove element at specified index**" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 14, 391 | "id": "e3eb9e2c-1cac-4441-b6b2-dc0e61d1ad14", 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "[30, 40, 'a', 'p', 'o', 'n']\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "mylist.pop(0)\n", 404 | "print(mylist)" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "id": "25df271f-60a4-408d-a44b-b31821e7df30", 410 | "metadata": {}, 411 | "source": [ 412 | "**`clear()` method removes all elements in the list**" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 15, 418 | "id": "ac7044ea-53ac-40dd-99a0-b06d1b5f56ef", 419 | "metadata": {}, 420 | "outputs": [ 421 | { 422 | "name": "stdout", 423 | "output_type": "stream", 424 | "text": [ 425 | "[]\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "mylist.clear()\n", 431 | "print(mylist)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "id": "1acfd0e8-a626-454d-951f-5396bb32b0b0", 437 | "metadata": {}, 438 | "source": [ 439 | "**using `del` keyword**" 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 16, 445 | "id": "71a93569-0d64-4eba-867e-bb78bd601f37", 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "name": "stdout", 450 | "output_type": "stream", 451 | "text": [ 452 | "[10, 20, 30, 40, 50]\n", 453 | "[20, 30, 40, 50]\n" 454 | ] 455 | } 456 | ], 457 | "source": [ 458 | "mylist = [10, 20, 30, 40, 50]\n", 459 | "print(mylist)\n", 460 | "del mylist[0] # deleting the specified index\n", 461 | "print(mylist)" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 17, 467 | "id": "8f8ae24a-7af7-4f36-862b-37c569e743c6", 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "ename": "NameError", 472 | "evalue": "name 'mylist' is not defined", 473 | "output_type": "error", 474 | "traceback": [ 475 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 476 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 477 | "Cell \u001b[0;32mIn[17], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m mylist \u001b[38;5;66;03m# deleting the list \u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# it deletes the list instead of removing all elements in the list. So the next line will result in an error.\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmylist\u001b[49m)\n", 478 | "\u001b[0;31mNameError\u001b[0m: name 'mylist' is not defined" 479 | ] 480 | } 481 | ], 482 | "source": [ 483 | "del mylist # deleting the list \n", 484 | "# it deletes the list instead of removing all elements in the list. So the next line will result in an error.\n", 485 | "print(mylist)" 486 | ] 487 | } 488 | ], 489 | "metadata": { 490 | "kernelspec": { 491 | "display_name": "Python 3 (ipykernel)", 492 | "language": "python", 493 | "name": "python3" 494 | }, 495 | "language_info": { 496 | "codemirror_mode": { 497 | "name": "ipython", 498 | "version": 3 499 | }, 500 | "file_extension": ".py", 501 | "mimetype": "text/x-python", 502 | "name": "python", 503 | "nbconvert_exporter": "python", 504 | "pygments_lexer": "ipython3", 505 | "version": "3.12.1" 506 | } 507 | }, 508 | "nbformat": 4, 509 | "nbformat_minor": 5 510 | } 511 | --------------------------------------------------------------------------------