1、栈的实现以及基本功能
#include <stdio.h>
#include <mm_malloc.h>
#define MAXSIZE 50
#define OK 1
#define ERROR -1
typedef struct SqeStack{
char elem[MAXSIZE];
int top;
}SqeStack,*Sqe;
int main(){
Sqe InitStack();
int push(Sqe s,char elem);
int pop(Sqe s,char *x);
int GetTop(Sqe s,char *x);
int x;
Sqe s = InitStack();
push(s,'1');
push(s,'2');
push(s,'a');
push(s,'b');
GetTop(s,&x);
pop(s,&x);
GetTop(s,&x);
printf("元素值:%c",x);
}
Sqe InitStack(){
SqeStack *s = (SqeStack *)malloc(sizeof(SqeStack));
s->top = -1;
return s;
}
int push(Sqe sqe,char elem){
if (sqe->top == MAXSIZE-1){
printf("栈已满");
exit(0);
}
sqe->top++;
sqe->elem[sqe->top] = elem;
return OK;
}
int pop(Sqe s,char *x){
if (s->top == -1){
printf("栈为空");
exit(0);
}
*x = s->elem[s->top];
s->top--;
return OK;
}
int GetTop(Sqe s,char *x){
if (s->top == -1){
printf("栈为空");
exit(0);
}
*x = s->elem[s->top];
return OK;
}
int isEmpty(struct SqeStack *s){
return s->top == -1 ? 1 : 0;
}