• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

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

Aniruddha Chaudhari/21168/6
C / C++CodeCSE SubjectData Structure

Table of Contents

  • What is Linked List?
  • Understaning Linked List Structure
  • Write a Program to Implement Linked List in C
  • Linked List vs Array in C

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:

  • Loop statement in C

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:

  • How to reverse Linked List with or without recursion?

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.

CodecppLinked List
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Darun Anmol
    March 9, 2019 at 3:24 pm

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

    • Reply
      Aniruddha Chaudhari
      March 9, 2019 at 3:25 pm

      You right. If you go through interview questions, you will see many coding questions from Linked List.

  • Reply
    kriti
    August 8, 2019 at 7:03 pm

    Please share the code for inserting and deleting in a linked list.

    • Reply
      Aniruddha Chaudhari
      August 8, 2019 at 9:52 pm

      Hi Kirti. Taking note of your query. I will write another tutorial for this.

  • Reply
    VENKATA SARAVANAN
    September 17, 2021 at 6:23 pm

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

    • Reply
      Jayakumar
      January 31, 2022 at 11:00 pm

      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 Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard