Array vs Linked List

Linked lists have several advantages over arrays.

1)I think the insertion of new elements is trivial in a linked-list but it’s a major chore in an array.
2)arrays allow random access, while linked lists allow only sequential access to elements.
3)It’s easier to store data of different sizes in a linked list. An array assumes every element is exactly the same size.
4)Shuffling a linked list is just a matter of changing what points to what. Shuffling an array is more complicated and/or takes more memory.
5)Linked List better from a multi-threading point of view.
6)Merging two linked lists is much faster than merging two arrays. The former takes O(1), the latter takes O(n).
7)Elements can be inserted and deleted in linked lists indefinitely but Insertion/Deletion of values in arrays are very expensive. It requires memory reallocation.
8)The size of linked lists are dynamic by nature and the size of the array is restricted to the declaration.
9)Coding is more complex than Arrays.
an example :
>>for an array
a = (1 2 3 4, ....)
b = (4 3 2 1 1 2 3 4 ...)
c = (3 4 ...)

>>for linked list:
b = 4 -> 3 -> 2 -> 1 -> a
c = a.next.next

  • without having to copy the data being pointed to by a into b and c

No comments

Powered by Blogger.