栈
只允许在一端插入和删除的线性表
只允许插入的一段为栈顶(所谓的栈顶指针),另一端为栈底
取栈顶:取栈顶指针指向的数据
#include <stdio.h>
struct stack
{
int num;
struct stack *next;
};
typedef struct stack * Stack;
enum Result{
STACK_EMPTY,
STACK_NOT_EMPTY //== #define STACK_NOT_EMPTY(仅相当于一个变量名)
};
void init_stack(Stack * top) //栈顶指针置空
{
*top = NULL;
}
int get_stack_top(Stack *top)
{
}
int is_stack_empty(Stack *top) //检测栈顶指针是否为空
{
if(*top == NULL)
{
return STACK_EMPTY; //枚举变量,相当于同时定义多个宏
}
return STACK_NOT_EMPTY;
}
void push_stack(Stack *newstack, Stack*top)
{
(*newstack)->next = (*top);
(*top) = *newstack;
}
int pop_stack(Stack *top)
{
if(is_stack_empty(top) == STACK_EMPTY)
{
return STACK_EMPTY;
}
int num = (*top)->num;
Stack temp = *top;
*(top) = (*top)->next;
free(temp);
temp = NULL;
return num;
}
int main()
{
int i;
Stack top;
Stack newstack;
init_stack(&top);
for(i = 0; i < 10; i++)
{
newstack =(Stack)malloc(sizeof(struct stack));
newstack->num= i + 1;
push_stack(&newstack,&top);
}
for(i = 0; i < 10; i++)
{
printf("pop:%d\n",pop_stack(&top));
}
return 0;
}