What is the difference between Array List and Linked List ?

5 min 5 sec read Basic

Till now we have studied the arrays, right? Arrays are used to store data of the same type. In arrays, we must know the size of the array. That's why an array size is fixed every time.

For example,

Assume there is an array like this,

10 30 40 50 60 70

Now suppose you want to add element 20 at the first index after 10. Now to insert 20 after 10 you have to shift everything one position towards the right. So a shifted array looks like this,

10 20 30 40 50 60 70

Now think for a while, how expensive an operation of shifting an element of the array is. Now suppose you have to remove element 30 from the given array. So you have to shift all the elements towards the left. The solution to this shifting operation is a well-known Data Structure known as the Linked list in Data Structures.

ArrayList vs Linked List

The difference between Array List and the Linked List is explained here by the advantages of Linked List over Arrays.

1) Dynamic size 
2) Insertion/Deletion is easy in Linked List.

What is Linked List?

The linked list is a linear data Structure used to store the elements dynamically. No need to know the actual size. In the case of arrays, you must know the size of an array. Linked List in Data Structure gives better advantages than arrays.


Linked List looks like this

    +---+-----+------       +----+----+------         +----+----+-----
    | Data | Pointer|-----> | Data | Pointer| ------> | Data  | NULL |  
    +---+-----+------       +----+----+------         +----+----+-----

Linked List is created using nodes. Each node contains Data and Pointer. And each Pointer is attached with Next pointer. And the end Pointer contains NULL to indicate that the linked list is finished. The first Node is HEAD always.

Program to introduce Linked List

C program to understand the concept of the linked list in Data Structures

           head        first             Second
             |            |                |
             |            |                |
             V            |                |
        +---+---+     +---+---+       +----+------+
        | 1  | o----->|  2  | o-----> |  3 | NULL |
        +---+---+     +---+---+       +----+------+    

// A simple C program to introduce a linked list in DataStructures
struct node 
  int data;
  struct node *next;

/* Program to create a simple linked list with 3 nodes */
int main()
  struct node* head = NULL;
  struct node* first = NULL;
  struct node* second = NULL;
/* allocate 3 nodes in the heap  */
  head = (struct node*)malloc(sizeof(struct node)); 
  first = (struct node*)malloc(sizeof(struct node));
  second = (struct node*)malloc(sizeof(struct node));
  head->data = 1;       //assign data in head node
  head->next = first;  // Link head node with the first node

  first->data = 2;      //assign data to first node
  first->next = second; // Link first node with the second node
  second->data = 3;      //assign data to second node
  second->next = NULL;    
  return 0;

Note, firstly head is pointing to the head node. Then head node to the first node and the first node to the second node.

The comments given are self-explanatory. What we did is first we have allocated the three Nodes using the malloc() function. We named it as the head, first, the second node. We added the data in the head node and then linked it with the first node. Then we added data to the first node and linked it with the second node. Then we added data to the second node and then added NULL to indicate the end of the linked list.

It's easy if you looked at the program carefully. Just three boxes. Add data into it and link it to the next node. Now you can create any number of nodes you want.

I hope you have got the concept of how to create a node in the linked list with the C Program. Tada! Also, check out the related articles which you might love to read. They are given in the below section.

How was the tutorial? Nice. Right?

Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.

What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp

Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python