头文件stack.h内容如下:
/*
* 对任何调用该例程用户,必须知道selemtype, lkstack类型,即这两个类型是对用户释放的
* */
/*selemtype是存储实际数据的类型,随着不同的应用具体内容应该不一样*/
typedef long selemtype;
/*stack_elem是链式栈的基本单元,成员包含了存储实际内容的单元和指向下个单元的指针*/
typedef struct snodetype_t {
selemtype x;
struct snodetype_t *next;
} snodetype;
/*链式栈的栈顶*/
typedef struct {
int count; /*用于判断栈是否为空,记录实际分配的结点数*/
snodetype *head;
} lkstack;
/*从名字我们就可以知道函数的功能*/
lkstack *stack_new();
void stack_free(lkstack **stk);
int stack_empty(lkstack *stk);
void stack_push(lkstack *stk, selemtype x);
selemtype stack_pop(lkstack *stk);
//
主体文件stack.c内容如下:
#include <stdio.h>/*fprintf()*/
#include <string.h>/*strerror()*/
#include <stdlib.h>/*exit(),malloc(),free()*/
#include <assert.h>/*assert()*/
#include <errno.h>
#include "stack.h"
lkstack *stack_new()
{
lkstack *stk;
stk = (lkstack *)malloc(sizeof(lkstack));
if(stk == NULL)
{
fprintf(stderr, "%s(%s %d): malloc failed: %s/n", __func__, __FILE__, __LINE__, strerror(errno));
exit(1);
}
stk->count = 0;
stk->head = NULL;
return stk;
}
int stack_empty(lkstack *stk)
{
assert(stk);/*
*若stk==NULL,说明是编程者的出错,因此使用assert()而不是exit()。
*assert()调用abort()生成一个内核转储,将当时程序的状态保存在这个文件中,便于 *以后使用gdb调试
*/
return stk->count == 0;
}
void stack_push(lkstack *stk, selemtype x)
{
snodetype *t;
assert(stk);
t = (snodetype *)malloc(sizeof(snodetype));
if(t == NULL)
{
fprintf(stderr, "%s(%s %d): malloc failed: %s/n", __func__, __FILE__, __LINE__, strerror(errno));
exit(1);
}
t->x = x;
t->link = stk->head;
stk->head = t;
stk->count++;
}
selemtype stack_pop(lkstack *stk)
{
selemtype x;
snodetype *t;
assert(stk);
assert(stk->count > 0);
t = stk->head;
stk->head = t->next;
stk->count--;
x = t->x;
free(t);
return x;
}
/*参数lkstack **stk是为了避免悬空指针*/
void stack_free(lkstack **stk)
{
snodetype *t, *u;
assert(stk && *stk);/*
*注意&&的优先级低于*,通过资料我们知道&&优先级在最后的30%内,而*的优 *先级在前30%内(共48个优先等级)
*/
for(t = (*stk)->head; t; t = u)
{
u = t->next;
free(t);
}
free(*stk);
*stk = NULL;/*避免指向链式栈栈头的指针出现悬空指针现象*/
///
测试程序
#include <stdio.h>
#include "stack.h"
int main()
{
selemtype ch;
lkstack *stk;
stk = stack_new();
stack_push(stk, 'o');
stack_push(stk, 'l');
stack_push(stk, 'l');
stack_push(stk, 'e');
stack_push(stk, 'h');
while(!stack_empty(stk))
{
ch = stack_pop(stk);
printf("%c", ch);
}
printf("/n");
stack_free(&stk);
return 0;
}
[bruce@localhost stack]$ ./hello
hello
[bruce@localhost stack]$
}
/*
* 对任何调用该例程用户,必须知道selemtype, lkstack类型,即这两个类型是对用户释放的
* */
/*selemtype是存储实际数据的类型,随着不同的应用具体内容应该不一样*/
typedef long selemtype;
/*stack_elem是链式栈的基本单元,成员包含了存储实际内容的单元和指向下个单元的指针*/
typedef struct snodetype_t {
selemtype x;
struct snodetype_t *next;
} snodetype;
/*链式栈的栈顶*/
typedef struct {
int count; /*用于判断栈是否为空,记录实际分配的结点数*/
snodetype *head;
} lkstack;
/*从名字我们就可以知道函数的功能*/
lkstack *stack_new();
void stack_free(lkstack **stk);
int stack_empty(lkstack *stk);
void stack_push(lkstack *stk, selemtype x);
selemtype stack_pop(lkstack *stk);
//
主体文件stack.c内容如下:
#include <stdio.h>/*fprintf()*/
#include <string.h>/*strerror()*/
#include <stdlib.h>/*exit(),malloc(),free()*/
#include <assert.h>/*assert()*/
#include <errno.h>
#include "stack.h"
lkstack *stack_new()
{
lkstack *stk;
stk = (lkstack *)malloc(sizeof(lkstack));
if(stk == NULL)
{
fprintf(stderr, "%s(%s %d): malloc failed: %s/n", __func__, __FILE__, __LINE__, strerror(errno));
exit(1);
}
stk->count = 0;
stk->head = NULL;
return stk;
}
int stack_empty(lkstack *stk)
{
assert(stk);/*
*若stk==NULL,说明是编程者的出错,因此使用assert()而不是exit()。
*assert()调用abort()生成一个内核转储,将当时程序的状态保存在这个文件中,便于 *以后使用gdb调试
*/
return stk->count == 0;
}
void stack_push(lkstack *stk, selemtype x)
{
snodetype *t;
assert(stk);
t = (snodetype *)malloc(sizeof(snodetype));
if(t == NULL)
{
fprintf(stderr, "%s(%s %d): malloc failed: %s/n", __func__, __FILE__, __LINE__, strerror(errno));
exit(1);
}
t->x = x;
t->link = stk->head;
stk->head = t;
stk->count++;
}
selemtype stack_pop(lkstack *stk)
{
selemtype x;
snodetype *t;
assert(stk);
assert(stk->count > 0);
t = stk->head;
stk->head = t->next;
stk->count--;
x = t->x;
free(t);
return x;
}
/*参数lkstack **stk是为了避免悬空指针*/
void stack_free(lkstack **stk)
{
snodetype *t, *u;
assert(stk && *stk);/*
*注意&&的优先级低于*,通过资料我们知道&&优先级在最后的30%内,而*的优 *先级在前30%内(共48个优先等级)
*/
for(t = (*stk)->head; t; t = u)
{
u = t->next;
free(t);
}
free(*stk);
*stk = NULL;/*避免指向链式栈栈头的指针出现悬空指针现象*/
///
测试程序
#include <stdio.h>
#include "stack.h"
int main()
{
selemtype ch;
lkstack *stk;
stk = stack_new();
stack_push(stk, 'o');
stack_push(stk, 'l');
stack_push(stk, 'l');
stack_push(stk, 'e');
stack_push(stk, 'h');
while(!stack_empty(stk))
{
ch = stack_pop(stk);
printf("%c", ch);
}
printf("/n");
stack_free(&stk);
return 0;
}
[bruce@localhost stack]$ ./hello
hello
[bruce@localhost stack]$
}