Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C
  4. Mastering Linked Lists in C: A Comprehensive Guide

Mastering Linked Lists in C: A Comprehensive Guide

Date- Mar 14,2026 87
c linked lists

Overview of Linked Lists

A linked list is a linear data structure that consists of a sequence of elements called nodes. Each node contains data and a reference (or link) to the next node in the sequence. Linked lists are essential because they allow for efficient insertion and deletion of elements compared to arrays, where resizing can be costly. Understanding linked lists is crucial for grasping more complex data structures and algorithms.

Prerequisites

  • Basic understanding of C programming language
  • Familiarity with pointers and memory management
  • Knowledge of arrays and basic data structures
  • Basic understanding of algorithms

Creating a Linked List

To begin working with linked lists, we first need to define the structure of a node and create a function to initialize the list.

#include 
#include 

struct Node {
    int data;
    struct Node *next;
};

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

int main() {
    struct Node* head = createNode(10);
    head->next = createNode(20);
    head->next->next = createNode(30);

    // Printing the linked list
    struct Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    return 0;
}

This code snippet defines a simple linked list and initializes it with three nodes. Let's break it down:

  • #include <stdio.h> and #include <stdlib.h>: These headers are included for input/output and memory allocation functions.
  • struct Node: This structure represents a node in the linked list, containing an integer data and a pointer next to the next node.
  • createNode: This function allocates memory for a new node, sets its data, and initializes its next pointer to NULL.
  • main: We create the head node and add two more nodes to the list, then print their values.

Inserting Elements in a Linked List

Insertion operations are vital for linked lists. Here, we will implement functions to insert nodes at the beginning, at the end, and after a specific node.

void insertAtBeginning(struct Node** head_ref, int new_data) {
    struct Node* new_node = createNode(new_data);
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

void insertAtEnd(struct Node** head_ref, int new_data) {
    struct Node* new_node = createNode(new_data);
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }
    struct Node* last = *head_ref;
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = new_node;
}

void insertAfter(struct Node* prev_node, int new_data) {
    if (prev_node == NULL) {
        printf("The given previous node cannot be NULL.");
        return;
    }
    struct Node* new_node = createNode(new_data);
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}

This code implements three insertion methods. Here’s a breakdown:

  • insertAtBeginning: Inserts a new node at the beginning of the list. It updates the head pointer to point to the new node.
  • insertAtEnd: Inserts a new node at the end of the list. If the list is empty, it sets the head to the new node. Otherwise, it traverses to the last node and links it to the new node.
  • insertAfter: Inserts a node after a specified node. It checks if the previous node is NULL and links the new node accordingly.

Deleting Elements from a Linked List

Just as important as insertion is the ability to delete nodes from a linked list. We will implement a function to delete a node with a specific value.

void deleteNode(struct Node** head_ref, int key) {
    struct Node* temp = *head_ref;
    struct Node* prev = NULL;

    if (temp != NULL && temp->data == key) {
        *head_ref = temp->next; // Changed head
        free(temp); // Free old head
        return;
    }

    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) return;

    prev->next = temp->next;
    free(temp);
}

This function deletes a node with a specific value. Let's analyze the code:

  • deleteNode: It takes a pointer to the head and the key to be deleted. If the head node contains the key, it updates the head to the next node and frees the memory.
  • It traverses the list while checking for the key. If found, it links the previous node to the next node and frees the memory.

Best Practices and Common Mistakes

When dealing with linked lists, consider the following best practices:

  • Memory Management: Always free allocated memory to avoid memory leaks.
  • Null Checks: Always check for NULL pointers before dereferencing them to avoid segmentation faults.
  • Consistent Naming: Use meaningful variable names for ease of understanding and maintenance.
  • Test Thoroughly: Write test cases to ensure all operations (insertion, deletion, traversal) work correctly.

Common mistakes include:

  • Forgetting to update the head pointer when deleting the first node.
  • Not handling memory allocation failures.
  • Improper traversal logic leading to infinite loops.

Conclusion

In this blog post, we explored the fundamental concepts of linked lists in C, including node creation, insertion, deletion, and best practices. Linked lists are powerful data structures that provide flexibility in managing data. By mastering linked lists, you will enhance your programming skills and better understand more complex data structures. Remember to apply the best practices to avoid common pitfalls!

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Searching Algorithms in C: Linear and Binary Search Explained
Mar 14, 2026
Mastering Sorting Algorithms in C: Bubble, Selection, and Insertion Sort
Mar 14, 2026
Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free
Mar 11, 2026
Mastering JavaScript Events: Understanding addEventListener and Event Bubbling
Mar 30, 2026
Previous in C
Mastering Recursion in C Programming: A Comprehensive Guide
Next in C
Understanding Stacks and Queues in C: A Beginner's Guide
Buy me a pizza

Comments

On this page

More in C

  • Mastering Unconditional Statements in C: A Complete Guide wi… 21439 views
  • Understanding C: A Complete Guide with Examples 5147 views
  • Mastering Unconditional Statements in C: A Complete Guide wi… 4196 views
  • Mastering 2-D Arrays in C: A Complete Guide with Examples 3913 views
  • Introduction to C: A Step-by-Step Guide with Examples 3575 views
View all C posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor