Singly-linked lists offer flexibility in managing data by allowing dynamic deletion of nodes. Imagine you have a train (linked list) where passengers (data) disembark at different stations. This program demonstrates how to delete nodes from various positions within a singly linked list in C.
C Program for Declaring and Deleting Nodes in a Singly Linked List:
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the linked list
typedef struct node {
int data;
struct node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to declare (initialize) an empty linked list
Node* declareList() {
return NULL; // An empty list is represented by a NULL head pointer
}
// Function to delete the first node of the linked list
void deleteAtBeginning(Node** headPtr) {
if (*headPtr == NULL) {
printf("List is empty!\n");
return;
}
Node* temp = *headPtr;
*headPtr = (*headPtr)->next;
free(temp);
}
// Function to delete a specific node from the linked list
void deleteNode(Node** headPtr, int target) {
if (*headPtr == NULL) {
printf("List is empty!\n");
return;
}
Node* prev = NULL;
Node* current = *headPtr;
while (current != NULL && current->data != target) {
prev = current;
current = current->next;
}
if (current == NULL) {
printf("Node with data %d not found!\n", target);
return;
}
if (prev == NULL) { // Deleting the head node
*headPtr = current->next;
} else {
prev->next = current->next;
}
free(current);
}
int main() {
// Declare an empty list
Node* head = declareList();
// Create some nodes
Node* node1 = createNode(10);
Node* node2 = createNode(20);
Node* node3 = createNode(30);
head = node1; // Make node1 the head
node1->next = node2;
node2->next = node3;
printf("Initial list: ");
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
// Delete the first node
deleteAtBeginning(&head);
// Delete the node with data 20
deleteNode(&head, 20);
printf("List after deletions: ");
current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
return 0;
}
Explanation:
Node structure: Defines the basic building block of the list, containing data and a pointer to the next node.
createNode function: Allocates memory for a new node, initializes it with data and a NULL next pointer, and returns the new node.
declareList function: Initializes the head pointer of the list to NULL, signifying an empty list.
deleteAtBeginning function: Takes the head pointer by reference (**headPtr) to modify it. Checks if the list is empty and returns if so. Stores the current head node in a temporary pointer, updates the head pointer to point to the next node, and frees the memory of the deleted node.
deleteNode function: Takes the head pointer by reference and the target data as input. Checks if the list is empty and returns if so. Uses two pointers, prev and current, to iterate through the list. If the target node is found, updates the next pointer of the previous node to skip the deleted node and frees the memory of the deleted node. Handles deleting the head node as a special case.
main function:
Declares an empty list using declareList.
Creates three nodes with
Comments