顺序栈
2021-03-27 本文已影响0人
qianranow
#include <stdio.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack *s) {
s->top = -1;
}
int Push(SqStack *s, int e) {
if (s->top == MaxSize-1) return -1;
s->data[++s->top] = e;
return 1;
}
int Pop(SqStack *s, int *e) {
if (s->top == -1) return -1;
*e = s->data[s->top--];
return 1;
}
int GetTop(SqStack s, int *e) {
if (s.top == -1) return -1;
*e = s.data[s.top];
return 1;
}
void StackPrintf(SqStack s) {
for (int i = s.top; i > -1; i--) {
printf("%i\n", s.data[i]);
}
}
int main() {
SqStack s;
InitStack(&s);
Push(&s, 23);
Push(&s, 3);
Push(&s, 45);
Push(&s, 78);
StackPrintf(s);
int e = -1;
Pop(&s, &e);
Pop(&s, &e);
GetTop(s, &e);
printf("%i\n", e);
return 0;
}