#include <stdio.h>
#include <stdlib.h>
typedef struct{
int* arr;
int top;
int capacity;
}Stack;
void initStack(Stack* s, int size){
s->arr = (int*)malloc(sizeof(int)*size);
s->top = -1;
s->capacity = size;
}
void push(Stack* s, int value){
if(s->top == s->capacity -1){
printf("스택 가득 참\n");
return;
}
s->arr[++(s->top)] = value;
}
int pop(Stack* s){
if(s->top == -1){
printf("스택 비어 있음");
return -1;
}
return s->arr[(s->top)--];
}
void printStack(Stack* s){
for(int i=0; i<=s->top; i++){
printf("%d ", s->arr[i]);
}
printf("\n");
}
void freeStack(Stack* s){
free(s->arr);
}
int main() {
Stack s;
initStack(&s, 5);
push(&s, 1);
push(&s, 2);
push(&s, 3);
push(&s, 4);
push(&s, 5);
printStack(&s);
freeStack(&s);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: