Code
public
Mar 24, 2025
Never
23
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 // Node structure definition 5 struct Node { 6 int data; 7 struct Node* next; 8 }; 9 10 // Function to create a new node 11 struct Node* createNode(int data) { 12 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 13 if (!newNode) { 14 printf("Memory allocation failed!\n"); 15 exit(1); 16 } 17 newNode->data = data; 18 newNode->next = NULL; 19 return newNode; 20 } 21 22 // Function to insert a node at the end 23 void insertAtEnd(struct Node** head, int data) { 24 struct Node* newNode = createNode(data); 25 if (*head == NULL) { 26 *head = newNode; 27 return; 28 } 29 30 struct Node* temp = *head; 31 while (temp->next != NULL) 32 temp = temp->next; 33 34 temp->next = newNode; 35 } 36 37 // Function to insert a node at the beginning 38 void insertAtBeginning(struct Node** head, int data) { 39 struct Node* newNode = createNode(data); 40 newNode->next = *head; 41 *head = newNode; 42 } 43 44 // Function to delete a node with a given key 45 void deleteNode(struct Node** head, int key) { 46 struct Node* temp = *head, *prev = NULL; 47 48 // If head node itself holds the key 49 if (temp != NULL && temp->data == key) { 50 *head = temp->next; 51 free(temp); 52 return; 53 } 54 55 // Search for the key 56 while (temp != NULL && temp->data != key) { 57 prev = temp; 58 temp = temp->next; 59 } 60 61 // Key not found 62 if (temp == NULL) { 63 printf("Node with value %d not found!\n", key); 64 return; 65 } 66 67 // Unlink the node and free memory 68 prev->next = temp->next; 69 free(temp); 70 } 71 72 // Function to search for a node 73 int search(struct Node* head, int key) { 74 struct Node* temp = head; 75 while (temp != NULL) { 76 if (temp->data == key) 77 return 1; // Found 78 temp = temp->next; 79 } 80 return 0; // Not found 81 } 82 83 // Function to display the linked list 84 void display(struct Node* head) { 85 struct Node* temp = head; 86 if (head == NULL) { 87 printf("List is empty!\n"); 88 return; 89 } 90 while (temp != NULL) { 91 printf("%d -> ", temp->data); 92 temp = temp->next; 93 } 94 printf("NULL\n"); 95 } 96 97 // Main function to demonstrate linked list operations 98 int main() { 99 struct Node* head = NULL; 100 101 int choice, value, key; 102 while (1) { 103 printf("\nMenu:\n"); 104 printf("1. Insert at Beginning\n"); 105 printf("2. Insert at End\n"); 106 printf("3. Delete Node\n"); 107 printf("4. Search Node\n"); 108 printf("5. Display List\n"); 109 printf("6. Exit\n"); 110 printf("Enter your choice: "); 111 scanf("%d", &choice); 112 113 switch (choice) { 114 case 1: 115 printf("Enter value to insert at beginning: "); 116 scanf("%d", &value); 117 insertAtBeginning(&head, value); 118 break; 119 120 case 2: 121 printf("Enter value to insert at end: "); 122 scanf("%d", &value); 123 insertAtEnd(&head, value); 124 break; 125 126 case 3: 127 printf("Enter value to delete: "); 128 scanf("%d", &key); 129 deleteNode(&head, key); 130 break; 131 132 case 4: 133 printf("Enter value to search: "); 134 scanf("%d", &key); 135 printf("Search result: %s\n", search(head, key) ? "Found" : "Not Found"); 136 break; 137 138 case 5: 139 printf("Linked List: "); 140 display(head); 141 break; 142 143 case 6: 144 printf("Exiting program...\n"); 145 exit(0); 146 break; 147 148 default: 149 printf("Invalid choice! Please enter a valid option.\n"); 150 } 151 } 152 153 return 0; 154 }