#include "iostream.h"
# include "conio.h"
# include "stdio.h"
#include <stdlib.h>
#define N 5
#define M 0
struct cel { int info;
struct cel *prox;
};
typedef struct cel Celula;
void inserte (int, Celula **);
void mostrar_vencedor (Celula *);
void josephus (Celula **);
void encontrar (Celula **p);
void elimina (Celula **p);
int main () { int cont;
Celula *persona = NULL;
printf("%d personas.\n", N); printf("%d passes.\n", M); for (cont = 1; cont <= N; ++cont)
inserte (cont, &persona);
josephus (&persona);
mostrar_vencedor (persona);
return EXIT_SUCCESS;
}
void inserte (int num, Celula **p) { Celula *nodo;
nodo = (Celula *) malloc (sizeof (Celula));
nodo->info = num;
if (*p == NULL) { *p = nodo;
(**p).prox = *p;
}
else { Celula *temp = *p;
while ((**p).prox != temp) { *p = (**p).prox;
}
(**p).prox = nodo;
nodo->prox = temp;
*p = temp;
}
}
void josephus (Celula **p) { if (*p != NULL) { while (*p != (**p).prox) { encontrar (p);
elimina (p);
}
}
}
void encontrar (Celula **p) { int cont = M;
while (cont) { *p = (**p).prox;
cont--;
}
}
void elimina (Celula **p) { Celula *morta = *p;
while ((**p).prox != morta) { *p = (**p).prox;
}
(**p).prox = (**p).prox->prox;
*p = (**p).prox;
free(morta);
}
void mostrar_vencedor (Celula *p) { if (p != NULL)
printf ("vencedor : %d\n", p->info); getch();
}