#include #include #include #include struct thing { char *stuff; struct thing *another_thing; }; /* * What does this function do? */ void action1(struct thing **yp, const char *stuff) { struct thing *x; x = malloc(sizeof(struct thing)); if (x == NULL) { fprintf(stderr, "Memory allocation near line %d failed!\n", __LINE__); exit(1); } /* * Alternatively, the next line of code can be written: * x->stuff = strdup(stuff); */ x->stuff = malloc(strlen(stuff) + 1); if (x->stuff == NULL) { fprintf(stderr, "Memory allocation near line %d failed!\n", __LINE__); exit(1); } strcpy(x->stuff, stuff); x->another_thing = *yp; *yp = x; } /* * What does this function do? */ void action2(struct thing **yp) { struct thing *x; while ((x = *yp) != NULL) { printf("%s ", x->stuff); yp = &x->another_thing; } putchar('\n'); } /* * What does this function do? */ bool action3(struct thing **yp, const char *stuff) { struct thing *x; while ((x = *yp) != NULL) { if (strcmp(x->stuff, stuff) == 0) return (true); else yp = &x->another_thing; } return (false); } /* * What does this function do? */ void action4(struct thing **yp, const char *stuff) { struct thing *x; while ((x = *yp) != NULL) { if (strcmp(x->stuff, stuff) == 0) { *yp = x->another_thing; free(x->stuff); free(x); return; } else yp = &x->another_thing; } } /* * What does this program output? */ int main(void) { struct thing *y = NULL; action2(&y); action1(&y, "Cox"); action1(&y, "Alan"); action1(&y, "Hello"); action2(&y); printf("Hello %d Huh %d\n", action3(&y, "Hello"), action3(&y, "Huh")); action4(&y, "Michael"); action2(&y); action4(&y, "Alan"); action2(&y); action4(&y, "Hello"); action2(&y); action4(&y, "Cox"); action2(&y); action1(&y, "Dave"); action1(&y, "Johnson"); action1(&y, "Hello"); action2(&y); return (0); }