Tuesday, December 13, 2011

What are the differences between arrays and linked lists?

Difference between arrays and linked lists:

Arrays:

1. Arrays are static memory allocations. We can not change the array size at run time. If we declare the array size 10, then it can hold only 10 values.
2. The advantage of Arrays is, it supports random access. Means if we want to access 3rd element in an array, we can aceess it simply by using array[2],because array starts from 0.

Linked Lists:

1. Linked lists are dynamic memory allocations. We can allocate memory at run time.

2. The disadvantage of linked list is, it supports only sequential access. It does not support random access. If we want to access 3rd element, we need to traverse previous two nodes to access 3rd element.