⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.c

📁 <<c语言接口与实现>>一书源码,此书无疑是一部经典的c语言书籍,只可惜翻译的质量很差
💻 C
字号:
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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -