25.10.25

14. Linked List Operations, The One Where Friends Build and Break the Chain


hello everyone 

After creating a linked list that could only add things, Aarav said, “Cool, but what if I want to delete something?”  Riya immediately replied, “You just want to break things again, don’t you?”  And that’s how we leveled up by learning Insertion, Deletion, and Display in Linked Lists!

What’s New Here?

This program lets you:

  • Insert nodes either at the end or after a specific value 

  • Delete a node that matches a given number 

  • Display the full list ending neatly with NULL 

All nodes are created dynamically using malloc(), so memory is handled smartly.
The user gets a clear menu to choose what to do and the fun keeps going as long as you type “yes”!

How It Works? 

Riya was the insertion expert, linking new nodes like a pro . Aarav, of course, took care of deletion a little too happily . And I (Daksh) proudly handled the display, making sure our linked list looked clean and readable. By the end, we had a flexible list where nodes came and went smoothly  kind of  like friends joining and leaving a group chat, but in C! 

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 struct Node {
     int data;
     struct Node* next;
 };
 
 void insertAtEnd(struct Node** head, int value) {
     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
     newNode->data = value;
     newNode->next = NULL;
     if (*head == NULL) {
         *head = newNode;
         return;
     }
     struct Node* temp = *head;
     while (temp->next != NULL) temp = temp->next;
     temp->next = newNode;
 }
 
 void insertAfter(struct Node* head, int target, int value) {
     struct Node* temp = head;
     while (temp != NULL && temp->data != target) temp = temp->next;
     if (temp == NULL) {
         printf("Target value %d not found\n", target);
         return;
     }
     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
     newNode->data = value;
     newNode->next = temp->next;
     temp->next = newNode;
 }
 
 void deleteNode(struct Node** head, 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) {
         printf("Value %d not found\n", key);
         return;
     }
     prev->next = temp->next;
     free(temp);
 }
 
 void display(struct Node* head) {
     struct Node* temp = head;
     if (temp == NULL) {
         printf("List is empty\n");
         return;
     }
     printf("Linked List: ");
     while (temp != NULL) {
         printf("%d -> ", temp->data);
         temp = temp->next;
     }
     printf("NULL\n");
 }
 
 int main() {
     struct Node* head = NULL;
     char choice[5];
     int value, target, op;
     do {
         printf("\nChoose an operation:\n");
         printf("1. Insert at End\n2. Insert After\n3. Delete\n4. Display\nEnter your choice (1-4): ");
         scanf("%d", &op);
         switch(op) {
             case 1:
                 printf("Enter value to insert: ");
                 scanf("%d", &value);
                 insertAtEnd(&head, value);
                 break;
             case 2:
                 printf("Enter target value: ");
                 scanf("%d", &target);
                 printf("Enter value to insert: ");
                 scanf("%d", &value);
                 insertAfter(head, target, value);
                 break;
             case 3:
                 printf("Enter value to delete: ");
                 scanf("%d", &value);
                 deleteNode(&head, value);
                 break;
             case 4:
                 display(head);
                 break;
             default:
                 printf("Invalid choice!\n");
         }
         printf("\nDo you want to perform another operation? (yes/no): ");
         scanf("%s", choice);
     } while (strcmp(choice, "yes") == 0);
     printf("\nOkay, exiting the program.\n");
     return 0;
 }

Output:

 
← Previous 🏠 Homepage Next Chapter →

No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...