stack.c
上传用户:shmaik
上传日期:2014-06-01
资源大小:45093k
文件大小:1k
- static char rcsid[] = "$Id: H:/drh/idioms/book/RCS/inter.doc,v 1.11 1997/02/21 19:42:15 drh Exp $";
- #include <stddef.h>
- #include "assert.h"
- #include "mem.h"
- #include "stack.h"
- #define T Stack_T
- struct T {
- int count;
- struct elem {
- void *x;
- struct elem *link;
- } *head;
- };
- T Stack_new(void) {
- T stk;
- NEW(stk);
- stk->count = 0;
- stk->head = NULL;
- return stk;
- }
- int Stack_empty(T stk) {
- assert(stk);
- return stk->count == 0;
- }
- void Stack_push(T stk, void *x) {
- struct elem *t;
- assert(stk);
- NEW(t);
- t->x = x;
- t->link = stk->head;
- stk->head = t;
- stk->count++;
- }
- void *Stack_pop(T stk) {
- void *x;
- struct elem *t;
- assert(stk);
- assert(stk->count > 0);
- t = stk->head;
- stk->head = t->link;
- stk->count--;
- x = t->x;
- FREE(t);
- return x;
- }
- void Stack_free(T *stk) {
- struct elem *t, *u;
- assert(stk && *stk);
- for (t = (*stk)->head; t; t = u) {
- u = t->link;
- FREE(t);
- }
- FREE(*stk);
- }