Linked list vs array
Published by
sanya sanya
Definitions:
Arrays: An array is a collection of similar data elements stored in contiguous memory locations and accessed by a common name or index. The elements in an array can be of any data type, such as integers, characters, or even other arrays. Size of an array is fixed and determined at the time of declaration. Once an array is created, the size cannot be changed.
Linked list: A linked list is a data structure that consists of a sequence of nodes, where each node contains a value and a reference to the next node in the sequence. Each node in a linked list can be located anywhere in memory, and it contains a pointer pointing to the next node in the sequence.
Major Differences:
||Array|Linked List| | :- | :- | :- | |Memory allocation|Contiguous memory allocation at compile time or runtime.|Non-contiguous memory allocation at runtime.| |Size|The size of an array is fixed and determined at the time of declaration.|The size of a linked list can be dynamic and can change during runtime.| |Accessing elements|Element can be directly accessed using its index.|Element is accessed after traversing through all the previous nodes.| |Insertion and Deletion|Slower process, as elements need to be shifted in memory to accommodate the change.|Relatively faster, as only pointers need to be updated| |Memory usage|Uses less memory than linked list.|Uses more memory as it stores pointers along with data.|
Advantages of linked list:
- Dynamic size: Linked lists can grow or shrink in size during runtime, while the size of an array is fixed and must be determined at the time of declaration.
- Efficient insertion and deletion: Insertion and deletion of elements in a linked list are relatively cheap operations, as only the pointers need to be updated. In contrast, insertion and deletion in an array can be expensive, as elements may need to be shifted in memory to accommodate the change.
- No need for initialization: Linked lists do not need to be initialized with a default value, unlike arrays. This can be useful in certain situations where default values are not needed.
Disadvantages of linked list:
- Random access: Linked lists do not support random access to elements, as elements are accessed sequentially through their pointers after traversing. Arrays access elements through index with O(1) time complexity.
- Memory usage: Linked lists use more memory than arrays, as each element requires a pointer to the next element, which adds overhead.
- Cache locality: Linked lists do not have good cache locality, as accessing a single element can require traversal through multiple nodes. In contrast, arrays have good cache locality, as elements are stored contiguously in memory.
- Complex working: It is more complex to work with pointers in linked list.
Advantages of array:
- Accessing elements: Arrays allow for constant-time random access to elements, as elements are stored in contiguous memory locations and can be accessed using their index. In contrast, linked lists require traversal through the list to access a specific element.
- Cache locality: Arrays have good cache locality, as accessing a single element will also load nearby elements into the cache, reducing cache misses. In contrast, linked lists have poor cache locality due to their scattered memory locations.
- Memory efficiency: Arrays are more memory-efficient than linked lists for small data sets, as they do not require additional memory for pointers between elements. Also, as the array is of fixed size and contiguously stored, there is no memory shortage or overflow.
- Ease of use: Arrays are simpler and easier to use than linked lists, as they do not require dynamic memory allocation or pointer manipulation.
Disadvantages of array:
- Fixed size: Arrays have a fixed size that must be determined at the time of declaration. This means that once the array is created, it cannot be resized, and if more space is needed, a new array must be created and the elements must be copied over.
- Memory allocation: Large arrays may require a lot of memory, and it may not be possible to allocate the required amount of memory in one contiguous block.
- Inefficient insertion and deletion: Inserting or deleting elements from an array can be inefficient, as elements may need to be shifted in memory to accommodate the change. This can be particularly problematic for large arrays.
- Wasted space: If an array is not fully utilized, there may be wasted space in memory.
Library
WEB DEVELOPMENT
FAANG QUESTIONS