设为首页 加入收藏

TOP

C语言接口与实现实例(二)
2015-01-22 21:26:21 来源: 作者: 【 】 浏览:77
Tags:语言 接口 实现 实例
t, *u;
? ? assert(stk && *stk);
? ? for (t = (*stk)->head; t; t = u) {
? ? ? ? u = t->link;
? ? ? ? FREE(t);
? ? }
? ? FREE(*stk);
}
完整实现代码如下:
?
#include
#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);
}
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言柔性数组 下一篇编程算法 - 字符串的排列 代码(C)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: