Array vs Linked List – Difference between Array and Linked List

Ever wondered, How data is stored inside the computer’s memory? You can’t just throw data into the memory without making proper structure for the data.

To provide a organised structure for the data to get stored inside the computer’s memory so that we can use and manage the stored data in the most efficient way possible, we use Data Structures.

All programming languages provide fundamental data structures like int, char, float etc. to store the primitive type values. But with the use of user defined data types we can create data structure as required. It is like creating our home with bricks, we can make any type of structure we want.

Here in this article we will see two most commonly used user defined data types: Arrays and Linked List, Difference between Array and Linked List and their implementation.

Array

The simplest type of data structure is Array which is used to store set of similar data in continuous blocks of memory under a common heading or variable name. With its simplicity comes some barriers like it need continuous block of memory and if free memory is not available in contiguous block it becomes inefficient to use arrays.

Array Representation

Array Representation (Source)

Arrays indexing starts with 0, and every element of the array can be accessed using its index. Operation on array is very fast as any element can accessed directly using array indexing. Name of array is the base address of the array and all other elements can be accessed using the base address because the memory allocated to array is consecutive.

Linked List

On the other hand, a simple yet very useful data structure which doesn’t need contiguous blocks of memory is Linked List. They are used in situations when there is need to allocate memory dynamically that is during runtime of a program. It can be visualized like a train where we have an Engine (Head) and all the carriages(nodes) are linked to the train engine either directly or indirectly through other carriages.

Linked list Representation

Linked list Representation (Source)

Linked list is user-defined data type where all the nodes of the list are linked using pointers. To access any node of the list, we must traverse the list linearly unlike array where we can directly access the elements.

Array vs Linked List – Difference between Array and Linked List

Parameters Array Linked List
Definition It is a collection of elements having same data type with a common name. It is an ordered collection of elements which are connected by links or pointers.
Size Fixed, once declared cannot be changed. Variable, can be changed during run-time.
Memory Allocation Require Contiguous blocks of memory. Random memory can be allocated.
Access Direct or randomly accessed, i.e., Specify the array index or subscript. Sequentially accessed, i.e., Traverse starting from the first node in the list by the pointer.
Insertion and Deletion Relatively slow, as to insert or delete any element, other elements need to be shifted to occupy the empty space or make space. Faster, Easier and Efficient as just linking or unlinking of node is required.
Searching Binary search and Linear search Linear search
Extra Space Extra space is not required. To link nodes of list, pointers are used which require extra Space.
Memory Utilization Inefficient Efficient
Types It can be single dimensional, two dimensional or multi-dimensional. It can be singly, doubly or circular linked list.

Array Implementation in C++

Output

5 4 3 2 1

Linked List Implementation in C++

Output

The linked list is: 7 6 4 1 5

Comment below if you have queries related to difference between array and linked list.

Category: DSA

Leave a Reply

Your email address will not be published. Required fields are marked *