Created
January 1, 2016 19:20
-
-
Save pqnelson/bcb1b39d97fa2d6063e6 to your computer and use it in GitHub Desktop.
Terrible C hack for Linked Lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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