#include <stdio.h>
#include <stdlib.h>
typedef struct stack{
int top;
int capacity;
int* array;
} Stack;
Stack* createStack(int capacity) {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int *)malloc(stack->capacity*sizeof(int));
return stack;
}
int isFull(Stack* stack) {
return stack->top == stack->capacity-1;
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
void push(Stack* stack, int item) {
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
int pop(Stack* stack) {
int temp; // 마지막에 저장된 값을 저장하는 임시 변수
if (isEmpty(stack))
return -9999;
temp = stack->array[stack->top];
--stack->top; // top을 감소시킴
return temp;
}
int peek(Stack* stack) {
if (isEmpty(stack))
return;
return stack->array[stack->top]; // 마지막에 저장된 값을 반환함
}
int main() {
Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
push(stack, 30);
push(stack, 40);
printf("%d pop from stack\n", pop(stack));
printf("%d pop from stack\n", pop(stack));
push(stack, 50);
printf("%d pop from stack\n", pop(stack));
printf("%d pop from stack\n", pop(stack));
printf("%d pop from stack\n", pop(stack));
printf("%d pop from stack\n", pop(stack));
return 0;
}