From 3ef816be807878ad947eb1411e7490b044b2bf43 Mon Sep 17 00:00:00 2001 From: Sidhant Date: Fri, 2 Oct 2020 05:42:26 +0530 Subject: [PATCH] added insertion in sorted linked list algorithm --- .../C-CPP/insert_in_sorted_linked_list.c | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Sorting Algorithms/C-CPP/insert_in_sorted_linked_list.c diff --git a/Sorting Algorithms/C-CPP/insert_in_sorted_linked_list.c b/Sorting Algorithms/C-CPP/insert_in_sorted_linked_list.c new file mode 100644 index 0000000..4494d25 --- /dev/null +++ b/Sorting Algorithms/C-CPP/insert_in_sorted_linked_list.c @@ -0,0 +1,97 @@ +#include +#include + +struct node { + int val; + struct node* next; +}; + +// insert value while keeping linked list sorted +struct node* insertInSorted(struct node *s, int x) { + struct node* p; + p = (struct node*) malloc(sizeof(struct node)); + p->val = x; + if (s == NULL) { + s = p; + p->next = NULL; + } + else if (x <= s->val) { + p->next = s; + s = p; + } + else { + struct node *q = s; + while (q->next != NULL && q->next->val < x) { + q = q->next; + } + p->next = q->next; + q->next = p; + } + return s; +} + +// delete value x in a sorted linked list +struct node* deleteNodeInSorted(struct node* s, int x) { + struct node *p = NULL, *q = s; + if (s == NULL) + return s; + while (q != NULL && q->val < x) { + p = q; + q = q->next; + } + if (q != NULL && q->val == x) { + if (p == NULL) + s = q->next; + else + p->next = q->next; + free(q); + } + return s; +} + +void display(struct node *s) { + struct node *p = s; + while (p != NULL) { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); +} + +int main() { + + struct node *s = NULL; + printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n\n"); + + while (1) { + + int choice, x; + scanf("%d", &choice); + + switch (choice) { + + case 1: + printf("\nEnter value to insert: "); + scanf("%d", &x); + s = insertInSorted(s, x); + break; + + case 2: + printf("\nEnter value to delete: "); + scanf("%d", &x); + s = deleteNodeInSorted(s, x); + break; + + case 3: + display(s); + + case 4: + exit(1); + + default: + printf("Invalid option selected"); + } + } + + return 0; +} \ No newline at end of file