Sunday, March 8, 2015

Linked Lists

A linked list structure can be thought of as an item and a ordered sub-list. It can be implemented on python with the use of 2 classes. The first class represents a node, which can be thought of as an item in the list. This class keeps track of the node's own value which can be any object type, and also has a 'next' attribute that points to the next node. A second class stores information about the entire list. It keeps track of the very first node (front), the last node (back) and the number of nodes in the list. It can be visualized as shown below.


There are many methods or functions that can work with linked lists. It is possible to check whether the list contains a certain value, to retrieve a value with its index, add a node anywhere in the list, delete nodes anywhere in the list etc. The majority of these things require moving along the list and examining each item. This can be done by creating a current_node variable and setting it to the front node of the list, then moving current_node along the nodes in the list.

current_node = self.front
while <condition>:
    <action>
    current_node = current_node.nxt
*self refers to the class that keeps track of the entire list

I found the concept really easy to grasp, and it is relatively easy to work with linked lists as long as I draw out examples as I am writing methods/functions that work on linked lists. I didn't find linked lists to be unpleasant, but I didn't find it very interesting either, but I'm sure there will be more fun new things coming up for us to cover in the course. 

2 comments:

  1. Great explanation on LinkedList. For me, LinkedList concept was a bit hard... I just hope I did well on my term 2 test.

    ReplyDelete
  2. Good comprehension, and I like your blogs' pages!

    ReplyDelete