Created
March 18, 2021 01:22
-
-
Save pamelafox/5874e0fdd85d44914ba2195cc4d2fc5d to your computer and use it in GitHub Desktop.
Link
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 Link: | |
"""A linked list.""" | |
empty = () | |
def __init__(self, first, rest=empty): | |
assert rest is Link.empty or isinstance(rest, Link) | |
self.first = first | |
self.rest = rest | |
def __repr__(self): | |
if self.rest: | |
rest_repr = ', ' + repr(self.rest) | |
else: | |
rest_repr = '' | |
return 'Link(' + repr(self.first) + rest_repr + ')' | |
def __str__(self): | |
string = '<' | |
while self.rest is not Link.empty: | |
string += str(self.first) + ' ' | |
self = self.rest | |
return string + str(self.first) + '>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment