Last week we started learning about linked lists. Linked lists are very helpful when you want to insert an item to a list because linked lists allocate memory more efficiently. However, to find an item in a linked list, you must walk through the list to find the item.
In a linked list, each object or node has a value and a reference (or link) to an object that comes after it:
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
You can also think of linked lists where the first node is the first item in the list and the second item is a sublist of comprising the rest of the linked list.
We say that we use a "wrapper" class to store the front, back and size of the linked list.
The general approach to walking through the linked list (as aforementioned) is as follows: you "pick" a node, and use a while loop to set the current node as the next node until the condition is False.
I did have some trouble with the lab but it I understand it much better after reading the solutions. I was thinking of more complicated solutions for append, prepend, and delete_front. I tried implementing them without using front, back, and size.
No comments:
Post a Comment