#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
#define ERROR 0
#define OK 1
typedef struct Sqtack {
int stacksize;
int *top;
int *base;
}Sqtack;
int Xj_stack(Sqtack &S) {//申请内存
if (S.stacksize == MAXSIZE) {
printf("请勿重复创建!\a\n");
return ERROR;
}
S.base = (int*)malloc(MAXSIZE*sizeof(int));
if (!S.base) {
printf("内存申请失败!\a\n");
return ERROR;
}
S.top = S.base;
S.stacksize = MAXSIZE;
printf("内存申请成功!\n");
return OK;
}
int Rz_stack(Sqtack& S) {//入栈
if (S.stacksize != MAXSIZE|| S.stacksize == NULL) {
printf("栈未被创建!\a\n");
return ERROR;
}
if (S.top - S.base == MAXSIZE) {
printf("栈已满!\a\n");
return ERROR;
}
int e;
printf("输入想入栈的数据:");
scanf("%d",&e);
printf("\n");
*S.top++ = e;
printf("入栈成功!\n");
return OK;
}
int Cz_stack(Sqtack& S)
C语言:栈的基本操作
最新推荐文章于 2023-08-22 15:39:33 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)