顺序栈


#include 

#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;
}

你可能感兴趣的:(顺序栈)