A quick and dirty untested example. Uses a single-linked list structure; items are clicked and pushed out of the list header.
#include <stdlib.h>
Edit
This would help to have an example of how to use it:
int main(void) { struct stack_t *theStack = newStack(); char *data; push(theStack, "foo"); push(theStack, "bar"); ... data = top(theStack); pop(theStack); ... clear(theStack); destroyStack(&theStack); ... }
You can declare stacks as automatic variables, rather than using newStack () and destroyStack (), you just need to make sure that they are loaded correctly, as in
int main(void) { struct stack_t myStack = {NULL, 0}; push (&myStack, "this is a test"); push (&myStack, "this is another test"); ... clear(&myStack); }
I'm just used to creating pseudo-constructors / destructors for everything.
source share