From b883874dee88ce681778581be10c4aab3f168509 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 10 Sep 2024 11:30:56 +0530 Subject: [PATCH] Update DS/C/Lab/Week7/linkedlist.c --- DS/C/Lab/Week7/linkedlist.c | 54 +++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/DS/C/Lab/Week7/linkedlist.c b/DS/C/Lab/Week7/linkedlist.c index 6dc0c3b..728a60d 100644 --- a/DS/C/Lab/Week7/linkedlist.c +++ b/DS/C/Lab/Week7/linkedlist.c @@ -6,7 +6,56 @@ typedef struct Node struct Node *next; } Node; +// case 3 +Node *delEle(Node *head, int data) +{ + if (head == NULL) + { + printf("Linked list is empty.\n"); + return NULL; + } + Node *current = head; + Node *prev = NULL; + while (current != NULL && current->data != data) + { + prev = current; + current = current->next; + } + if (current == NULL) + { + printf("Element not found in the linked list.\n"); + return head; + } + if (prev == NULL) + { + head = current->next; + } + else + { + prev->next = current->next; + } + free(current); + printf("Element deleted successfully.\n"); + return head; +} +// case 4 +void dispList(Node *head) +{ + if (head == NULL) + { + printf("Linked list is empty.\n"); + return; + } + Node *current = head; + printf("Linked list elements: "); + while (current != NULL) + { + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} int main() { @@ -41,7 +90,7 @@ int main() break; case 4: printf("List traversal"); - dispList(); + dispList(head); break; case 5: printf("The reversed link list is: "); @@ -53,11 +102,12 @@ int main() break; case 7: printf("The list after deleting every alternate node is:"); + delAltNode(); break; case 8: printf("Enter the element you want to insert: "); scanf("%d", &data); - enterSortList(); + enterSortList(data); break; } }