From 9f0f3557f2366a647bf9e014563be8f1c814f70d Mon Sep 17 00:00:00 2001 From: khushboo nijhawan <79616524+KhushbooNijhawan@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:03:16 +0530 Subject: [PATCH] josephus problem --- algorithms/C/linked-lists/josephusProblem.c | 88 +++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 algorithms/C/linked-lists/josephusProblem.c diff --git a/algorithms/C/linked-lists/josephusProblem.c b/algorithms/C/linked-lists/josephusProblem.c new file mode 100644 index 00000000..35d1da02 --- /dev/null +++ b/algorithms/C/linked-lists/josephusProblem.c @@ -0,0 +1,88 @@ +#include +#include +// KHUSHBOO NIJAHWAN +typedef struct Node{ + int data; + struct Node *next, *prev; +}node; + +node *BML(int n) +{ + node *head = NULL; + node *ptr = head, *ptr_prev = head; + + for(int i=0; idata = i+1; + head->next = head->prev = head; + ptr_prev = head; + } + else{ + ptr = (node*)malloc(sizeof(node)); + ptr->data = i+1; + ptr_prev->next = ptr; + ptr->prev = ptr_prev; + ptr_prev = ptr; + } + } + ptr->next = head; + head->prev = ptr; + return head; +} + +void display(node *head) +{ + node *ptr = head; + do{ + printf("%d-> ", ptr->data); + ptr = ptr->next; + }while(ptr != head); + printf("\b\b\b \n"); +} + +void display_rev(node *head) +{ + node *ptr = head; + do{ + printf("%d-> ", ptr->data); + ptr = ptr->prev; + }while(ptr != head); + printf("\b\b\b \n"); +} + +int josephus_Khushboo(node *head, int k) +{ + display(head); + node *ptr = head; + if(ptr->next == head) + return ptr->data; + + node *todel=ptr, *todel_prev = todel->prev; + for(int i=1; i< k; i++){ + todel_prev = todel; + todel = todel->next; + } + node *new_head = todel->next; + new_head->prev = todel_prev; + todel_prev->next = new_head; + + free(todel); + return josephus_Khushboo(new_head, k); +} + +int main() +{ + node *head = NULL; + int n, k; + + printf("Enter n: "); + scanf("%d", &n); + head = BML(n); + printf("Enter k: "); + scanf("%d", &k); + printf("Survival: %d\n", josephus_Khushboo(head, k)); + printf("By Khushboo Nijhawan\n"); + printf("20BML0035"); + return 0; +} \ No newline at end of file