Explain the scenarios where you can use linked lists and arrays

Following are the scenarios where we use linked list over array:

  • When we do not know the exact number of elements beforehand.
  • When we know that there would be large number of add or remove operations.
  • Less number of random access operations.
  • When we want to insert items anywhere in the middle of the list, such as when implementing a priority queue, linked list is more suitable.

Below are the cases where we use arrays over the linked list:

  • When we need to index or randomly access elements more frequently.
    *When we know the number of elements in the array beforehand in order to allocate the right amount of memory.
  • When we need speed while iterating over the elements in the sequence.
    When memory is a concern:
  • Due to the nature of arrays and linked list, it is safe to say that filled arrays use less memory than linked lists.
  • Each element in the array indicates just the data whereas each linked list node represents the data as well as one or more pointers or references to the other elements in the linked list.
    *To summarize, requirements of space, time, and ease of implementation are considered while deciding which data structure has to be used over what.