diff --git a/data_structures/linked_list/singly_link_list_deletion.c b/data_structures/linked_list/singly_link_list_deletion.c index 2df425ce0..f94aef54b 100644 --- a/data_structures/linked_list/singly_link_list_deletion.c +++ b/data_structures/linked_list/singly_link_list_deletion.c @@ -4,39 +4,64 @@ when passed with a key of the node. */ #include +#include +#include struct node { int info; struct node *link; }; struct node *start = NULL; -/////////////////////////////////////////////////////////// -struct node *createnode() // function to create node +////////////////////////////////////////////////////////////////// +struct node *createnode() // function to create node { struct node *t; t = (struct node *)malloc(sizeof(struct node)); return (t); } -//////////////////////////////////////////////////////// -void insert() // function to insert at first location +////////////////////////////////////////////////////////////////// +int insert(int pos, int d) { - struct node *p; - p = createnode(); - printf("\nenter the number to insert"); - scanf("%d", &p->info); - p->link = NULL; - if (start == NULL) + struct node *new; + new = createnode(); + new->info = d; + if (pos == 1) { - start = p; + new->link = NULL; + if (start == NULL) + { + start = new; + } + else + { + new->link = start; + start = new; + } } else { - p->link = start; - start = p; + struct node *pre = start; + for (int i = 2; i < pos; i++) + { + if (pre == NULL) + { + break; + } + pre = pre->link; + } + if(pre==NULL) + { + printf("Position not found!"); + return 0; + } + new->link = pre->link; + pre->link = new; } -} -/////////////////////////////////////////////////////////// -void deletion() // function to delete from first position + return 0; + } + +/////////////////////////////////////////////////////////////////// +int deletion(int pos) // function to delete from any position { struct node *t; if (start == NULL) @@ -45,14 +70,34 @@ void deletion() // function to delete from first position } else { - struct node *p; - p = start; - start = start->link; - free(p); + if (pos == 1) + { + struct node *p; + p = start; + start = start->link; + free(p); + } + else + { + struct node *prev = start; + for (int i = 2; i < pos; i++) + { + if (prev == NULL) + { + printf("Position not found!"); + return 0; + } + prev = prev->link; + } + struct node *n = prev->link; // n points to required node to be deleted + prev->link = n->link; + free(n); + } } + return 0; } -/////////////////////////////////////////////////////// -void viewlist() // function to display values +/////////////////////////////////////////////////////////////////// +void viewlist() // function to display values { struct node *p; if (start == NULL) @@ -69,32 +114,64 @@ void viewlist() // function to display values } } } -////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////// +static void test() +{ + insert(1, 39); + assert(start->info == 39); + insert(2, 10); + insert(3, 11); + deletion(1); + assert(start->info != 39); + printf("Self-tests successfully passed!\n"); +} +////////////////////////////////////////////////////////////////// int main() { - int n; - while (1) + int n = 0, pos = 0, p = 0, num = 0, c = 0; + printf("\n1.self test mode"); + printf("\n2.interactive mode"); + printf("\nenter your choice:"); + scanf("%d", &c); + if (c == 1) { - printf("\n1.add value at first location"); - printf("\n2.delete value from first location"); - printf("\n3.view value"); - printf("\nenter your choice"); - scanf("%d", &n); - switch (n) + test(); + } + else if (c == 2) + { + while (1) { - case 1: - insert(); - break; - case 2: - deletion(); - break; - case 3: - viewlist(); - break; - default: - printf("\ninvalid choice"); + printf("\n1.add value at the given location"); + printf("\n2.delete value at the given location"); + printf("\n3.view list"); + printf("\nenter your choice :"); + scanf("%d", &n); + switch (n) + { + case 1: + printf("enter the position where the element is to be added :"); + scanf("%d", &p); + printf("enter the element is to be added :"); + scanf("%d", &num); + insert(p, num); + break; + case 2: + printf("enter the position where the element is to be deleted :"); + scanf("%d", &pos); + deletion(pos); + break; + case 3: + viewlist(); + break; + default: + printf("\ninvalid choice"); + } } } - return (0); + else + { + printf("Invalid choice"); + } + return 0; }