0

Adv and dis-adv of using Array vs Linked List

When to use Array and When to use linked list? what are advantages and disadvantages of using both?

28th Oct 2016, 2:44 PM
Amarnath K
Amarnath K - avatar
1 Resposta
0
Array is good when you need to be able to get element by its index. When you use linked list, you know just position of first element, so you need to traverse in worst case whole list to find an element. Advantage of linked list is fast insertion and deletion. In array, when you want to insert new element, you need to move every element that is right from position on which you are inserting by one to create free space for new element. The same is with deletion (you are moving elements to the left to cover the hole). In linked list, deletion or insertion means just changing of 2 pointers, so it is in constant time. So when you need to erase and insert elements, use linked list, otherwise use array.
28th Oct 2016, 4:49 PM
Daniel Oravec
Daniel Oravec - avatar