//Josephus problem #include<stdio.h> #include<conio.h> #include<alloc.h> #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<n;i++) p=insert(p); show(p); printf("\n Name From Where Start:"); scanf("%s",c); p=search(p,c); show(p); printf("\n Enter Number Between 1 to 9:"); scanf("%d",&n); if(n>=10) { printf("\n Value is too large.\n"); exit(0); } play(p,n); return 1; }

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.