Friday, February 28, 2014

Week 7: Linked Lists

When first introduced, linked lists appear to be abstract and hard to grasp. The idea of "next" or "rest" does not strike me as a straight forward concept until explained by our TA, Bryan. This is what I've come to terms with at the end of the week:

Node 0: empty; The variable to the class instance. In other words,
  >>> a = LinkedList()
Node 0 is basically a, used to call the first node, node 1, of the newly created instance of the class LinkedList. So, node 0 points to node 1.

Node 1: first element of the linked list; similar to the first element of a traditional python list. It points to node 2, i.e. the "rest" or "next" of node 1 is node 2.

Node 2:  second element of the linked list; similar to the second element of a traditional python list. It points to node 3, i.e. the "rest" or "next" of node 2 is node 3.

Consider node one as the head, the rest would be node 2, node 3, node 4, until the end because all the nodes are linked and point to their own next.

24.2 of the reading explained this similarly, but with graphical aid and shell code to illustrate it.

The methods to implement LinkedList are generally recursive. As the lab activity suggests, once you understand one code, the others tend to be similar.

The key is to start with the base case to handle None, then save rest as temp to preserve them while you change the head. Also, to traverse the list, use index value and recursively call index - 1 with the next node until index value puts you to where you want to be.

That's it for the week! Hope we all did up to our standards on our midterms!

No comments:

Post a Comment