Sunday, March 15, 2015

Slog#7. Linked Lists


Linked list was another complex topic to comprehend. I was really confused when we implemented methods such as append and prepend. Methods prepend was easier to comprehend and I had almost no difficulty implementing these methods on my own. However, append method was a lot more challenging. The main problem I had for prepend was that I did not quite understand the point of putting the following in prepend method:

if self.back is None:
self.back = self.front

When I tried printing an instance of linked list class, it seemed to work fine with these two lines commented out. However, I learned that not putting those lines will be problematic because we need to keep track of self.back at all times which is not instantiated when the class is created. Unless we specify which value self.back is going to take, it will remain as None.

It was much more difficult to comprehend append method. The main problem here was that I could not figure out how self.back.nxt = new_node changes self.front as well. After following every step in python visualizer, I learned that when self.front = self.back= new_node is called at first, both self.front and self.back refers to the memory address of an object which contain LLNode(5). This node has no ‘next’ node associated with at this point. Subsequent append method calls will lead to self.back.nxt = new_node and now the memory address with LLNode(5) has a next node associated with it such that now it looks something like LLNode(5, LLNode(6)), for instance. Consequently, self.front changes accordingly as it also refers to the memory address of this node which has a value of a 5 and is associated with next node LLNode(6) which itself has a value of 6 but has not yet been associated with a ‘next’ node. When this node is connected next node, self.front will change again accordingly. 

No comments:

Post a Comment