diff --git a/C/linked_list.c b/C/linked_list.c index a395deebb..9f8bd37a3 100644 --- a/C/linked_list.c +++ b/C/linked_list.c @@ -1,13 +1,12 @@ -// Singly LinkedList with all operaions in C language - #include #include -struct node -{ + +struct node { int data; struct node *next; }; -struct node *head; + +struct node *head = NULL; // Initialize head to NULL void beginsert(); void lastinsert(); @@ -17,247 +16,206 @@ void last_delete(); void random_delete(); void display(); void search(); -void main() -{ + +int main() { int choice = 0; - while (choice != 9) - { - printf("\n\n*********Main Menu*********\n"); - printf("\nChoose one option from the following list ...\n"); - printf("\n===============================================\n"); - printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\n9.Exit\n"); - printf("\nEnter your choice?\n"); - scanf("\n%d", &choice); - switch (choice) - { - case 1: - beginsert(); - break; - case 2: - lastinsert(); - break; - case 3: - randominsert(); - break; - case 4: - begin_delete(); - break; - case 5: - last_delete(); - break; - case 6: - random_delete(); - break; - case 7: - search(); - break; - case 8: - display(); - break; - case 9: - exit(0); - break; - default: - printf("Please enter valid choice.."); + while (choice != 9) { + printf("\n\n********* Main Menu *********\n"); + printf("1. Insert at Beginning\n"); + printf("2. Insert at Last\n"); + printf("3. Insert at Random Location\n"); + printf("4. Delete from Beginning\n"); + printf("5. Delete from Last\n"); + printf("6. Delete Node at Specified Location\n"); + printf("7. Search for an Element\n"); + printf("8. Display List\n"); + printf("9. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: beginsert(); break; + case 2: lastinsert(); break; + case 3: randominsert(); break; + case 4: begin_delete(); break; + case 5: last_delete(); break; + case 6: random_delete(); break; + case 7: search(); break; + case 8: display(); break; + case 9: printf("Exiting...\n"); break; + default: printf("Please enter a valid choice.\n"); } } + return 0; } -void beginsert() -{ - struct node *ptr; - int item; - ptr = (struct node *)malloc(sizeof(struct node *)); - if (ptr == NULL) - { - printf("\nOVERFLOW"); - } - else - { - printf("\nEnter value\n"); - scanf("%d", &item); - ptr->data = item; - ptr->next = head; - head = ptr; - printf("\nNode inserted"); - } + +void beginsert() { + struct node *ptr = (struct node *)malloc(sizeof(struct node)); + if (ptr == NULL) { + printf("Memory allocation failed\n"); + return; + } + printf("Enter value: "); + scanf("%d", &ptr->data); + ptr->next = head; + head = ptr; + printf("Node inserted at beginning\n"); } -void lastinsert() -{ - struct node *ptr, *temp; - int item; - ptr = (struct node *)malloc(sizeof(struct node)); - if (ptr == NULL) - { - printf("\nOVERFLOW"); - } - else - { - printf("\nEnter value?\n"); - scanf("%d", &item); - ptr->data = item; - if (head == NULL) - { - ptr->next = NULL; - head = ptr; - printf("\nNode inserted"); - } - else - { - temp = head; - while (temp->next != NULL) - { - temp = temp->next; - } - temp->next = ptr; - ptr->next = NULL; - printf("\nNode inserted"); + +void lastinsert() { + struct node *ptr = (struct node *)malloc(sizeof(struct node)); + if (ptr == NULL) { + printf("Memory allocation failed\n"); + return; + } + printf("Enter value: "); + scanf("%d", &ptr->data); + ptr->next = NULL; + + if (head == NULL) { + head = ptr; + } else { + struct node *temp = head; + while (temp->next != NULL) { + temp = temp->next; } + temp->next = ptr; } + printf("Node inserted at last\n"); } -void randominsert() -{ - int i, loc, item; - struct node *ptr, *temp; - ptr = (struct node *)malloc(sizeof(struct node)); - if (ptr == NULL) - { - printf("\nOVERFLOW"); - } - else - { - printf("\nEnter element value"); - scanf("%d", &item); - ptr->data = item; - printf("\nEnter the location after which you want to insert "); - scanf("\n%d", &loc); - temp = head; - for (i = 0; i < loc; i++) - { + +void randominsert() { + int loc, i; + struct node *ptr = (struct node *)malloc(sizeof(struct node)); + if (ptr == NULL) { + printf("Memory allocation failed\n"); + return; + } + + printf("Enter value: "); + scanf("%d", &ptr->data); + printf("Enter the position after which to insert (0 for beginning): "); + scanf("%d", &loc); + + if (loc == 0) { + ptr->next = head; + head = ptr; + printf("Node inserted at beginning\n"); + } else { + struct node *temp = head; + for (i = 0; i < loc && temp != NULL; i++) { temp = temp->next; - if (temp == NULL) - { - printf("\ncan't insert\n"); - return; - } } - ptr->next = temp->next; - temp->next = ptr; - printf("\nNode inserted"); + if (temp == NULL) { + printf("Position out of bounds\n"); + free(ptr); + } else { + ptr->next = temp->next; + temp->next = ptr; + printf("Node inserted\n"); + } } } -void begin_delete() -{ - struct node *ptr; - if (head == NULL) - { - printf("\nList is empty\n"); - } - else - { - ptr = head; - head = ptr->next; - free(ptr); - printf("\nNode deleted from the begining ...\n"); - } + +void begin_delete() { + if (head == NULL) { + printf("List is empty\n"); + return; + } + struct node *temp = head; + head = head->next; + free(temp); + printf("Node deleted from beginning\n"); } -void last_delete() -{ - struct node *ptr, *ptr1; - if (head == NULL) - { - printf("\nlist is empty"); + +void last_delete() { + if (head == NULL) { + printf("List is empty\n"); + return; } - else if (head->next == NULL) - { - head = NULL; + + if (head->next == NULL) { free(head); - printf("\nOnly node of the list deleted ...\n"); + head = NULL; + printf("Only node deleted, list is now empty\n"); + return; } - else - { - ptr = head; - while (ptr->next != NULL) - { - ptr1 = ptr; - ptr = ptr->next; - } - ptr1->next = NULL; - free(ptr); - printf("\nDeleted Node from the last ...\n"); + struct node *temp = head; + while (temp->next->next != NULL) { + temp = temp->next; } + free(temp->next); + temp->next = NULL; + printf("Node deleted from last\n"); } -void random_delete() -{ - struct node *ptr, *ptr1; + +void random_delete() { int loc, i; - printf("\n Enter the location of the node after which you want to perform deletion \n"); + printf("Enter the position of the node to delete (0 for beginning): "); scanf("%d", &loc); - ptr = head; - for (i = 0; i < loc; i++) - { - ptr1 = ptr; - ptr = ptr->next; - - if (ptr == NULL) - { - printf("\nCan't delete"); - return; - } + + if (head == NULL) { + printf("List is empty\n"); + return; + } + + if (loc == 0) { + begin_delete(); + return; + } + + struct node *temp = head; + for (i = 0; i < loc - 1 && temp != NULL; i++) { + temp = temp->next; + } + + if (temp == NULL || temp->next == NULL) { + printf("Position out of bounds\n"); + return; } - ptr1->next = ptr->next; - free(ptr); - printf("\nDeleted node %d ", loc + 1); + + struct node *to_delete = temp->next; + temp->next = to_delete->next; + free(to_delete); + printf("Node deleted from position %d\n", loc); } -void search() -{ - struct node *ptr; - int item, i = 0, flag; - ptr = head; - if (ptr == NULL) - { - printf("\nEmpty List\n"); + +void search() { + int item, i = 0; + struct node *ptr = head; + + if (ptr == NULL) { + printf("List is empty\n"); + return; } - else - { - printf("\nEnter item which you want to search?\n"); - scanf("%d", &item); - while (ptr != NULL) - { - if (ptr->data == item) - { - printf("item found at location %d ", i + 1); - flag = 0; - } - else - { - flag = 1; - } - i++; - ptr = ptr->next; - } - if (flag == 1) - { - printf("Item not found\n"); + + printf("Enter item to search: "); + scanf("%d", &item); + + while (ptr != NULL) { + if (ptr->data == item) { + printf("Item found at position %d\n", i); + return; } + ptr = ptr->next; + i++; } + printf("Item not found\n"); } -void display() -{ - struct node *ptr; - ptr = head; - if (ptr == NULL) - { - printf("Nothing to print"); - } - else - { - printf("\nprinting values . . . . .\n"); - while (ptr != NULL) - { - printf("\n%d", ptr->data); - ptr = ptr->next; - } +void display() { + struct node *ptr = head; + + if (ptr == NULL) { + printf("List is empty\n"); + return; + } + + printf("List contents:\n"); + while (ptr != NULL) { + printf("%d -> ", ptr->data); + ptr = ptr->next; } -} \ No newline at end of file + printf("NULL\n"); +}