- Identify the node that you want to delete. Let's call this node node_to_delete.
- Find the node before node_to_delete, and let's call this node prev.
- Set prev->next to point to the node after node_to_delete, rather than node_to_delete.
- Free the memory occupied by node_to_delete.
Here's some example code that demonstrates how to delete a node from a linked list in C:
struct node {
int data;
struct node* next;
};
void delete_node(struct node** head, int data) {
// Find the node to delete
struct node* node_to_delete = *head;
while (node_to_delete != NULL && node_to_delete->data != data) {
node_to_delete = node_to_delete->next;
}
// If the node was not found, return
if (node_to_delete == NULL) {
return;
}
// Find the node before the node to delete
struct node* prev = *head;
while (prev->next != node_to_delete) {
prev = prev->next;
}
// Unlink the node from the list
prev->next = node_to_delete->next;
// Free the memory occupied by the node
free(node_to_delete);
}
This code assumes that the linked list has a structure defined as follows:
struct node {
int data;
struct node* next;
};
Each node in the list contains an int value (data) and a pointer to the next node in the list (next). The head of the list is a pointer to the first node in the list.
Here are two examples of how to delete a node from a linked list in C:
Example 1:
In this example, we will delete a node from a linked list of integers. The linked list is implemented using a structure with two fields: an integer value (data) and a pointer to the next node in the list (next).
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
// Function to delete a node from the linked list
void delete_node(struct node** head, int data) {
// Find the node to delete
struct node* node_to_delete = *head;
while (node_to_delete != NULL && node_to_delete->data != data) {
node_to_delete = node_to_delete->next;
}
// If the node was not found, return
if (node_to_delete == NULL) {
return;
}
// Find the node before the node to delete
struct node* prev = *head;
while (prev->next != node_to_delete) {
prev = prev->next;
}
// Unlink the node from the list
prev->next = node_to_delete->next;
// Free the memory occupied by the node
free(node_to_delete);
}
// Function to print the linked list
void print_list(struct node* head) {
struct node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
// Create a linked list with three nodes
struct node* head = malloc(sizeof(struct node));
head->data = 1;
head->next = malloc(sizeof(struct node));
head->next->data = 2;
head->next->next = malloc(sizeof(struct node));
head->next->next->data = 3;
head->next->next->next = NULL;
// Print the linked list
printf("Original list: ");
print_list(head);
// Delete the second node (contains data 2)
delete_node(&head, 2);
// Print the updated linked list
printf("Updated list: ");
print_list(head);
return 0;
}
Output
Original list: 1 2 3
Updated list: 1 3
Example 2:
In this example, we will delete a node from a linked list of strings. The linked list is implemented using a structure with two fields: a string value (data) and a pointer to the next node in the list (next).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char* data;
struct node* next;
};
// Function to delete a node from the linked list
void delete_node(struct node** head, char* data) {
// Find the node to delete
struct node* node_to
0 Comments