Circular chain list Solution Joseph Ring problem
#include <iostream>using namespacestd;structnode{intdata; Node*next;}; Node*joseph (Node *head,intMintk);//Create a circular linked list (unidirectional)Node *cycle_list (intNintMintk) {Node*head=Newnode; Node*p=Head; //node *temp;Node *temp; for(intI=0; i<n;i++) {Temp=Newnode; Temp->data=i+1; P->next=temp; P=temp; } head=head->Next; P->next=Head; while(head->next!=head) {Head=Joseph (Head,m,k); } returnhead;}//Joseph QuestionNode *joseph (node *head,intMintk) {Node*p=Head; Node*p2=Head; intx=m+k-1; for(intI=0; i<x-1; i++) {p=p->Next; if(i<x-2) P2=p2->Next; } P2->next=p->Next; Deletep; Head=p2->Next; returnhead;}//Print cycle linked listvoidPrint_list (Node *head) {Node*p=Head; while(p->next!=p) {cout<<p->data<<Endl; P=p->Next; } if(p->next==p) cout<<"Empty"<<Endl;}intMain () {cout<<"input N,m,k"<<Endl; intn,m,k; CIN>>n>>m>>K; Node*p=cycle_list (n,m,k); Print_list (P); return 0;}
Circular chain list Solution Joseph Ring problem