Skip to content

Instantly share code, notes, and snippets.

@pqnelson
Created January 1, 2016 19:20
Show Gist options
  • Select an option

  • Save pqnelson/bcb1b39d97fa2d6063e6 to your computer and use it in GitHub Desktop.

Select an option

Save pqnelson/bcb1b39d97fa2d6063e6 to your computer and use it in GitHub Desktop.
Terrible C hack for Linked Lists
// a doubly linked list of "stuff"
struct item {
int kind;
struct item *next, *prev;
};
// an item can be a man eating bear
#define MAN_EATING_BEAR_KIND 1
struct manEatingBear {
int kind;
struct item *next, *prev;
unsigned int number_hew_mans_eaten;
char *name;
};
// ...or an apple pie, because they're very similar
#define APPLE_PIE_KIND 2
struct applePie {
int kind;
struct item *next, *prev;
unsigned char numberApples;
};
unsigned int numberOfApples(struct item *list) {
struct item *elt;
unsigned int seen = 0;
for(elt = list; elt; elt=elt->next) {
if (APPLE_PIE_KIND == elt->kind) {
// huge hack!!
seen+=((struct *applePie)elt)->numberApples;
}
}
return seen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment