Added linked list delete method.

This commit is contained in:
Donne Martin 2015-05-08 16:49:33 -04:00
parent e8060f4b6e
commit 24cd55b9aa

View File

@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement a linked list with insert, find, delete, and print methods.\n",
"## Problem: Implement a linked list with insert, find, delete, and print methods\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
@ -50,7 +50,10 @@
"\n",
"### Delete\n",
"\n",
"* \n",
"* Delete a NULL\n",
"* Delete in an empty list\n",
"* Delete in a list with one element or more matching elements\n",
"* Delete in a list with no matches\n",
"\n",
"### Print\n",
"\n",
@ -92,7 +95,13 @@
"\n",
"### Delete\n",
"\n",
"* Coming soon\n",
"* If data we are deleting is NULL, return\n",
"* If the list is empty, return\n",
"* For each node, keep track of previous and current node\n",
" * If the value we are deleting is a match in the current node\n",
" * Update previous node's next pointer to the current node's next pointer\n",
" * We do not have have to explicitly delete in Python\n",
" * Else, move on to the next node\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
@ -156,6 +165,21 @@
" return curr_node\n",
" else:\n",
" curr_node = curr_node.next\n",
" \n",
" def delete(self, data):\n",
" if data is None:\n",
" return\n",
" if self.head is None:\n",
" return\n",
" prev_node = self.head\n",
" curr_node = prev_node.next\n",
" while curr_node is not None:\n",
" if curr_node.data == data:\n",
" prev_node.next = curr_node.next\n",
" return\n",
" else:\n",
" prev_node = curr_node\n",
" curr_node = curr_node.next\n",
"\n",
" def print_list(self):\n",
" curr_node = self.head\n",
@ -195,13 +219,13 @@
"outputs": [],
"source": [
"# Test find\n",
"# Find a NULL\n",
"# Find in an empty list\n",
"linked_list = LinkedList(None)\n",
"node = linked_list.find('a')\n",
"print(node)\n",
"# Find in an empty list\n",
"# Find a NULL\n",
"head = Node(10)\n",
"linked_list = LinkedList(None)\n",
"linked_list = LinkedList(head)\n",
"node = linked_list.find(None)\n",
"print(node)\n",
"# Find in a list with one element or more matching elements\n",
@ -216,6 +240,35 @@
"print(node)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Test delete\n",
"# Delete in an empty list\n",
"linked_list = LinkedList(None)\n",
"linked_list.delete('a')\n",
"linked_list.print_list()\n",
"# Delete a NULL\n",
"head = Node(10)\n",
"linked_list = LinkedList(head)\n",
"linked_list.delete(None)\n",
"linked_list.print_list()\n",
"# Delete in a list with one element or more matching elements\n",
"head = Node(10)\n",
"linked_list = LinkedList(head)\n",
"linked_list.insert_to_front('a')\n",
"linked_list.insert_to_front('bc')\n",
"linked_list.delete('a')\n",
"linked_list.print_list()\n",
"# Delete in a list with no matches\n",
"linked_list.delete('aa')"
]
},
{
"cell_type": "code",
"execution_count": null,