Thursday, 20 March 2014

Week8: Linked List

This week we are introduced to LinkedList, and again it is new and abstract. It has recursive structure. There are two ways to interpret it:

1. The list that made by two parts (1) the value or item; (2) the remaining list or rest;
2. A node or object that has a reference and a value. The reference and value are nodes as well.

In order to understand these concepts better, first we need to know what wrapper and helper are and how they work. Wrappers and helpers increase the clarity of our code, and also they are easier to come up with. For example I wrote a function that will capitalize the word:

def capitalized(word: str) -> str:
      return word.upper()

However, I want to add an explanation in front of my capitalized word. Instead of modifying my original function, I can write a wrapper function:

def capitalized_word(word: str) -> str:
      w = capitalized(word)
      return 'The word I capitalized: ' + w

Inside the wrapper function capitalized_word, capitalized is called directly as a helper function. This is a simple example, and we can just combine these two functions. However, if the case become complex, such as the LinkedList and LListNode, wrappers and helpers become necessary. LinkedList(the wrapper class) is considered as a collection of LListNode(the helper class), therefore it can be decomposed in some sense, and invokes the method of LListNode directly.

Next, we are going to examine methods applied in LinkedList. There are three methods taught in lecture: insert, delete_front, and reverse. Notice that LinkedList is first-in, first-out, therefore, we add object in front of LinkedList. The idea here is create a LListNode that has its next node refers to the LinkedList. When we delete an object, we delete the front LListNode. We implement it by replacing the front node with its next node. Moreover, reversing LinkedList is a bit more complex, so the helper function does the work of reversing LListNode, and the front node is replaced with the reversed LListNode. In addition, the pair of BST(wrapper class) and BTNode(helper class) are similar to LinkedList and LListNode. BST seems more effective in the aspect of storing values. Its nodes are ordered like nodes within a Tree.

Thus, getting familiar with properties and methods of Linked List is not enough for doing well on the course. I really need to practice on more codes and get a deeper understanding on what else it can do. Hope it works~

Finally, thanks for reading my slog and welcome the discussion:)

1 comment:

  1. i forgot we also had a reverse method for linkedlist, thanks for mentioning it!

    ReplyDelete