Linked List in C Language with Programs and Examples
Table of Contents
A linked list in C is a dynamic data structure that stores elements in nodes connected using pointers. Unlike arrays, linked lists do not require contiguous memory allocation, which makes them flexible and efficient for insertions and deletions. If you are learning data structures in C, mastering linked lists is essential for exams, interviews, and real-world programming.
This linked list tutorial C guide explains concepts step-by-step with programs and examples that are easy to understand.
What Is a Linked List?
A linked list in C is a collection of nodes where each node contains:
- Data part
- Pointer to the next node
Instead of storing elements in continuous memory like arrays, linked lists store elements anywhere in memory and connect them using pointers.
Basic idea:
Node → Node → Node → NULL
Each arrow represents an address stored in a pointer.
- dynamic data structure in C
- pointer based data structure
- memory allocation in C
- what is linked list in data structure
- linked list definition in C
- dynamic memory structures
Types of Linked Lists
Before writing a linked list program in C, it is important to understand different types.
- Singly Linked List
Each node points to the next node only.
Structure:
Node → Node → Node → NULL
This is the most commonly taught version and widely used in exams.
- Doubly Linked List
Each node contains:
- Pointer to next node
- Pointer to previous node
Structure:
NULL ← Node ↔ Node ↔ Node → NULL
Used when backward traversal is needed.
- Circular Linked List
Last node points back to the first node instead of NULL.
Structure:
Node → Node → Node → (back to start)
Used in scheduling algorithms and buffers.
For beginners, focus on singly linked list in C first.
Node Structure
A linked list is built using a self-referential structure.
Basic struct node example C students learn:
struct node {
int data;
struct node *next;
};Explanation:
- data → stores value
- next → pointer to next node
This is why linked lists depend heavily on pointers and dynamic memory.
This node design is based on a self referential structure in C, where a structure contains a pointer to its own type.
Creating a Linked List
To create a linked list in C language, follow these steps:
- Define structure
- Create head pointer
- Allocate memory dynamically
Understanding how pointers work inside structures is essential before building advanced data structures.
Example program:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main() {
struct node *head = NULL;
head = (struct node*)malloc(sizeof(struct node));
head->data = 10;
head->next = NULL;
printf("First node value: %d", head->data);
free(head);
return 0;
}Here:
- malloc allocates memory
- head stores address of first node
This is the base of all linked list operations.
Insertion Operations
Insertion is one of the most important linked list operations asked in exams and interviews.
We will cover:
- Insert at beginning
- Insert at end
Insertion at Beginning
This is the easiest insertion method.
Steps:
- Create new node
- Set new node next = head
- Update head
Program:
void insertAtBeginning(struct node **head, int value) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
Time complexity: O(1)
Used when fast insertion is required.
Insertion at End
This method is commonly asked in practical exams.
Steps:
- Create new node
- Traverse list
- Attach at last
Program:
void insertAtEnd(struct node **head, int value) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}Time complexity: O(n)
People also search for:
- insert node linked list C
- insertion in singly linked list
- linked list insertion program
Deletion in Linked List
Deletion removes a node from the linked list.
Common types:
- Delete from beginning
- Delete by value
Example: delete from beginning
void deleteBeginning(struct node **head) {
if (*head == NULL) return;
struct node *temp = *head;
*head = (*head)->next;
free(temp);
}Important points:
- Always free memory
- Avoid memory leaks
Deletion questions are very common in university papers.
Traversal of Linked List
Traversal means visiting each node to display or process data.
Basic traversal program:
void traverse(struct node *head) {
struct node *temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL");
}Output example:
10 → 20 → 30 → NULL
Traversal helps in:
- Displaying list
- Searching values
- Counting nodes
Advantages of Linked List Over Arrays
Many students get confused between arrays and linked lists.
Here is a clear comparison.
| Feature | Arrays | Linked List |
| Memory allocation | Fixed | Dynamic |
| Size flexibility | No | Yes |
| Insertion speed | Slow | Fast |
| Deletion speed | Slow | Fast |
| Random access | Fast | Slow |
Apart from dynamic memory usage, C also provides memory optimization techniques like bit fields for compact storage.
When to use linked list:
- Frequent insertions
- Unknown data size
- Dynamic memory usage
When to use arrays:
- Index-based access
- Fixed size datasets
- linked list vs array
- dynamic vs static memory
- advantages of linked list
Real-World Applications
Linked list in C language is not just academic. It has real-world applications.
- Operating Systems
Used in:
- Process scheduling
- Memory management
- Music Players
Songs stored as linked list for easy navigation.
- Browser History
Back and forward navigation uses linked structures.
- Undo/Redo Systems
Text editors use linked lists internally.
- Graph Implementations
Adjacency lists use linked list representation.
- applications of linked list
- real life examples of linked list
- where linked lists are used
Many complex systems combine linked lists with trees and graphs to build scalable software architectures.
Common Mistakes Students Make
Avoid these errors while writing linked list programs in C.
- Not Initializing Head
Always initialize head = NULL.
- Forgetting malloc
Linked lists require dynamic memory allocation.
- Not Freeing Memory
Leads to memory leaks.
- Pointer Confusion
Remember:
- -> for pointers
- . for structure variables
- Infinite Loops
Incorrect traversal conditions cause loops.
Real Exam Questions
These are commonly asked in India (BCA, BTech, Polytechnic).
- Write a program to create a singly linked list in C.
- Insert a node at beginning and end.
- Delete a node from linked list.
- Difference between array and linked list.
- Advantages of linked list.
Practicing these improves scoring and interview readiness.
FAQs
What is a linked list in C?
A linked list in C is a dynamic data structure where nodes are connected using pointers instead of contiguous memory.
Why use linked list instead of array?
Linked lists allow dynamic size and faster insertions and deletions.
What is singly linked list in C?
A singly linked list contains nodes where each node points only to the next node.
Is linked list important for interviews?
Yes. It is one of the most frequently asked data structure topics.
Is linked list difficult to learn?
Not if you understand:
- Pointers
- Structures
- Dynamic memory
Conclusion
Linked list in C language is one of the most important data structures every programmer must understand. It provides dynamic memory management and efficient insertion and deletion operations, making it superior to arrays in many scenarios.
From academic exams to coding interviews, linked lists remain a foundational topic. Once you master singly linked list in C, you can easily move to advanced topics like doubly linked lists, circular lists, stacks, queues, and trees.
If you want to build strong programming fundamentals, learning linked list programs in C is a must.
Further Reading: