Single linked list in C
0 227
Introduction
A singly linked list is a fundamental data structure in computer science, commonly used for dynamic memory allocation and efficient data storage.
It consists of a sequence of elements called nodes, where each node contains two parts:
data and a pointer to the next node in the sequence.
The final node is linked to NULL, signaling the termination of the list.
Syntax :
Node Definition:
struct Node { int data; struct Node* next; };
This structure delineates a node within the linked list, encapsulating an integer data field and a pointer to the ensuing node.
Initialization of Head Pointer:
struct Node* head = NULL;
The pointer named head references the initial node within the list.
Initially set to NULL, indicating an empty list.
Insertion at the Beginning:
void insertAtBeginning(int newData) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = head; head = newNode; }
Allocates memory for a new node.
Inserts the new node at the beginning of the list.
Traversal and Display:
void display() { struct Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); }
Traverses the list starting from the head node.
Prints the data of each node until NULL is encountered.
Deletion of a Node:
void deleteNode(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); }
Deletes the node containing the given key.
Handles deletion at the beginning, end, and middle of the list.
This implementation provides basic functionality for creating, traversing, inserting, and deleting nodes in a singly linked list in C.
// prgoram for single linklist in c #include<stdio.h>#include<stdlib.h> // Define a structure for a node struct Node { int data; struct Node* next; }; // Function to insert a new node at the beginning of the list void insertAtBeginning(struct Node** headRef, int newData) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed.\n"); return; } newNode->data = newData; newNode->next = *headRef; *headRef = newNode; } // Function to display the linked list 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; // Initialize an empty list // Insert some elements at the beginning insertAtBeginning(&head, 10); insertAtBeginning(&head, 20); insertAtBeginning(&head, 30); // Display the list printf("Linked List: "); display(head); // Free the memory allocated for the nodes struct Node* temp; while (head != NULL) { temp = head; head = head->next; free(temp); } return 0; }
Output:
Linked List: 30 -> 20 -> 10 -> NULL
This program defines a basic singly linked list in C.
It includes functions to insert elements at the beginning of the list and to display the list.
In the main function, some elements are inserted into the list, then the list is displayed, and finally, memory allocated for the nodes is freed to prevent memory leaks.
Share:
Comments
Waiting for your comments