Last Updated: 06 October, 2024 4 Mins
Array and Linked List both are the linear data structure and used to store collection of elements with the same data type.
Before we start discussing the differences between Linked List and the Array, let's first briefly know what Linked List and the Array are. Then, afterward, we will see the differences between both.
An array is an object that stores collections of elements with the same data type. The length of an array is fixed at the time of creation, and it cannot be modified after that.
An array allocates contiguous memory locations for each element. An array is an index-based data structure; each element is accessed by its unique numerical index value, which starts at 0, which means the first element of the array will be stored at the 0th index position, the second element will be stored at the 1st index position, and so on. Using the index number, we can directly access any random value from the array.
Here, 10 characters size of Array is created which means Array length is 10 and storing values like A, B, C, D, E, F, G, H, I, J. Since at the time of array declaration, the character 'A' is declared first and then 'B' character and so on, therefore 'A' will be store at the 0 index position and 'B' will be store at index 1 and so on.
Linked List is a linear data structure, in which the elements are not stored at contiguous memeory location. In Linked List each element called node, which has two parts, first part contains data and second part contains address of next node.
A Linked List is a dynamic data structure so the number of nodes in a list is not fixed, it can be grow and shrink based on the need.
In a linked list, the first node is called the Head, and it acts as an access point. The head pointer points to the first node of the linked list. The last node is called the Tail, and it points to a NULL value. As we can see in the above image.
Head: The reference to the first node in a linked list, or a pointer to it, is called the head of the list. The linked list begins at this pointer.
Node: A node is an entity that contains data and pointer (address of the next node).
Data: It contains the information/date of the node.
Next Pointer: It contains the address of the next node the linked list. It is a part of node.
Array | Linked List |
Array is a group of elements of a similar data type. | Linked List is a group of nodes, each node consists of two fields: data and address to the next node. |
An array stores elements in a contiguous memory location. | Linked lists store elements randomly at any address in the memory. |
Its memory size is fixed while declaration and cannot be changed at run time. | Where as, in Linked list memory size is not fixed, it can be changed at run time. |
In Array, all the elements are indepentent they don't store address of adjacent element. | Elements in a linked list are dependent on each other, as each node stores the address of its next node. |
It takes more time for operations like insertion, deletion, etc. | It takes less time than arrays for the same operations. |
Memory is allocated at compile time. | Memory is allocated at run time. |
It is easier and faster to access the element in an array with the help of Indices. | Accessing an element in a linked list is time-consuming as we have to start traversing from the first element. |
Memory utilization is ineffective in arrays. | In linked lists, memory utilization is effective. |
Arrays support to represent multi-dimensional data structures. | Linked lists are typically used to represent one-dimensional data structures. |
Arrays are commonly used in low-level programming and for implementing data structures. | Linked lists are often used for specific data management needs like task scheduling and memory allocation. |
Operations | Array | Linked List |
Random Access | O(1) | O(n) |
Insertion and Deletion at the beginning | O(n) | O(1) |
Insertion and Deletion at the end | O(1) | O(n) |
Insertion and Deletion from any random location | O(n) | O(n) |
That's all, guys. I hope this article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com