#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int top;
} Stack;
void InitStack(Stack *s) {
s->top = -1;
}
int IsEmpty(Stack *s) {
return s->top == -1;
}
int IsFull(Stack *s) {
return s->top == MAXSIZE - 1;
}
void Push(Stack *s, int element) {
if (IsFull(s)) {
printf("Stack is full!\n");
return;
}
s->data[++s->top] = element;
}
int Pop(Stack *s) {
if (IsEmpty(s)) {
printf("Stack is empty!\n");
return -1;
}
return s->data[s->top--];
}
int main() {
Stack s;
InitStack(&s);
Push(&s, 10);
Push(&s, 20);
printf("Popped element: %d\n", Pop(&s));
printf("Popped element: %d\n", Pop(&s));
return 0;
}
top是指标 最好设置为-1不浪费空间