Array vs Linked List
Array and Linked list are the two ways of organizing the data in the memory. Before understanding the differences between the Array and the Linked List, we first look at an array and a linked list.
- Array vs Linked List
- What is an array?
- Declaration of array
- What is Linked list?
- Differences between Array and Linked list
- 1. Cost of accessing an element
- 2. Cost of inserting an element
- 3. Memory requirements
- Linked List vs Array
- What are the differences between arrays and linked lists?
- Understanding the Difference Between Array and Linked List
- Table of Contents
- What is an Array
- What is a Linked List
- Difference between Array and Linked List
- Video liên quan
What is an array?
An array is a data structure that contains the elements of the same type. A data structure is a way of organizing the data; an array is a data structure because it sequentially organizes the data. An array is a big chunk of memory in which memory is divided into small-small blocks, and each block is capable of storing some value.
Suppose we have created an array that consists of 10 values, then each block will store the value of an integer type. If we try to store the value in an array of different types, then it is not a correct array and will throw a compile-time error.
Declaration of array
An array can be declared as:
To declare an array, we first need to specify the type of the array and then the array's name. Inside the square brackets, we need to specify the number of elements that our array should contain.
Let's understand through an example.
In the above case, we have declared an array of 5 elements with 'a' name of an integer data type.
What is Linked list?
A linked list is the collection of nodes that are randomly stored. Each node consists of two fields, i.e., data and link. Here, data is the value stored at that particular node, and the link is the pointer that holds the address of the next node.
Differences between Array and Linked list
We cannot say which data structure is better, i.e., array or linked list. There can be a possibility that one data structure is better for one kind of requirement, while the other data structure is better for another kind of requirement. There are various factors like what are the frequent operations performed on the data structure or the size of the data, and other factors also on which basis the data structure is selected. Now we will see some differences between the array and the linked list based on some parameters.
1. Cost of accessing an element
In case of an array, irrespective of the size of an array, an array takes a constant time for accessing an element. In an array, the elements are stored in a contiguous manner, so if we know the base address of the element, then we can easily get the address of any element in an array. We need to perform a simple calculation to obtain the address of any element in an array. So, accessing the element in an array is O(1) in terms of time complexity.
In the linked list, the elements are not stored in a contiguous manner. It consists of multiple blocks, and each block is represented as a node. Each node has two fields, i.e., one is for the data field, and another one stores the address of the next node. To find any node in the linked list, we first need to determine the first node known as the head node. If we have to find the second node in the list, then we need to traverse from the first node, and in the worst case, to find the last node, we will be traversing all the nodes. The average case for accessing the element is O(n).
We conclude that the cost of accessing an element in array is less than the linked list. Therefore, if we have any requirement for accessing the elements, then array is a better choice.
2. Cost of inserting an element
There can be three scenarios in the insertion:
- Inserting the element at the beginning: To insert the new element at the beginning, we first need to shift the element towards the right to create a space in the first position. So, the time complexity will be proportional to the size of the list. If n is the size of the array, the time complexity would be O(n).
In the case of a linked list, to insert an element at the starting of the linked list, we will create a new node, and the address of the first node is added to the new node. In this way, the new node becomes the first node. So, the time complexity is not proportional to the size of the list. The time complexity would be constant, i.e., O(1).
- Inserting an element at the end
If the array is not full, then we can directly add the new element through the index. In this case, the time complexity would be constant, i.e., O(1). If the array is full, we first need to copy the array into another array and add a new element. In this case, the time complexity would be O(n).
To insert an element at the end of the linked list, we have to traverse the whole list. If the linked list consists of n elements, then the time complexity would be O(n).
- Inserting an element at the mid
Suppose we want to insert the element at the ith position of the array; we need to shift the n/2 elements towards the right. Therefore, the time complexity is proportional to the number of the elements. The time complexity would be O(n) for the average case.
In the case of linked list, we have to traverse to that position where we have to insert the new element. Even though, we do not have to perform any kind of shifting, but we have to traverse to n/2 position. The time taken is proportional to the n number of elements, and the time complexity for the average case would be O(n).
The resultant linked list is:
- Ease of use
The implementation of an array is easy as compared to the linked list. While creating a program using a linked list, the program is more prone to errors like segmentation fault or memory leak. So, lots of care need to be taken while creating a program in the linked list.
- Dynamic in size
The linked list is dynamic in size whereas the array is static. Here, static doesn't mean that we cannot decide the size at the run time, but we cannot change it once the size is decided.
3. Memory requirements
As the elements in an array store in one contiguous block of memory, so array is of fixed size. Suppose we have an array of size 7, and the array consists of 4 elements then the rest of the space is unused. The memory occupied by the 7 elements:
Memory space = 7*4 = 28 bytes
Where 7 is the number of elements in an array and 4 is the number of bytes of an integer type.
In case of linked list, there is no unused memory but the extra memory is occupied by the pointer variables. If the data is of integer type, then total memory occupied by one node is 8 bytes, i.e., 4 bytes for data and 4 bytes for pointer variable. If the linked list consists of 4 elements, then the memory space occupied by the linked list would be:
Memory space = 8*4 = 32 bytes
The linked list would be a better choice if the data part is larger in size. Suppose the data is of 16 bytes. The memory space occupied by the array would be 16*7=112 bytes while the linked list occupies 20*4=80, here we have specified 20 bytes as 16 bytes for the size of the data plus 4 bytes for the pointer variable. If we are choosing the larger size of data, then the linked list would consume a less memory; otherwise, it depends on the factors that we are adopting to determine the size.
Let's look at the differences between the array and linked list in a tabular form.
|An array is a collection of elements of a similar data type.||A linked list is a collection of objects known as a node where node consists of two parts, i.e., data and address.|
|Array elements store in a contiguous memory location.||Linked list elements can be stored anywhere in the memory or randomly stored.|
|Array works with a static memory. Here static memory means that the memory size is fixed and cannot be changed at the run time.||The Linked list works with dynamic memory. Here, dynamic memory means that the memory size can be changed at the run time according to our requirements.|
|Array elements are independent of each other.||Linked list elements are dependent on each other. As each node contains the address of the next node so to access the next node, we need to access its previous node.|
|Array takes more time while performing any operation like insertion, deletion, etc.||Linked list takes less time while performing any operation like insertion, deletion, etc.|
|Accessing any element in an array is faster as the element in an array can be directly accessed through the index.||Accessing an element in a linked list is slower as it starts traversing from the first element of the linked list.|
|In the case of an array, memory is allocated at compile-time.||In the case of a linked list, memory is allocated at run time.|
|Memory utilization is inefficient in the array. For example, if the size of the array is 6, and array consists of 3 elements only then the rest of the space will be unused.||Memory utilization is efficient in the case of a linked list as the memory can be allocated or deallocated at the run time according to our requirement.|
Next TopicStack vs. Queue
← prev next →
Linked List vs Array
Arrays store elements in contiguous memory locations, resulting in easily calculable addresses for the elements stored and this allows faster access to an element at a specific index. Linked lists are less rigid in their storage structure and elements are usually not stored in contiguous locations, hence they need to be stored with additional tagsgiving a reference to the next element. This difference in the data storage scheme decides which data structure would be more suitable for a given situation.
Data storage scheme of an array
Data storage scheme of a linked list
Major differences are listed below:
- Size: Since data can only be stored in contiguous blocks of memory in an array, its size cannot be altered at runtime due to the risk of overwriting other data. However, in a linked list, each node points to the next one such that data can exist at scattered (non-contiguous) addresses; this allows for a dynamic size that can change at runtime.
- Memory allocation: For arrays at compile time and at runtime for linked lists. but, a dynamically allocated array also allocates memory at runtime.
- Memory efficiency: For the same number of elements, linked lists use more memory as a reference to the next node is also stored along with the data. However, size flexibility in linked lists may make them use less memory overall; this is useful when there is uncertainty about size or there are large variations in the size of data elements; memory equivalent to the upper limit on the size has to be allocated (even if not all of it is being used) while using arrays, whereas linked lists can increase their sizes step-by-step proportionately to the amount of data.
- Execution time: Any element in an array can be directly accessed with its index; however in the case of a linked list, all the previous elements must be traversed to reach any element. Also, better cache locality in arrays (due to contiguous memory allocation) can significantly improve performance. As a result, some operations (such as modifying a certain element) are faster in arrays, while some others (such as inserting/deleting an element in the data) are faster in linked lists.
Following are the points in favor of Linked Lists.
(1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and in practical uses, the upper limit is rarely reached.
(2) Inserting a new element in an array of elements is expensive because room has to be created for the new elements and to create room existing elements have to be shifted.
For example, suppose we maintain a sorted list of IDs in an array id[ ].
id[ ] = [1000, 1010, 1050, 2000, 2040, …..].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays unless some special techniques are used. For example, to delete 1010 in id, everything after 1010 has to be moved.
So Linked list provides the following two advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Linked lists have the following drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do a binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
3) Arrays have better cache locality that can make a pretty big difference in performance.
4) It takes a lot of time in traversing and changing the pointers.
5) It will be confusing when we work with pointers.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Article Tags :
Practice Tags :
Read Full Article
What are the differences between arrays and linked lists?
Arrays and Linked Lists are two of the most popular linear data structures. Let’s look at some of their major differences:
Understanding the Difference Between Array and Linked List
Lesson 49 of 54By Nikita Duggal
Last updated on Dec 14, 20214382
Data Structure Tutorial
Overview(Video) Arrays vs Linked Lists - Computerphile
Arrays in Data Structures: A Guide With Examples
Lesson - 1
All You Need to Know About Two-Dimensional Arrays
Lesson - 2
All You Need to Know About a Linked List in a Data Structure
Lesson - 3
The Complete Guide to Implement a Singly Linked List
Lesson - 4
The Ultimate Guide to Implement a Doubly Linked List
Lesson - 5
The Fundamentals for Understanding Circular Linked List
Lesson - 6
The Ultimate Guide To Understand The Differences Between Stack And Queue
Lesson - 7
Implementing Stacks in Data Structures
Lesson - 8
Your One-Stop Solution for Stack Implementation Using Array
Lesson - 9
Your One-Stop Solution for Queue Implementation Using Array
Lesson - 10
Your One-Stop Solution to Learn Depth-First Search(DFS) Algorithm From Scratch
Lesson - 11
Your One-Stop Solution for Stack Implementation Using Linked-List
Lesson - 12
The Definitive Guide to Understand Stack vs Heap Memory Allocation
Lesson - 13
All You Need to Know About Linear Search Algorithm
Lesson - 14
All You Need to Know About Breadth-First Search Algorithm
Lesson - 15
A One-Stop Solution for Using Binary Search Trees in Data Structure
Lesson - 16
The Best Tutorial to Understand Trees in Data Structure
Lesson - 17
A Complete Guide to Implement Binary Tree in Data Structure
Lesson - 18
A Holistic Look at Using AVL Trees in Data Structures
Lesson - 19
All You Need to Know About Tree Traversal in Data Structure
Lesson - 20
The Best Guide You’ll Ever Need to Understand B-Tree in Data Structure
Lesson - 21
The Best Guide You'll Ever Need to Understand Spanning Tree in Data Structure
Lesson - 22
The Best and Easiest Way to Understand an Algorithm
Lesson - 23
Your One-Stop Solution to Understand Shell Sort Algorithm
Lesson - 24
Your One-Stop Solution to Quick Sort Algorithm
Lesson - 25
The Most Useful Guide to Learn Selection Sort Algorithm
Lesson - 26See AlsoFinancial Accounting questions and answersWhat is the best time to take vitamin D?97 Tennessee Facts and Weird LawsCHAN ROBLES VIRTUAL LAW LIBRARY
Everything You Need to Know About Radix Sort Algorithm
Lesson - 27
Everything You Need to Know About the Counting Sort Algorithm
Lesson - 28
Everything You Need to Know About the Merge Sort Algorithm
Lesson - 29
Insertion Sort Algorithm: One-Stop Solution That Will Help You Understand Insertion Sort
Lesson - 30
Everything You Need to Know About the Bubble Sort Algorithm
Lesson - 31
The Best Guide You’ll Ever Need to Understand Bucket Sort Algorithm
Lesson - 32
Your One-Stop Solution to Understand Recursive Algorithm in Programming
Lesson - 33
The Definitive Guide to Understanding Greedy Algorithm
Lesson - 34
Your One-Stop Solution to Understand Backtracking Algorithm
Lesson - 35(Video) Arrays vs Linked Lists - Data Structures and Algorithms
The Fundamentals of the Bellman-Ford Algorithm
Lesson - 36
Your One-Stop Solution for Graphs in Data Structures
Lesson - 37
The Best Guide to Understand and Implement Solutions for Tower of Hanoi Puzzle
Lesson - 38
A Simplified and Complete Guide to Learn Space and Time Complexity
Lesson - 39
All You Need to Know About the Knapsack Problem : Your Complete Guide
Lesson - 40
The Fibonacci Series: Mathematical and Programming Interpretation
Lesson - 41
The Holistic Look at Longest Common Subsequence Problem
Lesson - 42
The Best Article to Understand What Is Dynamic Programming
Lesson - 43
A Guide to Implement Longest Increasing Subsequence Using Dynamic Programming
Lesson - 44
A Holistic Guide to Learn Stop Solution Using Dynamic Programming
Lesson - 45
One Stop Solution to All the Dynamic Programming Problems
Lesson - 46
Understanding the Fundamentals of Binomial Distribution
Lesson - 47
Here’s All You Need to Know About Minimum Spanning Tree in Data Structures
Lesson - 48
Understanding the Difference Between Array and Linked List
Lesson - 49
The Best Article Out There to Understand the B+ Tree in Data Structure
Lesson - 50
A Comprehensive Look at Queue in Data Structure
Lesson - 51
Your One-Stop Solution to Understand Coin Change Problem
Lesson - 52
The Best Way to Understand the Matrix Chain Multiplication Problem
Lesson - 53
Your One-Stop Solution to Learn Floyd-Warshall Algorithm for Using Dynamic Programming
Lesson - 54
Table of Contents
Data structures are formats implemented in computer programming to store, manage, and organize data items. These storage formats enable efficient access and modification of data elements. There are several data structures for organizing data in memory, but perhaps the most basic ones are array and linked list. Both these data structures store homogeneous elements in sequential order. However, there are a lot of differences between arrays and linked lists. So, in this tutorial, we will unleash a few key differences between these data structures.
Post Graduate Program: Full Stack Web Development
in Collaboration with Caltech CTMEEnroll Now
What is an Array
An array is a data structure that has a fixed size. It can store elements of the same type. When there are multiple elements of the same type, it is not possible to store each of them as separate variables. An array provides an alternative to this issue. It stores all the elements as a single element. For example, double salary ; declares an array called salary that can store 10 double values. This array size is 10. Therefore, the programmer cannot store more than 10 elements in this array.
The index of the 1st element in the array is 0. If there are 10 elements in the array, the index of the last element is 9. All the elements in the array are in contiguous memory locations. The lowest address corresponds to the first element while the highest address corresponds to the last element. Moreover, it is possible to perform operations such as insert, delete, modify elements, and traversing through the array and merging arrays.
What is a Linked List
Linked List is a linear data structure which contains a group of nodes in a sequence. Each node consists of its own data and the address of another node. It can store the address of the next node, or both the next node and the previous node. The elements are linked together and form a structure similar to a chain. The main advantage of a Linked List is that it is dynamic. Unlike in an array, it is not necessary to allocate all required memory initially. Instead, a linked list allows allocating memory when required.
On the other hand, a Linked List requires more memory as it stores the addresses of other nodes. In a linked list, it is not possible to access an element randomly at once. The programmer should go through each node sequentially to access a particular element. Furthermore, it is difficult to perform reverse traversing in the linked list.
Difference between Array and Linked List
Both Linked List and Array are used to store linear data of similar type, but an array consumes contiguous memory locations allocated at compile time, i.e. at the time of declaration of array, while for a linked list, memory is assigned as and when data is added to it, which means at runtime.
Before we proceed further with the differences between Array and Linked List, if you are not familiar with Array or Linked list or both, you can check these topics first:
- Array in C
- Linked List
This is the basic and the most important difference between a linked list and an array. In the section below, we will discuss this in details along with highlighting other differences.
Linked lists differ from lists in the way that they store elements in memory. While lists use a contiguous memory block to store references to their data, linked lists store references as part of their own elements.Which of the following best describes the difference between an array and a linked list? ›
Hence, we can conclude that an array can be used when size is finite or small, and a linked list can be used when the size is unknown or infinite.Why are arrays better than linked lists? ›
Arrays allow random access and require less memory per element (do not need space for pointers) while lacking efficiency for insertion/deletion operations and memory allocation. On the contrary, linked lists are dynamic and have faster insertion/deletion time complexities.What is the difference between linked list and array and a comparison of the time complexity for different operations in an array and linked list? ›
Memory allocation: For arrays at compile time and at runtime for linked lists. but, a dynamically allocated array also allocates memory at runtime. Memory efficiency: For the same number of elements, linked lists use more memory as a reference to the next node is also stored along with the data.What is ArrayList and linked list? ›
Difference Between ArrayList and LinkedList.
|1) ArrayList internally uses a dynamic array to store the elements.||LinkedList internally uses a doubly linked list to store the elements.|
The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.Which is better for sorting array or linked list? ›
For a set of integers you want to sort using quicksort, it's probably faster to use an array; for a set of large structures you want to sort using selection sort, a linked list will be faster.What is the main advantage of a linked list? ›
The principal benefit of a linked list over a conventional array is that the list elements can be easily inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk, while restructuring an array at run-time is a much more ...What is linked list best used for? ›
Linked lists are often used because of their efficient insertion and deletion. They can be used to implement stacks, queues, and other abstract data types.What is the major difference between array and List? ›
Data Types Storage: Array can store elements of only one data type but List can store the elements of different data types too. Hence, Array stores homogeneous data values, and the list can store heterogeneous data values.
Arrays can hold the only the same type of data in its collection i.e only homogeneous data types elements are allowed in case of arrays. Collection, on the other hand, can hold both homogeneous and heterogeneous elements. Arrays can hold both object and primitive type data.Why array is faster than ArrayList? ›
An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.Which is better list or linked list? ›
In most cases, List<T> is more useful. LinkedList<T> will have less cost when adding/removing items in the middle of the list, whereas List<T> can only cheaply add/remove at the end of the list.What is the difference between node list and linked list? ›
ListNode is a node in a linked list. SingleList is a linked list. To draw an analogy - a node is a link in a chain; the linked list is the chain itself.What is list in linked list? ›
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.Why use a linked list over a list? ›
Linked lists have most of their benefit when it comes to the insertion and deletion of nodes in the list. Unlike the dynamic array, insertion and deletion at any part of the list takes constant time. Linked lists hold two main pieces of information (the value and pointer) per node.Which is faster linked list or array? ›
If you want to search or access a specific element, arrays are faster, but if you want to insert or delete an element, a linked list is faster.Is linked list FIFO or LIFO? ›
A singly-linked list may be LIFO (last-in-first-out) or FIFO (first-in-first-out). If the list is using the LIFO method, the nodes will be added to and deleted from the same end. If it's using FIFO, nodes will be added to one end and deleted from the opposite end. Additionally, the linked list may be sorted.What is the difference between a node list and an array? ›
- Simple Linked List − Item navigation is forward only.
- Doubly Linked List − Items can be navigated forward and backward.
- Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.
In a linked list, the items are linked together through a single next pointer. In a binary tree, each node can have 0, 1 or 2 subnodes, where (in case of a binary search tree) the key of the left node is lesser than the key of the node and the key of the right node is more than the node.What is linked list in short? ›
A linked list is the most sought-after data structure when it comes to handling dynamic data elements. A linked list consists of a data element known as a node. And each node consists of two fields: one field has data, and in the second field, the node has an address that keeps a reference to the next node.What is the real life example of linked list? ›
Let's talk about some real-life applications. A music player displays the property of a linked list. In a music player, each song is linked with the next song. Similarly, an image viewer links all the images with each other.What is the biggest advantage of the linked list? ›
Advantages Of Linked List: Dynamic data structure: A linked list is a dynamic arrangement so it can grow and shrink at runtime by allocating and deallocating memory. So there is no need to give the initial size of the linked list.Why linked list is rarely used? ›
They are too low level. You already have the array type, which is mostly implemented in native code and useful in the general case. One benefit of linked lists is quick removal of elements.