Linked List Implementation using Array in C [Step-By-Step]

Linked List Implementation using Array in C [Step-By-Step]

What is Linked List?

The linked list is the data structure in computer science that contains a group of nodes. Every node contains two fields, Data and Link field. Data field stores actual information that we want to save in a linked list. Every node is connected to other nodes by the link. In this post, we see step by step procedure to implement a Linked List in C.

Implement Linked List in C

Head contains pointer link to the first node of the Linked List. The last node-link contains a null pointer as it is not pointing to any other node in Linked List. When we add the new node at the end of the Linked List, this null value gets updated with the pointer of the new node.

Understaning Linked List Structure

It is the data structure in C programming. It is a complex data type using which you can group different types of variable inside it.

With the structure, it is easy to access all the data variables inside the structure with a single pointer.

Creating Structure Node for Linked List:

The structure can be created to implement the Linked List node in C as follows

struct * node
{
  int nData;
  struct node * pLink;
};

Creating Node of Linked List:

struct node* p = (struct node*)malloc(sizeof(struct node*));

malloc allocates runtime memory to the node while implementing Liked List in C. It returns the pointer to the linked list node named “p”.

 Accessing Variable inside Linked List Node:

After creating the node, we have to update the nData value with actual information that needs to save in Linked List and the nLink pointer to point to the next node in the Linked List.

  • p->nData = <info>;
  • p->nLink = <pinter to other node>;
  • p->pLink = NULL; //for last node in Linked List

Write a Program to Implement Linked List in C

Program: Create three Linked List nodes. Connect 3 nodes and display the linked list using displayLL(). Pass the head of the link list to displayLL() function.

I have explained how to implement Linked List in C, so We recommend you minimize the window and try it by yourself. If you are stuck anywhere, below is the full code to Implement Linked List in C.

Prerequisite:

C Program:

#include<stdio.h>
//Structure for Linked List Node
struct node
{
    int nData;
    struct node* pLink;
};
 
 
//Function to display Linked List
void displayLL(struct node* p)
{
  printf("Display The Link List:\n");   
  if(p)
  {
    do
    {
      printf(" %d", p->nData);
      p=p->pLink;
    }
    while(p);
  }
  else
    printf("Linked List is empty.");
}
 
int main()
{
  struct node* pNode1= NULL;
  struct node* pNode2= NULL;
  struct node* pNode3= NULL;
     
  //create node and assign data value
  pNode1 = (struct node *)malloc(sizeof(struct node *));
  pNode1->nData =10;
     
  pNode2 = (struct node *)malloc(sizeof(struct node *));
  pNode2->nData =20;
     
  pNode3 = (struct node *)malloc(sizeof(struct node *));
  pNode3->nData =30;
     
  //connecting nodes
  pNode1->pLink = pNode2;
  pNode2->pLink = pNode3;    
  pNode3->pLink = NULL;
     
  //Display Linked List if first node is not null
  if(pNode1)    
    displayLL(pNode1);
}

Linked List vs Array in C

Array and Linked List are two data structures and both have their own advantages.

Difference Between Array and Linked List:

  • As array allocates continuous memory space. Whereas the Linked list does not have continuous memory allocation.
  • In the array, it is easy to access elements of the array. We can directly access any element of the array. So to access any elements in Linked List we need to traverse from head to that particular node.

Likewise array, the linked list has its own importance.

Importance of Linked List over Array:

If both are important data structures to store data elements, Why there is a need to create a linked list from array elements?

  • As the array is the linear data structure having continuous memory location, there may be the chance of not having continuous memory available for creating a new element for the existing array.
  • This problem will not be there in case of the linked list data structure. We can create dynamic node any time to hold memory space at any address available. Not necessary to have continuous memory allocation.
  • So to avoid the trap of not having continuous memory allocation, there is a need to create a linked list over the array.

In the placement interview, it is a basic question interviewer can ask why the Linked list is a better choice than an array.

C program to Convert Array into Linked List:

Just like implementing a stack using an array, you can also implement a linked list from the array.

Write the code to create a linked list from array elements.

Following is the sample code for linked list implementation using array in C i.e. creating a linked list from array elements.

Difficulty Level: Moderate

#include<stdio.h>
 
struct node 
{
  int nData;
  struct node* pNode;
};
 
/*
Function to create Linked List from Array elements.
*/
struct node* createLL(int* nArr, int n)
{
  static int i=0;
  struct node* t = NULL;
  if(n==0)
    return NULL;
  // Create New Node    
  t = (struct node*)malloc(sizeof(struct node*));
  t->nData = nArr[i++];
  t->pNode = createLL(nArr, --n);
  return t;
}
 
void displayLL(struct node *t)
{
  while(t)
  {
    printf("%d ", t->nData);
    t=t->pNode;
  }
}
 
int main()
{
  int n=0, i=0, arr[100]={0};
  struct node *t = NULL;
  printf("\nEnter the number of elements: ");
  scanf("%d", &n);
  for(i=0; i<n; i++)
    scanf("%d", &arr[i]);
     
  printf("\nCreate linked list from array");
  t =createLL(arr, n);
  printf("\nDisplay Linked List : \n");
  if(t)
   displayLL(t);    
}

Output :

Enter the number of elements: 5
45 35 64 63 88

Create linked list from array

Display Linked List :
45 35 64 63 88

Time Complexity: O(n)

What’s Next:

Wrapping Up…

This is all about linked list implementation using array in C. If you are preparing for placement, the Linked list is an important topic. Most of the questions will be from Linked List.

If you have any doubts, discuss them in the comment section below. I would like to discuss your doubt and get resolved.

In the next post, we see how to insert the node at the beginning, middle, and end of the linked list.

8 Comments

  1. Linked list is one of the most important topics for placement. Thanks for writing. Keep it up.

  2. I have more difficulty studying all programs. Even basic programs also I feel some difficulty. Please give any solution for this.

    1. 1. Visualize a high-level view of the problem.
      2. Then understand the data flow in the program(consider examples from other programmers) by passing different inputs.
      3. Write your own logic based on the above steps and check the deviations.
      4. Hope this will help to improve your programming skills.

Leave a Reply

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