Explorar el Código

fixed a minor change

Harsh Parikh hace 2 años
padre
commit
ef6829c5a5
Se han modificado 1 ficheros con 36 adiciones y 16 borrados
  1. 36 16
      data_structures/linked_list/practice_problems/linked_list.ipynb

+ 36 - 16
data_structures/linked_list/practice_problems/linked_list.ipynb

@@ -24,19 +24,39 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "class Node:\n",
+    "    def __init__(self, value):\n",
+    "        self.value = value\n",
+    "        self.next = None\n",
+    "\n",
+    "class LinkedList:\n",
+    "    def __init__(self):\n",
+    "        self.head = None\n",
+    "        self.tail = None\n",
+    "        self.next = None"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Adding an element inside a linked list"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
    "metadata": {},
    "outputs": [
     {
-     "ename": "AttributeError",
-     "evalue": "'NoneType' object has no attribute 'value'",
-     "output_type": "error",
-     "traceback": [
-      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
-      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
-      "\u001b[0;32m<ipython-input-4-75cdb50160b5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
-      "\u001b[0;32m<ipython-input-4-75cdb50160b5>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
-      "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'value'"
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1, 4, 3, 2, 0]\n"
      ]
     }
    ],
@@ -71,7 +91,7 @@
     "                self.head = new_node\n",
     "\n",
     "            elif location == -1:\n",
-    "                new_node.next = Node\n",
+    "                new_node.next = None\n",
     "                self.tail.next = new_node\n",
     "                self.tail = new_node\n",
     "\n",
@@ -90,12 +110,12 @@
     "\n",
     "\n",
     "linkedlist = LinkedList()\n",
-    "node_1 = Node(1)\n",
-    "node_2 = Node(2)\n",
+    "linkedlist.__insert__(1, 1)\n",
+    "linkedlist.__insert__(2, 1)\n",
+    "linkedlist.__insert__(3, 1)\n",
+    "linkedlist.__insert__(4, 1)\n",
+    "linkedlist.__insert__(0, 4)\n",
     "\n",
-    "linkedlist.head = node_1\n",
-    "linkedlist.head.next = node_2\n",
-    "linkedlist.tail = node_2\n",
     "print([node.value for node in linkedlist])"
    ]
   },