2015年3月15日星期日

Week 9 Impression of week 8

We focused on the LinkedList these days. LinkedList is a class as a list make up of a value and a sub-list. Or we can think it the other way and consider it as an object with a value and a reference to other similar objects.


It will be much easier to understand the codes if we can draw pictures for LinkedList.
We will find the codes below quite useful for tracing the values of the LinkedList.
cur_node = self.front
while <some condition here...>:
    # do something here...
    cur_node = cur_node.nxt
If the mutation does not only relate to the current nodes but also relate to the previous nodes. (For example, delete_back) We will find the codes below quite useful.
prev_node, cur_node = None, lnk.front
        # walk along until cur_node is lnk.back
        while <some condition>:
            prev_node = cur_node
            cur_node = cur_node.nxt
When we mutate the LinkedList, we will probably change the variables like self.front, self.back, self.size, self.value and self.nxt. What can't be ignored is that it's confusing when we change some variables and the best way to make it less complicated is drawing clear pictures carefully.
When it comes to adding a brand new node, we need to create a new node by writing LLNode(value,nxt) and link it with the previous node and the next node.

没有评论:

发表评论