//顺序栈 数组实现
#include <stdio.h>
#define MaxSize 10
typedef struct SqStack{
int data[MaxSize];
int top;
}SqStack;
// 初始化栈
void initStack(SqStack *sqStack){
(*sqStack).top = -1;
}
//销毁栈
void destory(SqStack *sqStack){
(*sqStack).top = -1;
}
//判空
int stackEmpty(SqStack *sqStack){
int top = (*sqStack).top;
if(top == -1){
return 0;
}
return 1;
}
//进栈
int push(SqStack *sqStack,int data){
int top = (*sqStack).top;
if(top == MaxSize-1){
return 0;
}
(*sqStack).top = top + 1;
(*sqStack).data[top] = data;
return 1;
}
//出栈
int pop (SqStack *sqStack){
int top = (*sqStack).top;
if(top == -1){
return 0;
}
int *data;
(*data) = (*sqStack).data[top];
(*sqStack).top = top -1;
// printf("%d,",(*sqStack).data[top]);
return 1;
}
//得到栈顶元素
int getTop(SqStack *sqStack,int *data){
int top = (*sqStack).top;
if(top == -1){
return 0;
}
(*data) = (*sqStack).data[top];
return 1;
}
int main(){
SqStack sqStack;
initStack(&sqStack);
printf("栈顶指针初始化的值:%d",sqStack.top);
int i = 0;
printf("\n\n12345依次进栈后依次出栈:");
for(;i<5;i++){
push(&sqStack,i);
}
for(i=3;i>=0;i--){
pop(&sqStack);
printf("%d,",sqStack.data[i]);
}
return 0;
}
|