Array vs. linked list: What's the Difference?

Advertisement

Key Difference

The difference between an array and a linked list is that an array is an index based data structure, every element is associated with an index whereas the linked list is a data structure that uses references, each node is referred to another node.

Advertisement

Array vs. linked list

Data structures and types of data structures is one of the most important concepts in computer science or computer programming. Two most important type of data structures are arrays and linked lists. Array and linked list are two difference data structures. If we talk about the main difference, then the main difference between an array and a linked list is that an array is a data structure that is an index based data structure, every element is associated with an index whereas the linked list is a data structure that uses references, each node is referred to another node.  The array is a sequential memory where it is set or sequence of similar objects. The array consists of different variable names.

Advertisement

In the case of the linked list, there is a sequence of the element where each node is connected to another node. Each node is given the reference of another node, so it makes a linked list. There are two fields in the linked list that are most important that are data field, and other is link field. There are actual data in the data field, and these data fields are connected through the data field.

The way data fields are connected is that link field contains the address of data fields. The pointer plays a significant role in this regard; pointer keeps the address of the data field. Array has a fixed sized and link list do not have fixed sized, in case of array we need to declare the size of the array where in link list it is not necessary to declare the size of link list first.

Advertisement

Comparison Chart

Basis Array Link List
MeaningAn array is a data structure that is an index based data structure.Linked list is a data structure that use references.
SizeIn array size is fixed.In the link, list size is not fixed.
Insertion In the array, there is a slow insertion.In the link list, there is a fast insertion.
Memory Utilization In the array, there is ineffective memory utilisation.In the link list, there is effective memory utilisation.

 What is Array?

An array is a data structure that is an index based data structure; every element is associated with an index. Array is a sequential memory where it is a set or sequence of similar objects. Array consists of different variable names. Array can only have one type of data.

Following is an example code of an array

C program to insert a particular element in a specified position

* In a given array

*/

#include <stdio.h>

void main()

{

int array[10];

int i, j, n, m, temp, key, pos;

printf(“Enter how many elements \n”);

scanf(“%d”, &n);

printf(“Enter the elements \n”);

for (i = 0; i < n; i++)

{

scanf(“%d”, &array[i]);

}

printf(“Input array elements are \n”);

for (i = 0; i < n; i++)

{

printf(“%d\n”, array[i]);

}

Operations on Array

Following are the operations you can perform on array

  • Creation of array
  • Traversing an array
  • Insertion of new elements
  • Deletion of required elements.
  • Modification of an element.
  • Merging of arrays

What is Linked List?

A linked list is a data structure that uses references; each node is referred to another node. Linked list there is a sequence of the element where each node is connected to another node. Each node is given the reference of another node, so it makes a linked list. There are two fields in the linked list that are most important which are data field, and other is link field. There are actual data in the data field, and these data fields are connected through the data field.

The way data fields are connected is that link field contains the address of data fields. The pointer plays a significant role in this regard; pointer keeps the address of the data field. The array has a fixed sized, and link list does not have fixed sized, in case of array we need to declare the size of the array where in link list it is not necessary to declare the size of link list first.

Example code of linked list

C program to create a linked list and display the elements in the list

*/

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

void main()

{

struct node

{

int num;

struct node *ptr;

};

typedef struct node NODE;

NODE *head, *first, *temp = 0;

int count = 0;

int choice = 1;

first = 0;

while (choice)

{

head  = (NODE *)malloc(sizeof(NODE));

printf(“Enter the data item\n”);

scanf(“%d”, &head-> num);

if (first != 0)

{

temp->ptr = head;

temp = head;

}

else

{

first = temp = head;

}

fflush(stdin);

printf(“Do you want to continue(Type 0 or 1)?\n”);

scanf(“%d”, &choice);

}

temp->ptr = 0;

/*  reset temp to the beginning */

temp = first;

printf(“\n status of the linked list is\n”);

while (temp != 0)

{

printf(“%d=>”, temp->num);

count++;

temp = temp -> ptr;

}

printf(“NULL\n”);

printf(“No. of nodes in the list = %d\n”, count);

}

Operations of Link list

  • Creation of link list
  • Traversing a link list
  • Insertion of new elements
  • Deletion of required elements.
  • Modification of an element.
  • Merging of the link list

Key Differences

  1. An array is a data structure that is index based data structure whereas the Linked list is a data structure that uses
  2. In array size is fixed whereas in link list size is not fixed.
  3. In the array, there is slow insertion whereas there is a very fast insertion in the link
  4. In the array, there is ineffective memory utilisation whereas in link list there is effective memory utilisation.

Conclusion

An array and a link list are two different data structures in this article above we see the clear difference between an array and a link list.

Leave a Reply

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