We have been learning about linked lists in class. Linked list structures are as objects (nodes) with a value and a reference to other similar objects. You can also picture them as lists made up of an item (value) and a sub-list (rest). There is a class called linked list node that initialize one node and the next node.
class LLNode:
'''Node to be used in linked list
nxt: LLNode -- next node
None iff we're at end of list
value: object --- data for current node
'''
def __init__(self, value, nxt=None):
''' (LLNode, object, LLNode) -> NoneType
Create LLNode (self) with data value and successor nxt.
'''
self.value, self.nxt = value, nxt
In linked lists, there is a front and back. There is also size to measure how large the list is. To move through the list, LLNode is used to go on to the next node. In a way. linked lists work like recursion, but it doesn't use recursion in it's functions.
To walk through a linked list:
cur_node = self.front
while <some condition here...>:
# do something here...
cur_node = cur_node.nxt
Now, you can apply this formula when you need to walk through the linked list using a function.
No comments:
Post a Comment