Array and Linked list both we use to store linear data of same datatype, but array has some limitation
Linked List
- The size of the linked list is dynamic.
- Ease of insertion or deletion.
- Linked List supports Sequential Access, which means to access any element/node in a linked list, we have to sequentially traverse the complete linked list, upto that element.
- To access nth element of a linked list, time complexity is O(n).
- In a linked list, new elements can be stored anywhere in the memory.
- Memory is allocated at runtime, as and when a new node is added. It's also known as Dynamic Memory Allocation.
- Linked list can be Linear(Singly) linked list, Doubly linked list or Circular linked list linked list.
- Array gets memory allocated in the Stack section.
Array
- The size of the arrays is fixed, so we should have knowledge of array size in advance.
- Inserting and deleting data in array is difficult.
- Array supports Random Access, which means elements can be accessed directly using their index, like arr[0] for 1st element, arr[6] for 7th element etc.
- accessing elements in an array is fast with a constant time complexity of O(1).
- In an array, elements are stored in contiguous memory location or consecutive manner in the memory.
- Memory is allocated as soon as the array is declared, at compile time. It's also known as Static Memory Allocation.
- Array can be single dimensional, two dimensional or multidimensional.
- Whereas, linked list gets memory allocated in Heap section.
EmoticonEmoticon