25.10.25

13. Linked List Operations, The One Where Friends Build a Chain Together

 

hello everyone 

After sorting all those arrays, Rahul said, “Let’s do something cooler arrays are too fixed!” Aarav was like, “So what now, floating arrays?”  And that’s when we jumped into Linked Lists flexible, dynamic, and way smarter! 

What’s Happening Here?

This C program lets you:

  • Insert new elements at the beginning 

  • Search for any value 

  • Display the whole linked list nicely NULL

Each node is created using malloc(), so memory is used only when needed no wasted space!
Plus, a simple menu system lets you keep adding, searching, or displaying until you type “no.”

How It Works 

Rahul handled the insertion part he linked nodes faster than anyone!  Aarav did the searching, though he sometimes looked for numbers that didn’t even exist . And I (Daksh) took care of the display, making sure every node proudly pointed to the next ending with a perfect NULLBy the end, we had a beautiful linked list like a friendship chain, 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 insertAtBeginning(struct Node** head, int value) {
     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
     if (!newNode) {
         printf("Memory allocation failed!\n");
         return;
     }
     newNode->data = value;
     newNode->next = *head;
     *head = newNode;
 }
 
 void search(struct Node* head, int key) {
     struct Node* currentNode = head;
     int position = 1;
     while (currentNode != NULL) {
         if (currentNode->data == key) {
             printf("Value %d found at position %d\n", key, position);
             return;
         }
         currentNode = currentNode->next;
         position++;
     }
     printf("Value %d not found in the list\n", key);
 }
 
 void display(struct Node* head) {
     struct Node* currentNode = head;
     if (currentNode == NULL) {
         printf("List is empty\n");
         return;
     }
     printf("Linked List: ");
     while (currentNode != NULL) {
         printf("%d -> ", currentNode->data);
         currentNode = currentNode->next;
     }
     printf("NULL\n");
 }
 
 int main() {
     struct Node* head = NULL;
     char continueChoice[5];
     int userValue, searchKey;
     do {
         printf("\nChoose an operation:\n");
         printf("1. Insert at Beginning\n2. Search\n3. Display\nEnter your choice (1-3): ");
         
         if (scanf("%d", &userValue) != 1) {
             while (getchar() != '\n');
             printf("Invalid input. Try again.\n");
             continue;
         }
         int menuOption = userValue;
 
         switch(menuOption) {
             case 1:
                 printf("Enter value to insert: ");
                 while (scanf("%d", &userValue) != 1) {
                     while (getchar() != '\n');
                     printf("Invalid input. Enter an integer: ");
                 }
                 insertAtBeginning(&head, userValue);
                 break;
             case 2:
                 printf("Enter value to search: ");
                 while (scanf("%d", &searchKey) != 1) {
                     while (getchar() != '\n');
                     printf("Invalid input. Enter an integer: ");
                 }
                 search(head, searchKey);
                 break;
             case 3:
                 display(head);
                 break;
             default:
                 printf("Invalid choice!\n");
         }
 
         printf("\nDo you want to perform another operation? (yes/no): ");
         scanf("%s", continueChoice);
         while (getchar() != '\n');
     } while (strcmp(continueChoice, "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 ...