|
| 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