Friday, April 4, 2014

week_8 LinkedList & LLNode

This week we've been introduced to a new class LinkedList. It seemed nothing special when I first saw it. A LinkedList class can be seen as a more systematic way to operate on lists. And LLNode is merely more focused on every node in a LinkedList. In a LinkedList, head is the first LLNode and tail usually represents the rest of the LinkedList linking directly behind the head. Thus, this class provides us with a more sophisticated way to modify lists. For example, if we want to pop an item in a common list, we can use the Python built-in method "pop" or "remove". But if we are working with a LinkedList,  we can simply let the head and rest to be rest.head and rest.rest, thus the original head is removed and then we can lind this new tail to a new head. Compared to "insert" or "append", we let the item which we intend to insert in to the list to be a new head of a tail, then link the modified tail to the original head. I would say, LinkedList focuses more on the relationship between each item rather than the whole list. In a common list,  we use index to control items but here, we have heads and tails and this head-and-tail depends on how you see it, which is more flexible, though I've been more used to list. I remembered we even saw a function to reverse a LinkedList, which is pretty cool with recursive calls. In LLNode class, the head-and-tail becomes current node and next node, which makes it more convenient to implement methods in a LinkedList since LLNodes are nodes in a LinkedList.

LinkedList is actually very useful, for example,  we can make a Stack class represented by a LinkedList so its original methods become more sophisticated. It's really blowing my mind. The lab about implementing methods in a LinkedList makes me reflect more on LinkedList, it was encouraging since it seemed I really got the hang of it.




No comments:

Post a Comment