/* mysortu3.c Jim Plank 7 February, 1994 */ /* This program is just like mysortu2.c, except that it uses the v.val field as an int instead of mallocing space for an int and having the v.val field point to it. This is sloppy coding, since it works on the assumption that pointers are at least as large as integers. Usually this assumption holds, although if you want to write truly portable code, you should not make it. */ #include #include #include "fields.h" #include "rb.h" main() { IS is; char *copy; Rb_node sorted_lines, tmp, r; int found; sorted_lines = make_rb(); is = new_inputstruct(NULL); while(get_line(is) >= 0) { r = rb_find_key_n(sorted_lines, is->text1, &found); /* If the line is already in the tree, then just increment its count */ if (found) { r->v.val = (char *) ( ((int) r->v.val) + 1 ); /* Otherwise, insert the line into the tree with a count of one */ } else { copy = strdup(is->text1); rb_insert(sorted_lines, copy, (char *)1); } } rb_traverse(tmp, sorted_lines) { printf("%6d\t%s", (int) tmp->v.val, tmp->k.key); } }