Linked List in C
A linked list is a dynamic data structure made of nodes, where each node contains data and a pointer to the next node.
Unlike arrays, linked lists do not require continuous memory and can grow or shrink at runtime.
What is a Node?
A node is the basic unit of a linked list. Every node contains:
- Data (value stored)
- Pointer to the next node
struct Node {
int data;
struct Node *next;
};
Linked List Structure (Diagram)
HEAD → [data | next] → [data | next] → [data | next] → NULL
Each node points to the next node, forming a chain.
Why Use Linked Lists?
- No need for continuous memory
- Efficient insertion & deletion anywhere
- Dynamic size – grows with memory availability
- Useful for stacks, queues, graphs, hashing
Types of Linked Lists
- Singly Linked List – Node points to next
- Doubly Linked List – Next + Previous pointers
- Circular Linked List – Last node points to first
Basic Operations
- Create Node
- Insert Node (beginning, end, position)
- Delete Node
- Traverse List
- Search Element
Examples
Example 1 – Create a Single Node
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = malloc(sizeof(struct Node));
head->data = 10;
head->next = NULL;
printf("Node value: %d", head->data);
free(head);
return 0;
}
Example 2 – Insert at Beginning
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertBegin(struct Node **head, int value) {
struct Node *new = malloc(sizeof(struct Node));
new->data = value;
new->next = *head;
*head = new;
}
int main() {
struct Node *head = NULL;
insertBegin(&head, 30);
insertBegin(&head, 20);
insertBegin(&head, 10);
struct Node *temp = head;
while(temp){
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL");
return 0;
}
Example 3 – Insert at End
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insertEnd(struct Node **head, int val){
struct Node *new = malloc(sizeof(struct Node));
new->data = val;
new->next = NULL;
if(*head == NULL){
*head = new;
return;
}
struct Node *temp = *head;
while(temp->next)
temp = temp->next;
temp->next = new;
}
int main(){
struct Node *head = NULL;
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
struct Node *temp=head;
while(temp){
printf("%d -> ", temp->data);
temp=temp->next;
}
printf("NULL");
return 0;
}
Example 4 – Delete First Node
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void deleteFirst(struct Node **head){
if(*head == NULL) return;
struct Node *temp = *head;
*head = temp->next;
free(temp);
}
int main(){
struct Node *head = NULL;
insertEnd(&head,10);
insertEnd(&head,20);
insertEnd(&head,30);
deleteFirst(&head);
struct Node *temp=head;
while(temp){
printf("%d -> ", temp->data);
temp=temp->next;
}
printf("NULL");
return 0;
}
Example 5 – Search a Value
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int search(struct Node *head, int key){
while(head){
if(head->data == key) return 1;
head = head->next;
}
return 0;
}
int main(){
struct Node *head=NULL;
insertEnd(&head,5);
insertEnd(&head,10);
insertEnd(&head,15);
if(search(head, 10))
printf("Found");
else
printf("Not Found");
return 0;
}