Friday, 7 November 2014

Array vs Link List

Q. What is the difference between array and linked list and when to use them?

Ans : Both are data structures. Data structures are used to store and processed data in some order in efficient way.

Arrays are static data structure. Static means we have to define the size of the array while initializing the arrays. Later on we can not modify the size of arrays.

Ex:   int array[10];

Here array is an array of 10 integers. We can not increase the size of the array at run time. But we can access any element of the array randomly. We do not have to traverse the whole array.

Ex:  array[1]=1;


Instead link list is a dynamic data structure. We do not have to define the size of the link list at the time of initialization. We can increase the size dynamically at run time. We implement the link list by using pointers and pointers can be allocated memory easily at running time.

In link list we can not access any element randomly we have to traverse the all the elements which are before the target element. So complexity of accessing element in link list are much more than the array.

Difference in the complexity of Searching and Sorting varies with the algorithms used to do the mentioned tasks. Generally we use the array when we know the size of the input otherwise we go for the link list.






No comments:

Post a Comment

Related Posts

Related Posts Plugin for WordPress, Blogger...