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", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "\n",
"* [Clarifying Questions](#Clarifying-Questions)\n", "* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@ -50,7 +50,10 @@
"\n", "\n",
"### Delete\n", "### Delete\n",
"\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", "\n",
"### Print\n", "### Print\n",
"\n", "\n",
@ -92,7 +95,13 @@
"\n", "\n",
"### Delete\n", "### Delete\n",
"\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", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
@ -156,6 +165,21 @@
" return curr_node\n", " return curr_node\n",
" else:\n", " else:\n",
" curr_node = curr_node.next\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", "\n",
" def print_list(self):\n", " def print_list(self):\n",
" curr_node = self.head\n", " curr_node = self.head\n",
@ -195,13 +219,13 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Test find\n", "# Test find\n",
"# Find a NULL\n", "# Find in an empty list\n",
"linked_list = LinkedList(None)\n", "linked_list = LinkedList(None)\n",
"node = linked_list.find('a')\n", "node = linked_list.find('a')\n",
"print(node)\n", "print(node)\n",
"# Find in an empty list\n", "# Find a NULL\n",
"head = Node(10)\n", "head = Node(10)\n",
"linked_list = LinkedList(None)\n", "linked_list = LinkedList(head)\n",
"node = linked_list.find(None)\n", "node = linked_list.find(None)\n",
"print(node)\n", "print(node)\n",
"# Find in a list with one element or more matching elements\n", "# Find in a list with one element or more matching elements\n",
@ -216,6 +240,35 @@
"print(node)" "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", "cell_type": "code",
"execution_count": null, "execution_count": null,