顺序栈第一版
功能上实现,连续进栈,出栈,读取栈顶,函数中功能划分为创建栈,初始化栈,进栈,出栈,栈满、栈空检查,查看栈顶信息。
代码详细
#include <stdio.h>
#include <stdlib.h>
#define Max 10
#define bottom -1
struct Seq_stack
{
int top ;
int Ele[Max];
};
enum State_Date
{
Full_Ok,Full_No,
Empty_Ok,Empty_No,
Push_Ok,Push_No,
Pop_Ok,Pop_No,
Gettop_Ok,Gettop_No
};
typedef struct Seq_stack Stack;
void Creat_stack(Stack ** stack )
{
do
{
*stack = (Stack *)malloc(sizeof(Stack));
}while(*stack == NULL);
}
void Init_stack(Stack * stack)
{
stack -> top = -1;
}
int Push_stack(Stack * stack , int num)
{
if(Full_Ok == is_full(stack))
{
printf("Stack full\n");
return Push_No;
}
stack -> top++;
stack -> Ele[stack -> top] = num ;
return Push_Ok;
}
int Pop_stack(Stack * stack , int * num)
{
if(Empty_Ok == is_empty(stack))
{
printf("stack empty\n");
return Pop_No;
}
(*num) = stack -> Ele[stack -> top];
stack -> top--;
return Pop_Ok;
}
int is_full(Stack * stack)
{
if((Max) == (stack -> top))
{
return Full_Ok;
}
return Full_No;
}
int is_empty(Stack * stack)
{
if((-1) == (stack -> top))
{
return Empty_Ok;
}
return Empty_No;
}
int Gettop_stack(Stack * stack , int *num)
{
if(Empty_Ok == (is_empty(stack)))
{
return Gettop_No;
}
(*num) = stack -> Ele[stack -> top];
return Gettop_Ok;
}
int main()
{
Stack *stack;
int i;
Creat_stack(&stack);
Init_stack(stack);
for(i = 0 ; i < Max ; i++)
{
if(Push_Ok == Push_stack(stack ,i + 1))
{
printf("Output push stack : %d\n" , stack -> Ele[i]);
printf("Output No. %d\n" ,i + 1);
}
else
{
printf("Push ERR_Full\n");
}
}
int pop = 0 ;
for(i = 0 ; i < Max ; i++)
{
if(Pop_Ok == Pop_stack(stack ,&pop))
printf("Output pop stack : %d\n" ,pop);
else
{
printf("Pop Err_Empty\n");
}
}
int get = 0;
if(Gettop_Ok == Gettop_stack(stack , &get))
{
printf("Output get is : %d\n ", get);
}
else
{
printf("Empty stack\n");
}
return 0;
}