1.源码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define MAX_VERT_NUM 20
typedef struct {
int data[MAX_VERT_NUM];
int top;
} stack_node, *stack;
stack stack_new()
{
stack s = (stack)malloc(sizeof(stack_node));
s->top = -1;
return s;
}
void stack_free(stack s)
{
free(s);
}
void stack_clear(stack s)
{
s->top = -1;
}
short stack_is_empty(stack s)
{
if(s->top < 0)
{
return 1;
}
else
{
return 0;
}
}
short stack_push(stack s, int x)
{
if(s->top >= MAX_VERT_NUM - 1)
{
return 0;
}
else
{
s->top++;
s->data[s->top] = x;
return 1;
}
}
short stack_pop(stack s)
{
if(s->top < 0)
{
return 0;
}
s->top--;
return 1;
}
int stack_top(stack s)
{
return s->data[s->top];
}
int main()
{
stack s = stack_new();
int i;
for(i=0; i<10; i++)
{
stack_push(s, i);
}
for(i=0; i<10; i++)
{
printf("%d ", stack_top(s));
stack_pop(s);
}
printf("\n");
stack_free(s);
return 0;
}
2.编译源码
$ gcc -o test test.c -std=c89
3.运行及其结果
./test
9 8 7 6 5 4 3 2 1 0
网友评论