1.

Why Would You Use An Array Vs Linked-list ?

Answer»

Linked List:

  • They allow a NEW element to be inserted or deleted at any position in a constant number of operations (changing some references) O(1).
  • Easy to delete a NODE (as it only has to rearrange the links to the different nodes)., O(1).
  • To find the nth node, will need to recurse through the list TILL it finds [linked lists allow only sequential access to elements. ], O(n)

Array

  • Insertion or deletion of element at any position require a linear (O(n)) number of operations.
  • POOR at deleting nodes (or elements) as it cannot remove one node without individually shifting all the elements up the list by one., O(n)
  • Poor at inserting as an array will EVENTUALLY either fill up or need to be resized, an expensive operation that may not even be possible if memory is fragmented. Similarly, an array from which many elements are removed may become wastefully empty or need to be made smaller, O(n)
  • easy to find the nth element in the array by directly referencing them by their position in the array.[ arrays allow random access ] , O(1)

Linked List:

Array



Discussion

No Comment Found