#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 3
struct stack{
char data[STACKSIZE];
int top;
};
int init(struct stack * s){
s->top=0;
return 0;
}
int push(struct stack * s,char elem){
if(s->top==STACKSIZE){
printf("栈满!入栈失败!\n");
return -1;
}
s->top++;
s->data[s->top]=elem;
printf("%c入栈\n",elem);
return 0;
}
int pop(struct stack * s){
if(s->top==0){
printf("栈空!删除失败!\n");
return -1;
}
printf("%c出栈\n",s->data[s->top]);
s->top--;
return 0;
}
int getTop(struct stack * s){
if(s->top==0){
printf("栈空!无栈顶元素!\n");
return -1;
}
printf("当前栈顶元素为:%c\n",s->data[s->top]);
return 0;
}
int getLength(struct stack * s){
printf("当前栈的长度为:%d\n",s->top);
return 0;
}
void display(struct stack * s){
if(s->top==0){
printf("栈空!无元素!\n");
}
else{
int step;
step=s->top;
printf("当前栈中元素为:");
while(step>0){
printf("%c ",s->data[step]);
step--;
}
printf("\n");
}
}
int main(){
struct stack * s;
s = (struct stack *)malloc(sizeof(struct stack));
init(s);
push(s,'a');
push(s,'b');
push(s,'c');
push(s,'d');
display(s);
getLength(s);
getTop(s);
pop(s);
display(s);
pop(s);
pop(s);
getLength(s);
getTop(s);
display(s);
pop(s);
return 0;
}
数组栈
最新推荐文章于 2021-11-09 21:46:53 发布