Created
August 2, 2017 12:15
-
-
Save skuro/77f7d45e39298404fee7153c2d91913e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, cargo, next=None): | |
self.cargo = cargo | |
self.next = next | |
def __str__(self): | |
return str(self.cargo) | |
def search(self, needle): | |
if self.cargo == needle: | |
# abbiamo trovato l'elemento | |
return True | |
elif self.next == None: | |
# siamo alla fine e ancora non abbiamo trovato l'elemento | |
return False | |
else: | |
# non abbiamo trovato l'elemento ma c'e' altro nella lista, continuiamo | |
return self.next.search(needle) | |
node1 = Node("bibi") | |
node2 = Node(42, node1) | |
node3 = Node("test", node2) | |
# a questo punto abbiamo node3 -> node2 -> node1 -> None | |
print node3.search("test") # True | |
print node2.search("test") # False, perche' comincia da node2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment