- 论坛徽章:
- 0
|
class Node(object):
'''A node in a linked list.'''
def __init__(self, data):
'''(Node, object) -> NoneType
Create a Node containing data with None as next.'''
self.data = data
self.next = None
def to_string(front):
'''(Node) -> str
Return a list representation of the linked list front in the Python list
style. If the list is circular, include ',...' at the end.'''
res = '['
if front:
t = front
# Treat the first node separately because we want one fewer commas than
# there are nodes.
res += str(t.data)
t = t.next
# Move along until we fall off or wrap back around.
while t and t != front:
res += ', %s' % t.data
t = t.next
# If we wrapped around, mark it as circular.
if t == front:
res += ', ...'
res += ']'
return res
def append(list1, list2):
'''(Node, Node) -> Node
Return the head of the linked list made by appending list2 to list1.
Precondition: neither are circular.'''
if list1[-2] != "." and list2[-2] != "." :
def toggle_circular(front):
'''(Node) -> NoneType
If linked list front is circular, make it non-circular. If linked list
front is non-circular, make it circular. If front is None, do nothing.'''
pass
def remove_values(front, v):
'''(Node, object) -> Node
Remove all Nodes from linked list front whose data are equal to v and
return the front of the resulting linked list.
Precondition: front is not circular.'''
pass
if __name__ == '__main__':
print to_string(None)
lst = Node(1)
print to_string(lst)
lst.next = lst # Make it circular.
print to_string(lst)
lst.next = Node(2) # Make it non-circular and append a second Node.
print to_string(lst)
lst.next.next = lst # Make it circular.
print to_string(lst)
追问: CLASS很难理解,可以简单概括下CLASS的用处吗 |
|