mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Implements has_next() method in list item
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user