Tuesday, March 10, 2015

LinkedList impression

This week we learned LinkedList. Actually, I was wondering why we need this kind of data structure. Its drawback is obviously, that if you want to find the value of a item, you have to search the whole list and the worst case is the value exits at the back of the list. Also, if you want to use a item, you also need to travel the whole list. But after the lecture, I know that it also has some good implementations. For example, if you want to cut a list at a specific point, and concatenate it with another list, LinkedList data type can do this task easily. Consider a normal array, if we want to do the task above, we need to copy the first part into a new array and copy the second part into another array and then add them together, which is a little bit tedious.

With LinkedList, if we want to do this, we simply use one while loop to find the point we want to cut and link it to the beginning of the other list and it becomes a new LinkedList, easy and fast. Another application of LinkedList i think, is to optimize the use of memory and avoid data overwrite. We know what LinkedList stores is just a pointer that stores address information of the data, which means we can manipulate these data without changing its real memory address. The good thing about that is we dont need to reserve a memory spot for all these data since they can locate at any address with the link of address pointer. With that, we can kind of maximize the usage of the memory and achieve dynamic memory allocation.