这是我的代码
#include <stdio.h>
#include <malloc.h>
#define sizemax 10
#define error 0
#define ok 1
typedef struct {
int *base;
int *top;
} sqstack;
int init(sqstack *s) {
s = (sqstack *)malloc(sizeof(sqstack));
s->base=NULL;
s->top=NULL;
return ok;
}
//入栈
int push(sqstack *s, int m) {
if (s->top - s->base == sizemax)
return error;
*(s->top) = m;
s->top++;
return ok;
}
//出栈
int pop(sqstack *s) {
int n;
if (s->top==NULL) {
return error;
}
n = *(s->top);
s->top--;
return n;
}
int main() {
sqstack s;
init(&s);
int a, e, i,j;
for (i = 0; i < 5; i++) {
scanf("%d", &e);
push(&s, e);
}
printf("\n");
printf("pop a number \n");
scanf("%d", &a);
for (j = 0; j < a; j++) {
printf("%d ",pop(&s));
}
return 0;
}
#include <stdio.h>
#include <malloc.h>
#define sizemax 10
#define error 0
#define ok 1
typedef struct {
int *base;
int *top;
} sqstack;
int init(sqstack *s) {
s = (sqstack *)malloc(sizeof(sqstack));
s->base=NULL;
s->top=NULL;
return ok;
}
//入栈
int push(sqstack *s, int m) {
if (s->top - s->base == sizemax)
return error;
*(s->top) = m;
s->top++;
return ok;
}
//出栈
int pop(sqstack *s) {
int n;
if (s->top==NULL) {
return error;
}
n = *(s->top);
s->top--;
return n;
}
int main() {
sqstack s;
init(&s);
int a, e, i,j;
for (i = 0; i < 5; i++) {
scanf("%d", &e);
push(&s, e);
}
printf("\n");
printf("pop a number \n");
scanf("%d", &a);
for (j = 0; j < a; j++) {
printf("%d ",pop(&s));
}
return 0;
}