#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 32
typedef struct __stack__
{
int data[N];
int top;
}seqstack_t;
//创建堆栈
seqstack_t * creat_empty_seqstack() //创建一个空的顺序栈 申请内存空间 并将栈顶初始化为 -1;
{
seqstack_t * s = (seqstack_t *)malloc(sizeof(seqstack_t));
s->top = -1;
memset(s->data,0,N);
return s;
}
int stack_is_empty(seqstack_t *s) //判断栈是否为空
{
return (s->top == -1)?1:0;
}
int stack_is_full(seqstack_t *s) //顺序栈需要考虑栈满的情况
{
return (s->top == N-1)?1:0;
}
int push_stack(seqstack_t *s,int val) //将数据压入栈
{
if (stack_is_full(s))
{
printf("stack_is_full\n");
return -1;
}
else
{
s->top ++;
s->data[s->top] = val;
}
return 0;
}
int pop_stack(seqstack_t *s,int *val) //弹出数据 并用地址传出值
{
if (stack_is_empty(s))
{
printf("stack_is_empty\n");
return -1;
}
else
{
*val =s->data[s->top];
s->top --;
}
}
int main()
{
seqstack_t * S = creat_empty_seqstack();
int i,tmp;
for(i = 1;i<=32;i++)
{
printf("%-3d",i);
push_stack(S,i);
}
printf("\n");
for(i = 1;i<=32;i++)
{
pop_stack(S,&tmp);
printf("%-3d",tmp);
}
printf("\n");
return 0;
}
数据结构之顺序栈
最新推荐文章于 2023-10-19 21:37:31 发布