Sunday, 8 March 2015

Impressions of Linked List

    As what we learned in this week, linked list a linear sequence of nodes, each with a value a reference to the next node. What's more, we keep reference to the front and back of the sequence. We defined two classes in the program, which is LLNode and LinkedList. LLNode represents a single node and LinkedList represents the linked list as a whole, which can be called a wrapper class. Regarding wrapper class, it means that we can bundle information about the overall linked list. Moreover, an empty linked list is an instance of the class, with attribute values set to show it's empty.
    There are three important thins about linked list. One is traversing a linked list. Following is a common pattern:

current = self.front
while <some condition holds>:
   <Do something with the current node.>
   # Advance to the next node.
   current = current.nxt

  Another thing is mutating a linked structure. Since code that mutates an existing structure is easy to mess up, we are strongly recommended to draw pictures, which helps us understand it better.
    The last thing we need to pay attention to is the problem of going to far. For some methods, once we've found what we are looking for, we have gone too far to make the changes we want. An useful solution to this is to walk two references along the list, with one trailing behind a step.

No comments:

Post a Comment