From 0b08c9752d5001e28b62f7c9e596837b1f55ed2f Mon Sep 17 00:00:00 2001 From: Amar Shinde Date: Sat, 2 Oct 2021 22:27:55 +0530 Subject: [PATCH] Create Josephus_Problem.cpp --- Josephus_Problem.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Josephus_Problem.cpp diff --git a/Josephus_Problem.cpp b/Josephus_Problem.cpp new file mode 100644 index 0000000..0046ba8 --- /dev/null +++ b/Josephus_Problem.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +class Node{ +public: + int data; + Node *next; +}; +int main() +{ + Node *head=NULL,*lastNode=NULL; + for(int i=1;i<=1000;i++){ + Node *newNode = (Node *)malloc(sizeof(Node)); + newNode->data=i; + newNode->next=NULL; + if(head==NULL){ + head=newNode; + lastNode = head; + head->next=head; + } + else{ + newNode->next=head; + lastNode->next = newNode; + lastNode=newNode; + } + } + Node *tmp=head; + while(tmp->next!=tmp){ + tmp->next=tmp->next->next; + tmp = tmp->next; + } + cout<data<<" Answer"; + return 0; +}