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

@@ -20,13 +20,11 @@
"source": [
"## Clarifying Questions\n",
"\n",
"* Do we assume the linked list class already exists?\n",
" * No, create one\n",
"* Is this a singly or doubly linked list?\n",
" * Singly\n",
"* Can you insert NULL values in the list?\n",
" * No\n",
"* Can use additional data structures?\n",
"* Can you use additional data structures?\n",
" * Implement both solutions"
]
},
@@ -70,6 +68,17 @@
"## Code: Hash Map Lookup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run linked_list.py"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -78,15 +87,7 @@
},
"outputs": [],
"source": [
"class Node(object):\n",
" def __init__(self, data):\n",
" self.next = None\n",
" self.data = data\n",
"\n",
"class LinkedList(object):\n",
" def __init__(self, head):\n",
" self.head = head\n",
" \n",
"class MyLinkedList(LinkedList):\n",
" def remove_dupes(self):\n",
" seen_data = set()\n",
" curr = self.head\n",
@@ -97,31 +98,24 @@
" else:\n",
" seen_data.add(curr.data)\n",
" prev = curr\n",
" curr = curr.next\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",
" \n",
" def print_list(self):\n",
" curr = self.head\n",
" while curr is not None:\n",
" print(curr.data)\n",
" curr = curr.next\n",
" \n",
" curr = curr.next"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Empty linked list\n",
"linked_list = LinkedList(None)\n",
"linked_list = MyLinkedList(None)\n",
"linked_list.remove_dupes()\n",
"linked_list.print_list()\n",
"# One element linked list\n",
"head = Node(2)\n",
"linked_list = LinkedList(head)\n",
"linked_list = MyLinkedList(head)\n",
"linked_list.remove_dupes()\n",
"linked_list.print_list()\n",
"# Multiple elements\n",
@@ -174,14 +168,7 @@
},
"outputs": [],
"source": [
"class Node(object):\n",
" def __init__(self, data):\n",
" self.next = None\n",
" self.data = data\n",
"\n",
"class LinkedList(object):\n",
" def __init__(self, head):\n",
" self.head = head\n",
"class MyLinkedList(LinkedList):\n",
" \n",
" def remove_dupes(self):\n",
" curr = self.head\n",
@@ -192,31 +179,24 @@
" runner.next = runner.next.next\n",
" else:\n",
" runner = runner.next\n",
" curr = curr.next\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",
" \n",
" def print_list(self):\n",
" curr = self.head\n",
" while curr is not None:\n",
" print(curr.data)\n",
" curr = curr.next\n",
" \n",
" curr = curr.next"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Empty linked list\n",
"linked_list = LinkedList(None)\n",
"linked_list = MyLinkedList(None)\n",
"linked_list.remove_dupes()\n",
"linked_list.print_list()\n",
"# One element linked list\n",
"head = Node(2)\n",
"linked_list = LinkedList(head)\n",
"linked_list = MyLinkedList(head)\n",
"linked_list.remove_dupes()\n",
"linked_list.print_list()\n",
"# Multiple elements\n",