A Singly Linked List is a fundamental data structure in computer science, commonly used in C programming. It is a linear data structure in which each element, called a node, contains a data field and a pointer to the next node in the sequence. This article provides an overview of singly linked lists, their implementation in C, and their applications.
A Singly Linked List is a dynamic data structure consisting of nodes that are connected sequentially. Each node comprises two parts:
The last node in a singly linked list has a pointer set to
NULL
, indicating the end of the list.
The main operations performed on a singly linked list include:
In C programming, a singly linked list is implemented using structures. Here's how a node is defined:
struct Node { int data; struct Node* next; };
The first step in implementing a singly linked list is to define a function that creates a new node.
struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; }
void insertAtBeginning(struct Node** head, int data) { struct Node* newNode = createNode(data); newNode->next = *head; *head = newNode; }
void insertAtEnd(struct Node** head, int data) { struct Node* newNode = createNode(data); if (*head == NULL) { *head = newNode; return; } struct Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; }
void 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; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); }
Traversal is used to access each node of the linked list.
void traverseList(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); }
Below is an example program demonstrating the implementation of a singly linked list in C:
#include <stdio.h> #include <stdlib.h> 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; } void insertAtEnd(struct Node** head, int data) { struct Node* newNode = createNode(data); if (*head == NULL) { *head = newNode; return; } struct Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } void traverseList(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; insertAtEnd(&head, 10); insertAtEnd(&head, 20); insertAtEnd(&head, 30); printf("Singly Linked List:\n"); traverseList(head); return 0; }
A singly linked list in C is a data structure where each node contains data and a pointer to the next node, forming a sequence.
Singly linked lists are used in dynamic memory allocation, implementation of stacks and queues, and handling hierarchical data structures like trees.
The advantages include dynamic memory allocation, efficient insertion and deletion, and flexibility compared to arrays.
Yes, a singly linked list can be circular, where the last node points back to the first node, forming a loop.
A singly linked list is an essential concept in data structures, providing a dynamic and efficient way to manage data in C programming. Understanding its implementation and operations is crucial for mastering advanced programming and problem-solving techniques.
Copyrights © 2024 letsupdateskills All rights reserved