Sunday, March 8, 2015

Link Them Together - Impression of Linked Lists

Hi sloggers, it is a new week again. Something unfortunate happened this week and I am sure you all must know about this, the strike. Sadly, I lost all of my tutorials but also got 4 hours of extra spare time every week. I hope that school can settle this properly as soon as possible. Tutorials are always beneficial as they offer practices for what I learned in class and my TAs are all really nice and good at explaining concepts. Anyways, I will try to keep up with my studies so let's talk about the new data structure we learned, Linked List!

A linked list is a sequence of nodes, each of which contains a value and a reference to the next node.
We used LLNode to represent a single node and LinkedList to represent linked list, which we call the wrapper class. Well, questions has been raised about why do we need this wrapper class. The answer is we want to know where does the list start and where does it end. otherwise, the data structure would not make any sense. 
Here is how you can think of a linked list in visual. Each box represents a LLNode

The __init__method of a LLNode class
Notice that self.nxt is None iff we are at the end of list.



we keep a reference to the front/head of the sequence. The back of the sequence is called the tail. 








traversing a linked list poses a little bit trouble as we can't access each node directly as in a normal list. Thus, we need to "walk through" a linked list and the general pattern is like this. 



We want to be able to perform normal operations like adding and deleting on a linked list. However, you gotta be extremely careful cause sometimes we may go so far in our "walk" that we lost the reference to go back one more step to make the changes we want.  The solution is to walk 2 references along the list, with one trailing behind a step.
There are a lot of practices in this week's lab, but I found them challenging as I went through them to prepare for next week's test. So, I discussed them with my friends and compared answer with the one Danny posted on course website. Here, I want to include one example and discuss some of the key points that I think deserve attention.
The key to any linked list problem is to DRAW PICTURES!! They are extremely handy in terms of understanding the whole process.              


To start, suppose we have a linked list like this 


Say we want to insert a node before A. We need to first walk through the list to find node A, then change the reference of E to refer to the new node  and link the new node to node A. Clearly, we need 2 references as we are walking through the list, since we want to access E after we find A. 

The picture looks like this. This is the general case and let's first write code up to the point we find node A.


This looks straight forward, but before we proceed, we still need to consider some special cases. 1. what if v2 is not in the list? then after the search for v2, cur_node would be None. 2. what if v2 is the first in the list? then we technically did not go into the while loop, so prev_node is None. keep this in mind and we can generate the rest of the code.








This is not that bad right? Again the key is to draw the pictures. I still need to practice more since I sometimes overlook the special cases, which can cause a big problem. I actually want to do a comparison between list and linkedlist, since they simply perform the same sort of tasks. Yet, today we have enough to say so I may go back to this post to continue on this topic or I may just simply do it in a later post. Thanks for reading and good luck on test2!!!

No comments:

Post a Comment