Sunday, 9 March 2014

Linked Lists

Once (when I was preparing for the term test) I had a feeling that almost all that we've learned during the previous weeks were examples of recursion: working on nested lists, recursive trees, python turtle, etc.

Linked lists, to no one's surprise, is another example of recursive processing, which as its own limitations and advantages. For example, in order to return the item at a given index in a linked list, we have to iterate over the list before this index so that we can find the item. We should first check if the index i is 0, an if not, we use a new index i-1 on the "rest of the list". As the index becomes zero, we return the first item in the current linked list being processed. It works in a recursive way, which seems to be less efficient than finding an item at the same index in a normal python list.

However, linked lists do have some advantages over normal lists. For example, when we're taking out an object from a queue (in which data are stored in a first-in-last-out form), when we take out the item at index 0 from the normal list, items in the rest of the list have to be shifted leftwards. In a linked list, when an item is deleted it is almost as simple as when an independent object is deleted. The rest of the list remains unchanged, which makes it more efficient than moving out the item from a normal list.

So as we're programming we should think of how we can improve by using different ways to store the data.

No comments:

Post a Comment