83 lines
1.7 KiB
C
83 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_SIZE 100
|
|
|
|
typedef struct Stack Stack;
|
|
|
|
struct Stack {
|
|
int data[MAX_SIZE];
|
|
int top;
|
|
|
|
void (*push)(Stack *s, int value);
|
|
int (*pop)(Stack *s);
|
|
bool (*isEmpty)(Stack *s);
|
|
};
|
|
|
|
void _push(Stack *s, int value){
|
|
if(s->top == MAX_SIZE-1){
|
|
printf("Stack is full!");
|
|
return;
|
|
}
|
|
s->data[++(s->top)] = value;
|
|
printf("stack : ");
|
|
for(int i=0; i<=s->top; i++){
|
|
printf("%d ",s->data[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int _pop(Stack *s){
|
|
if(s->top == -1){
|
|
printf("Stack is empty ");
|
|
return -1;
|
|
}
|
|
return s->data[(s->top)--];
|
|
}
|
|
|
|
bool _isEmpty(Stack *s){
|
|
return s->top == -1;
|
|
}
|
|
|
|
Stack creatStack(){
|
|
Stack s;
|
|
s.top = -1;
|
|
s.push = _push;
|
|
s.pop = _pop;
|
|
s.isEmpty = _isEmpty;
|
|
return s;
|
|
}
|
|
|
|
int main(){
|
|
Stack stack = creatStack();
|
|
while(true){
|
|
char input[10];
|
|
|
|
printf("\n명령을 입력하세요 (push, pop, isEmpty, exit): ");
|
|
scanf("%s", input);
|
|
|
|
if(!strcmp(input, "push")){
|
|
int value;
|
|
printf("스택에 넣을 값을 입력해주세요 : ");
|
|
scanf("%d", &value);
|
|
stack.push(&stack, value);
|
|
}
|
|
else if(!strcmp(input, "pop")){
|
|
int poppedValue = stack.pop(&stack);
|
|
|
|
if(poppedValue != -1){
|
|
printf("Pop value : %d\n", poppedValue);
|
|
}
|
|
}
|
|
else if(!strcmp(input, "isEmpty")){
|
|
printf("%s\n", stack.isEmpty(&stack) ? "스택이 비었습니다" : "스택이 비어있지 않습니다.");
|
|
}
|
|
else {
|
|
printf("종료합니다\n");
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
} |