Implements has_next() method in list item

This commit is contained in:
Julien Lengrand-Lambert
2013-12-07 15:27:50 +01:00
parent 26ed3fe4d8
commit 757e3cbcd0
2 changed files with 32 additions and 6 deletions

View File

@@ -5,13 +5,30 @@ Implementation of Linked Lists
2013/12
"""
class SingleListItem():
"""
Item from a Single Linked List.
Contains only a previous next element, and a value
"""
def __init__(self, value, next):
"""
def __init__(self, value, nexti=None):
self.value = value
self.next = next
self.nexti = nexti
def has_next(self):
"""
Returns True if the item has a next value
"""
return not (self.nexti is None)
class SingleLinkedList():
"""
Linked list with only one link between items.
The list can only be traversed one way"""
def __init__():
self.root= SinglelistItem(None, None) # The head
if __name__ == "__main__":
a = 12
t = SingleListItem(12)
print t.has_next()

View File

@@ -13,9 +13,18 @@ class test_single_linked_list_item(unittest.TestCase):
def test_item(self):
a = 12
t = SingleListItem(12, None)
t = SingleListItem(12)
self.assertEqual(a, t.value)
def test_has_next(self):
a = 12
t = SingleListItem(12)
self.assertEqual(False, t.has_next())
b = SingleListItem(13, t)
self.assertEqual(True, b.has_next())
if __name__ == "__main__":
unittest.main()
unittest.main()