| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include "avl.h" |
| 29 |
|
| 30 |
int depth(avl_node_t *n) { |
| 31 |
int d; |
| 32 |
for(d = 0; n; n = n->parent) |
| 33 |
d++; |
| 34 |
return d; |
| 35 |
} |
| 36 |
|
| 37 |
void readinto(avl_tree_t *t, char *fname) { |
| 38 |
FILE *f; |
| 39 |
char *s, *e; |
| 40 |
int len; |
| 41 |
|
| 42 |
f = fopen(fname, "r"); |
| 43 |
if(!f) { |
| 44 |
perror(fname); |
| 45 |
exit(1); |
| 46 |
} |
| 47 |
|
| 48 |
while((e = s = malloc(65536)) && fgets(s, 65536, f)) { |
| 49 |
len = strlen(s); |
| 50 |
if(s[len-1] == '\n') |
| 51 |
s[len-1] = '\0'; |
| 52 |
else |
| 53 |
len++; |
| 54 |
e = s = realloc(s, len); |
| 55 |
if(!s) |
| 56 |
break; |
| 57 |
avl_insert(t, s); |
| 58 |
} |
| 59 |
if(!e) { |
| 60 |
perror("malloc()"); |
| 61 |
exit(2); |
| 62 |
} |
| 63 |
fclose(f); |
| 64 |
} |
| 65 |
|
| 66 |
int main(int argc, char **argv) { |
| 67 |
avl_tree_t t, u; |
| 68 |
avl_node_t *c; |
| 69 |
|
| 70 |
if(argc != 3) { |
| 71 |
fprintf(stderr, "Requires exactly 2 arguments.\n"); |
| 72 |
exit(2); |
| 73 |
} |
| 74 |
|
| 75 |
avl_init_tree(&t, (avl_compare_t)strcmp, NULL); |
| 76 |
avl_init_tree(&u, (avl_compare_t)strcmp, NULL); |
| 77 |
|
| 78 |
readinto(&t, argv[1]); |
| 79 |
readinto(&u, argv[2]); |
| 80 |
|
| 81 |
for(c = t.head; c; c = c->next) |
| 82 |
if(!avl_search(&u, c->item)) |
| 83 |
printf("-%s\n", c->item); |
| 84 |
|
| 85 |
for(c = u.head; c; c = c->next) |
| 86 |
if(!avl_search(&t, c->item)) |
| 87 |
printf("+%s\n", c->item); |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |