//Josephus problem #include<stdio.h> #include
#include #include<string.h> #include<stdlib.h> typedef struct node { char name[10]; struct node *prev; }jp; jp* insert(jp *p) { jp *q; char c[10]; q=(jp*)malloc(sizeof(jp)); printf("\n Enter Name:"); scanf("%s",c); strcpy(q->name,c); if(p==NULL) { q->prev=q; p=q; return p; } q->prev=p->prev; p->prev=q; return p; } void show(jp *p) { jp *q; q=p; do { puts(q->name); printf("\n"); q=q->prev; }while(q!=p); } jp* search(jp *p,char s[10]) { int flag=0; jp *q; q=p; do { if(strcmp(q->name,s)==0) { flag=1; break; } q=q->prev; }while(q!=p); if(flag==1) return q; else printf("\n Name Not Found.\n");
return p; } void play(jp *p,int n) { int count; jp *q,*r; count=n; q=p; while(1) { for(count=n;count<9;count++) q=q->prev; r=q->prev; q->prev=r->prev; printf("\n Out: %s",r->name); getch(); if(q->prev==q) { printf("\n \n Winer is: %s",q->name); break; } q=q->prev; free(r); } } int main() { jp *p=NULL; char c[10]; int n,i; printf("\n How Many Boys?:"); scanf("%d",&n); for(i=0;i=10) { printf("\n Value is too large.\n"); exit(0); } play(p,n); return 1; }