
ayuda con el programa josephus
Publicado por andrea (10 intervenciones) el 01/05/2013 19:13:33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#define N 5
#define M 0
struct cel {
int info;
struct cel *prox;
};
typedef struct cel Celula;
void insere (int, Celula **);
void mostra_vencedor (Celula *);
void josephus (Celula **);
void encontra (Celula **p);
void elimina (Celula **p);
int main () {
int cont;
Celula *pessoa = NULL;
printf("%d pessoas.\n", N);
printf("%d passes.\n", M);
for (cont = 1; cont <= N; ++cont)
insere (cont, &pessoa);
josephus (&pessoa);
mostra_vencedor (pessoa);
return EXIT_SUCCESS;
}
void insere (int num, Celula **p) {
Celula *nova;
nova = (Celula *) malloc (sizeof (Celula));
nova->info = num;
if (*p == NULL) {
*p = nova;
(**p).prox = *p;
}
else {
Celula *temp = *p;
while ((**p).prox != temp) {
*p = (**p).prox;
}
(**p).prox = nova;
nova->prox = temp;
*p = temp;
}
}
void josephus (Celula **p) {
if (*p != NULL) {
while (*p != (**p).prox) {
encontra (p);
elimina (p);
}
}
}
void encontra (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 mostra_vencedor (Celula *p) {
if (p != NULL)
printf ("vencedor : %d\n", p->info);
}
ayudaaa!!! no he podido hacer el programa de josephus :(
Valora esta pregunta


0