栈的基本操作

#include <stdio.h>
#include <stdlib.h>

#define ERROR 0
#define OK 1

typedef struct Stack {
    int *elements;
    int max_size, top_index;
} Stack;

void init(Stack *s, int length) {
    s->elements = (int *)malloc(sizeof(int) * length);
    s->max_size = length;
    s->top_index = -1;
}

int push(Stack *s, int element) {
    if (s->top_index >= s->max_size - 1) {
        return ERROR;
    }
    s->top_index++;
    s->elements[s->top_index] = element;
    return OK;
}

int pop(Stack *s) {
    if (s->top_index < 0) {
        return ERROR;
    }
    s->top_index--;
    return OK;
}

int top(Stack *s) {
    return s->elements[s->top_index];
}

int empty(Stack *s) {
    if (s->top_index < 0) {
        return 1;
    }
    else {
        return 0;
    }
}

void clear(Stack *s) {
    free(s->elements);
    free(s);
}

int main() {
    int n, num;
    scanf("%d", &n);
    Stack *stack = (Stack *)malloc(sizeof(Stack));
    init(stack, n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &num);
        push(stack,num);
    }
    while (!empty(stack)) {
        printf("%d ",top(stack));
        pop(stack);
    }
    clear(stack);
    return 0;
}
上一篇:SLT2021: VAW-GAN FOR DISENTANGLEMENT AND RECOMPOSITION OF EMOTIONAL ELEMENTS IN SPEECH


下一篇:英文写作——The Elements of Style知识点解读3—冒号和破折号的用法