Singly Linked List in C Programming

What is a Singly Linked List in C?

A singly linked list is a linear data structure in which each element (called a node) points to the next node in the sequence. Unlike arrays, linked lists do not store elements in contiguous memory locations.

Each node typically contains:

  • Data – stores the actual value.
  • Next Pointer – points to the next node in the list.

The list starts with a pointer called head, which points to the first node. The last node’s next pointer is set to NULL.

Visual Representation:

[Data|Next] → [Data|Next] → [Data|Next] → NULL

Structure of a Node in C

Let’s begin by defining the basic structure of a singly linked list node:

advertisement
#include <stdio.h>
#include <stdlib.h>
 
struct Node {
    int data;
    struct Node* next;
};

This structure defines a node that holds an integer (data) and a pointer to the next node (next).

Basic Operations on Singly Linked List in C

1. Insertion

Free 30-Day C Certification Bootcamp is Live. Join Now!
  • At the Beginning: Insert a new node at the start of the list.
  • At the End: Append a new node at the end.
  • At a Specific Position: Insert a node at a given position.

2. Deletion

  • From the Beginning: Remove the first node.
  • From the End: Remove the last node.
  • From a Specific Position: Delete a node from a given position.

3. Traversal

  • Iterate through the list to access or display each node’s data.

Example: Creating a Singly Linked List

#include <stdio.h>
#include <stdlib.h>
 
struct Node {
    int quiz;
    struct Node* next;
};
 
void display(struct Node* head) {
    struct Node* certification = head;
    while (certification != NULL) {
        printf("%d -> ", certification->quiz);
        certification = certification->next;
    }
    printf("NULL\n");
}
 
int main() {
    struct Node* head = NULL;
    struct Node* mcqs = NULL;
    struct Node* test = NULL;
 
    // Allocate nodes
    head = (struct Node*)malloc(sizeof(struct Node));
    mcqs = (struct Node*)malloc(sizeof(struct Node));
    test = (struct Node*)malloc(sizeof(struct Node));
 
    // Assign data and links
    head->quiz = 10;
    head->next = mcqs;
 
    mcqs->quiz = 20;
    mcqs->next = test;
 
    test->quiz = 30;
    test->next = NULL;
 
    display(head);
    return 0;
}

Output:

10 -> 20 -> 30 -> NULL

The program defines a Node structure with an integer and a pointer to the next node. It dynamically allocates memory for three nodes using malloc(). It assigns the values 10, 20, and 30 to the nodes. The next pointers link the nodes to form a singly linked list. The display() function loops through the list and prints each value with an arrow. The program prints: 10 -> 20 -> 30 -> NULL.

Advantages of Singly Linked List

  • Dynamic Size: It can grow or shrink at runtime. This helps avoid memory waste.
  • Fast Insertions/Deletions: Adding or removing elements is quicker than arrays—especially at the start or middle. No shifting is needed.
  • No Memory Waste: Memory is used only when needed. It’s allocated dynamically with malloc().
  • Flexible Structure: Good for building stacks, queues, graphs, and hash tables.

Disadvantages of Singly Linked List

  • No Direct Access: You can’t jump to an element like in arrays (e.g., arr[i]). You must go from the start each time.
  • Extra Memory Use: Each node needs more memory to store a pointer to the next node.
  • No Backward Movement: You can’t move backward. Each node points only to the next one.
  • Harder to Work With: More complex than arrays. Writing and fixing the code takes more effort.

Applications of Singly Linked List

  • Dynamic Memory Allocation
  • Implementing Stacks and Queues
  • Graph Representations
  • Navigating Music or Image Galleries
  • Symbol Tables and Hash Tables

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.