Refactored linked list class. Class is now loaded by other notebooks that reference it.

This commit is contained in:
Donne Martin
2015-05-13 06:39:16 -04:00
parent 6f3e3b0b8f
commit 4c1f87a2ae
3 changed files with 80 additions and 122 deletions

View File

@@ -19,8 +19,6 @@
"source": [
"## Clarifying Questions\n",
"\n",
"* Do we assume the linked list class already exists?\n",
" * No, create one\n",
"* What if the final node is being deleted, for example a single node list?\n",
" * Make it a dummy node"
]
@@ -61,6 +59,17 @@
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run linked_list.py"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -74,10 +83,7 @@
" self.data = data\n",
" self.next = None\n",
"\n",
"class LinkedList(object):\n",
" def __init__(self, head):\n",
" self.head = head\n",
" \n",
"class MyLinkedList(LinkedList):\n",
" def delete_node(self, node):\n",
" if self.head is None:\n",
" return\n",
@@ -89,25 +95,7 @@
" node.next = None\n",
" else:\n",
" node.data = next.data\n",
" node.next = next.next\n",
"\n",
" \n",
" def insert_to_front(self, data):\n",
" if data is None:\n",
" return\n",
" node = Node(data)\n",
" if self.head is None:\n",
" self.head = node\n",
" else:\n",
" node.next = self.head\n",
" self.head = node\n",
" return node\n",
" \n",
" def print_list(self):\n",
" curr_node = self.head\n",
" while curr_node is not None:\n",
" print(curr_node.data)\n",
" curr_node = curr_node.next"
" node.next = next.next"
]
},
{