Sure! Below is a simple implementation of a singly linked list in C. We’ll go through it step by step in a humanized way, ensuring that everything is clear.
Program Explanation:
A linked list is a dynamic data structure where each element (or “node”) contains two parts:
- The data (value).
- A pointer to the next node in the list.
In a singly linked list, each node points to the next node, and the last node points to NULL, indicating the end of the list.
Operations We’ll Implement:
- Create a node (append to the end of the list).
- Display the list (traverse and print the list).
- Delete a node (remove a specific node from the list).
Code Implementation:
#include <stdio.h>
#include <stdlib.h>
// Definition of a node
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int value)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Dynamically allocate memory for a new node
if (newNode == NULL)
{
printf("Memory allocation failed!\n");
exit(1); // Exit if memory allocation fails
}
newNode->data = value; // Set the node's data
newNode->next = NULL; // Initially, the next pointer points to NULL (as it's the last node for now)
return newNode;
}
// Function to append a node to the end of the list
void appendNode(struct Node** head, int value)
{
struct Node* newNode = createNode(value);
if (*head == NULL)
{
*head = newNode; // If the list is empty, the new node becomes the head
return;
}
struct Node* temp = *head;
while (temp->next != NULL)
{
temp = temp->next; // Traverse to the last node
}
temp->next = newNode; // Append the new node at the end
}
// Function to display the linked list
void displayList(struct Node* head)
{
if (head == NULL)
{
printf("The list is empty.\n");
return;
}
struct Node* temp = head;
printf("Linked List: ");
while (temp != NULL)
{
printf("%d -> ", temp->data); // Print the data part of each node
temp = temp->next;
}
printf("NULL\n"); // The last node points to NULL
}
// Function to delete a node with a given value
void deleteNode(struct Node** head, int value)
{
if (*head == NULL)
{
printf("The list is empty. Cannot delete.\n");
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
// If the head node itself holds the value to be deleted
if (temp != NULL && temp->data == value)
{
*head = temp->next; // Move the head to the next node
free(temp); // Free the old head node
printf("Node with value %d deleted.\n", value);
return;
}
// Search for the node to be deleted, keeping track of the previous node
while (temp != NULL && temp->data != value)
{
prev = temp;
temp = temp->next;
}
// If the value was not found in the list
if (temp == NULL)
{
printf("Node with value %d not found in the list.\n", value);
return;
}
// Unlink the node from the linked list and free its memory
prev->next = temp->next;
free(temp);
printf("Node with value %d deleted.\n", value);
}
int main()
{
struct Node* head = NULL; // Initialize an empty list
// Append nodes to the list
appendNode(&head, 10);
appendNode(&head, 20);
appendNode(&head, 30);
appendNode(&head, 40);
// Display the list
displayList(head);
// Delete a node and display the list again
deleteNode(&head, 20);
displayList(head);
// Try deleting a node that doesn't exist
deleteNode(&head, 50);
displayList(head);
return 0;
}
Sure! Below is a simple implementation of a singly linked list in C. We’ll go through it step by step in a humanized way, ensuring that everything is clear.
Program Explanation:
A linked list is a dynamic data structure where each element (or “node”) contains two parts:
- The data (value).
- A pointer to the next node in the list.
In a singly linked list, each node points to the next node, and the last node points to NULL, indicating the end of the list.
Operations We’ll Implement:
- Create a node (append to the end of the list).
- Display the list (traverse and print the list).
- Delete a node (remove a specific node from the list).
Code Implementation:
#include <stdio.h>
#include <stdlib.h>
// Definition of a node
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int value)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Dynamically allocate memory for a new node
if (newNode == NULL)
{
printf("Memory allocation failed!\n");
exit(1); // Exit if memory allocation fails
}
newNode->data = value; // Set the node's data
newNode->next = NULL; // Initially, the next pointer points to NULL (as it's the last node for now)
return newNode;
}
// Function to append a node to the end of the list
void appendNode(struct Node** head, int value)
{
struct Node* newNode = createNode(value);
if (*head == NULL)
{
*head = newNode; // If the list is empty, the new node becomes the head
return;
}
struct Node* temp = *head;
while (temp->next != NULL)
{
temp = temp->next; // Traverse to the last node
}
temp->next = newNode; // Append the new node at the end
}
// Function to display the linked list
void displayList(struct Node* head)
{
if (head == NULL)
{
printf("The list is empty.\n");
return;
}
struct Node* temp = head;
printf("Linked List: ");
while (temp != NULL)
{
printf("%d -> ", temp->data); // Print the data part of each node
temp = temp->next;
}
printf("NULL\n"); // The last node points to NULL
}
// Function to delete a node with a given value
void deleteNode(struct Node** head, int value)
{
if (*head == NULL)
{
printf("The list is empty. Cannot delete.\n");
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
// If the head node itself holds the value to be deleted
if (temp != NULL && temp->data == value)
{
*head = temp->next; // Move the head to the next node
free(temp); // Free the old head node
printf("Node with value %d deleted.\n", value);
return;
}
// Search for the node to be deleted, keeping track of the previous node
while (temp != NULL && temp->data != value)
{
prev = temp;
temp = temp->next;
}
// If the value was not found in the list
if (temp == NULL)
{
printf("Node with value %d not found in the list.\n", value);
return;
}
// Unlink the node from the linked list and free its memory
prev->next = temp->next;
free(temp);
printf("Node with value %d deleted.\n", value);
}
int main()
{
struct Node* head = NULL; // Initialize an empty list
// Append nodes to the list
appendNode(&head, 10);
appendNode(&head, 20);
appendNode(&head, 30);
appendNode(&head, 40);
// Display the list
displayList(head);
// Delete a node and display the list again
deleteNode(&head, 20);
displayList(head);
// Try deleting a node that doesn't exist
deleteNode(&head, 50);
displayList(head);
return 0;
}
Breakdown of the Code:
- Node Structure: Each node contains:
- An integer
datafield. - A pointer
nextthat points to the next node in the list.
- An integer
- Creating a Node: The
createNode()function allocates memory for a new node usingmalloc(), assigns the value passed to it, and sets thenextpointer toNULL. - Appending a Node: The
appendNode()function adds a new node at the end of the list. It checks if the list is empty, in which case it directly assigns the new node as the head. Otherwise, it traverses to the last node and attaches the new node. - Displaying the List: The
displayList()function traverses the list from the head and prints each node’s data followed by an arrow (->) until it reaches the last node, which points toNULL. - Deleting a Node: The
deleteNode()function searches for a node with a given value. If found, it unlinks the node from the list and frees the memory. Special cases are handled, such as deleting the head node or when the value is not found.
Output of the Program:
Linked List: 10 -> 20 -> 30 -> 40 -> NULL
Node with value 20 deleted.
Linked List: 10 -> 30 -> 40 -> NULL
Node with value 50 not found in the list.
Linked List: 10 -> 30 -> 40 -> NULL
Explanation of Output:
- Initially, the linked list has four nodes: 10, 20, 30, and 40.
- After deleting the node with value 20, the list becomes: 10 -> 30 -> 40 -> NULL.
- When trying to delete the node with value 50, which doesn’t exist, the program notifies the user and the list remains unchanged.