之前发过一篇在函数中初始化不成功的文章,是关于指针的
现在补上关于栈成功在主函数中初始化的代码
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef int ElemType;
typedef struct{
ElemType* base;
ElemType top;//下标
}stack;
void Init(stack* s){//初始化函数
s->base=(ElemType*)malloc(maxsize*sizeof(ElemType));//申请空间
if(s==NULL){
printf("malloc fail in Init");
return NULL;
}
s->top=0;
}
void push(stack* s,ElemType e){//压栈
if(s->top==maxsize){//溢出
printf("stack is overflow");
return;
}
*(s->base+s->top)=e;//压入
s->top++;//栈顶上移
}
int pop(stack* s){//出栈
if(s->top==0){//空栈
printf("stack is underflow");
return 0;
}
s->top--;//栈顶下移
return *(s->base+s->top);//返回移出的值
}
void print(stack* s){//从栈顶到栈底打印栈
printf("stack:\n");
for(int i=s->top-1;i>=0;i--){
printf("%d\n",*(s->base+i));
}
}
int main(){
stack s;
Init(&s);
push(&s,1);
push(&s,2);
push(&s,3);
print(&s);
printf("pop:%d\n",pop(&s));
print(&s);
return 0;
}