Singly linked lists are one of the fundamental data structures in computer programming. Understanding how to implement a singly linked list in C is essential for beginners and intermediate learners alike. In this article, we will explore the concept of linked lists, their advantages, common operations, practical code examples, and real-world use cases.
A singly linked list is a linear data structure where each element, called a node, contains data and a pointer to the next node. Unlike arrays, linked lists do not require contiguous memory locations, which makes them more flexible for dynamic data management.
Each node in a singly linked list contains two parts:
struct Node { int data; // Data part struct Node* next; // Pointer to the next node };
Here are the basic operations performed on a linked list in C programming:
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void display(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; // Allocate nodes head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); // Assign data head->data = 10; head->next = second; second->data = 20; second->next = third; third->data = 30; third->next = NULL; // Display list display(head); return 0; }
Explanation: We created a linked list with three nodes and displayed it using a traversal function. Each node points to the next node, and the last node points to
NULL.
// Insert at beginning struct Node* insertAtBeginning(struct Node* head, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = head; head = newNode; return head; } // Insert at end struct Node* insertAtEnd(struct Node* head, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { return newNode; } struct Node* temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; return head; }
struct Node* deleteNode(struct Node* head, int key) { struct Node* temp = head; struct Node* prev = NULL; if (temp != NULL && temp->data == key) { head = temp->next; free(temp); return head; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return head; prev->next = temp->next; free(temp); return head; }
Singly linked lists are widely used in scenarios where dynamic memory allocation and flexible data insertion/deletion are required:
| Feature | Array | Singly Linked List |
|---|---|---|
| Memory | Contiguous allocation | Dynamic allocation |
| Insertion/Deletion | Expensive (requires shifting) | Efficient (just pointer changes) |
| Access | Direct access (O(1)) | Sequential access (O(n)) |
| Size | Fixed | Flexible |
This example demonstrates how to create a simple singly linked list in C. We will create three nodes and link them together. This is a fundamental step for understanding more advanced linked list operations like insertion, deletion, and traversal.
Each node contains two parts: data and a pointer to the next node.
struct Node { int data; // Store data struct Node* next; // Pointer to the next node };
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int main() { // Create nodes struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; // Allocate memory head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); // Assign data and link nodes head->data = 10; head->next = second; second->data = 20; second->next = third; third->data = 30; third->next = NULL; // Display linked list struct Node* temp = head; printf("Linked list: "); while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); return 0; }
When you run this program, the output will be:
Linked list: 10 -> 20 -> 30 -> NULL
Implementing a singly linked list in C is a fundamental skill for programmers. Linked lists provide dynamic memory management, efficient insertion/deletion, and are versatile in real-world applications. By understanding nodes, traversal, insertion, and deletion operations, beginners and intermediate learners can build a strong foundation for advanced data structures.
A singly linked list has nodes pointing only to the next node, while a doubly linked list has nodes pointing to both previous and next nodes, making traversal in both directions possible.
No. Pointers are essential in C for linking nodes dynamically, as they provide the ability to point to memory locations of subsequent nodes.
Traversal involves starting at the head node and iterating through each node using the next pointer until NULL is reached, processing each node’s data along the way.
Linked lists are dynamic in size, allow efficient insertion/deletion without shifting elements, and utilize memory more flexibly compared to arrays.
Yes. Singly linked lists can store duplicate values since each node is independent, and there is no restriction on data uniqueness.
Copyrights © 2024 letsupdateskills All rights reserved