Difference between Array and Linked List
|
Array |
Linked List |
Define |
Array is a collection of elements having same data type with common name. |
Linked list is an ordered collection of elements which are connected by links/pointers. |
Access |
In array, elements can be accessed using index/subscript value, i.e. elements can be randomly accessed like arr[0], arr[3], etc. So array provides fast and random access. |
In linked list, elements can’t be accessed randomly but can be accessed only sequentially and accessing element takes 0(n) time. |
Memory Structure |
In array, elements are stored in consecutive manner in memory. |
In linked list, elements can be stored at any available place as address of node is stored in previous node. |
Insertion & Deletion |
Insertion & deletion takes more time in array as elements are stored in consecutive memory locations. |
Insertion & deletion are fast & easy in linked list as only value of pointer is needed to change. |
Memory Allocation |
In array, memory is allocated at compile time i.e. Static Memory Allocation. |
In linked list, memory is allocated at run time i.e. Dynamic Memory Allocation. |
Types |
Array can be single dimensional, two dimension or multidimensional. |
Linked list can be singly, doubly or circular linked list. |
Dependency |
In array, each element is independent, no connection with previous element or with its location. |
In Linked list, location or address of elements is stored in the link part of previous element/node. |
Extra Space |
In array, no pointers are used like linked list so no need of extra space in memory for pointer. |
In linked list, adjacency between the elements are maintained using pointers or links, so pointers are used and for that extra memory space is needed. |
Figure |
|
|