ababup1192
6/20/2017 - 10:40 PM

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0013&lang=jp

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

#define N 200

typedef struct stack{
    int top;
    int data[N];
} Stack;

void initialize(Stack*);
int top(Stack*);
void push(Stack*, int);
void pop(Stack*);
int is_empty(Stack*);

int main(){
    int in;
    Stack *stack = malloc(sizeof(struct stack));

    initialize(stack);

    while(scanf("%d", &in) != EOF){
        if(in == 0){
            printf("%d\n", top(stack));
            pop(stack);
        }else{
            push(stack, in);
        }
    }

    free(stack);

    return 0;
}

void initialize(Stack *stack){
    stack->top = 0;
}

int top(Stack *stack){
    return stack->data[stack->top - 1];
}

void push(Stack *stack, int e){
    stack->data[stack->top++] = e;
}

void pop(Stack *stack){
    if(is_empty(stack)){
        printf("Error: stack is empty.");
    }else{
        --stack->top;
    }
}

int is_empty(Stack* stack){
    return stack->top == 0;
}