include stdio include stdlib include memory include string typedef str

 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
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
typedef struct node
{
char* name;
int visited;
struct node* parents;
} node;
node* mem[100];// = (node**)malloc(sizeof(node*) * 10);
#define idx(n) (((size_t)(n) - (size_t)mem[0]) / sizeof(node*))
int count_skills(node* dest)
{
int ret = 1;
dest->visited = 1;
if (!dest->parents)
{
/*printf("%s no parents at %d\n", dest->name, idx(dest));*/
return ret;
}
for (node* n = dest->parents; n; n++)
{
/*printf("next parent of %s: %s %p %d\n", dest->name, n->name,idx(n));*/
printf("checking %d\n", idx(n));
if (n->visited)
{
continue;
}
printf("going into %p %d\n", n, idx(n));
ret += count_skills(n);
printf("came back from %s at %d, will go to %d next\n", n->name, idx(n), idx(n+1));
}
return ret;
}
int main(void)
{
node nodes[100];
memset(&nodes, 0, sizeof(node)*100);
node* nn = NULL;
mem[0] = &nodes[0];
mem[1] = NULL;
mem[2] = &nodes[1];
mem[3] = NULL;
mem[4] = NULL;
mem[5] = &nodes[2];
mem[1] = NULL;
mem[9] = &nn;
mem[8] = NULL;
/*mem[5] = NULL;*/
mem[6] = &nn;
mem[10] = NULL;
mem[15] = NULL;
mem[18] = NULL;
/*&nodes[3] = NULL;*/
mem[0]->name = "fireball";
mem[0]->visited = 0;
mem[0]->parents = mem[2];
mem[2]->name = "fire";
mem[2]->visited = 0;
mem[2]->parents = mem[5];
mem[4] = &nn;
mem[5]->name = "magic";
mem[5]->visited = 0;
mem[5]->parents = NULL;
//(mem +5) = (node*)NULL;
/*mem[2].parents->name = "magic";*/
printf("%d\n", count_skills(mem[0]));
}