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; +}