Finishes implementing delete

This commit is contained in:
julien lengrand-lambert
2013-12-08 19:58:55 +01:00
parent 94a9bd3626
commit 7c2e8b5534
2 changed files with 45 additions and 2 deletions

View File

@@ -47,12 +47,12 @@ class SingleLinkedList():
self._size += 1
def delete_item(self, node=None):
def delete_item(self, node=0):
"""
Deletes the nth node of the list. (0 being the first element)
If node is None, deletes the first element of the list.
"""
if node == None:
if node == 0:
item = self._root.nexti
self._root = item
self._size -= 1
@@ -74,6 +74,22 @@ class SingleLinkedList():
self._size -= 1
def delete(self, value):
"""
Deletes the first node in the list that contains value
"""
ptr=0
item = self._root
while(item != None):
if item.value == value:
self.delete_item(ptr)
return
item = item.nexti
ptr += 1
print "Going there!"
raise Exception("Value not found in the list")
def __len__(self):
# Returns the number of elements in the list
@@ -85,6 +101,9 @@ class SingleLinkedList():
This way of doing is not exactly clever, given the fact that I put
everything in memory before printing
"""
if self._root == None:
return "Empty List"
to_print = ""
curr = self._root
to_print += str(curr.value)
@@ -101,3 +120,5 @@ if __name__ == "__main__":
ll.add(5)
ll.add(6)
print ll
ll.delete(14)

View File

@@ -69,5 +69,27 @@ class test_single_linked_list(unittest.TestCase):
sl.delete_item()
self.assertEqual("4, 5", sl.__str__())
def test_delete(self):
sl = SingleLinkedList()
sl.add(2)
sl.add(1)
sl.add(3)
self.assertEqual(3, len(sl))
self.assertEqual(2, sl._root.value)
self.assertRaises(Exception, lambda x: sl.delete(4))
sl.delete(1)
self.assertEqual("2, 3", sl.__str__())
sl.delete(2)
self.assertEqual("3", sl.__str__())
sl.delete(3)
self.assertEqual("Empty List", sl.__str__())
self.assertEqual(0, len(sl))
if __name__ == "__main__":
unittest.main()