#include <stdio.h>
#include <stdlib.h>
// Structure for a node in the singly linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Insertion at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
printf("Inserted %d at the beginning\n", data);
}
// Insertion at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
printf("Inserted %d at the end\n", data);
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
printf("Inserted %d at the end\n", data);
}
// Insertion after a given node (by value)
void insertAfter(struct Node* head, int afterData, int data) {
struct Node* temp = head;
while (temp != NULL && temp->data != afterData) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found\n", afterData);
return;
}
struct Node* newNode = createNode(data);
newNode->next = temp->next;
temp->next = newNode;
printf("Inserted %d after %d\n", data, afterData);
}
// Deletion of the first node
void deleteFirst(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
printf("Deleted %d from the beginning\n", temp->data);
free(temp);
}
// Deletion of the last node
void deleteLast(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
if ((*head)->next == NULL) {
printf("Deleted %d from the end\n", (*head)->data);
free(*head);
*head = NULL;
return;
}
struct Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
}
printf("Deleted %d from the end\n", temp->next->data);
free(temp->next);
temp->next = NULL;
}
// Deletion of a node with a specific value
void deleteByValue(struct Node** head, int data) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
if ((*head)->data == data) {
struct Node* temp = *head;
*head = (*head)->next;
printf("Deleted %d\n", data);
free(temp);
return;
}
struct Node* temp = *head;
while (temp->next != NULL && temp->next->data != data) {
temp = temp->next;
}
if (temp->next == NULL) {
printf("Node with data %d not found\n", data);
return;
}
struct Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
printf("Deleted %d\n", data);
free(nodeToDelete);
}
// Display the linked list
void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}
printf("Linked List: ");
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function to test the linked list operations
int main() {
struct Node* head = NULL;
// Insert some nodes
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
displayList(head);
// Insert at the beginning
insertAtBeginning(&head, 5);
displayList(head);
// Insert after a node
insertAfter(head, 20, 25);
displayList(head);
// Delete operations
deleteFirst(&head);
displayList(head);
deleteLast(&head);
displayList(head);
deleteByValue(&head, 20);
displayList(head);
return 0;
}
Output:



No comments:
Post a Comment