-
Notifications
You must be signed in to change notification settings - Fork 0
/
스택 구현하기(2).c
79 lines (75 loc) · 1.34 KB
/
스택 구현하기(2).c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define STACK_SIZE 10
typedef int element;
typedef struct
{
element num[STACK_SIZE];
element top;
}Stack;
Stack s;
bool isFull();
bool isEmpty();
void Push(element data );
int Pop();
void initStack(Stack* s );
int main(){
int menu;
element data ;
initStack( &s );
do{
printf("1:push, 2:pop, 0:exit : ");
scanf("%d", &menu);
switch( menu ){
case 1 :
printf("데이터 입력 : ");
scanf("%d",&data);
Push(data);
break;
case 2 :
data = Pop();
if( data )
printf("pop 데이터 : %d\n", data);
break;
case 0 :
break;
default :
printf("잘못된 입력 입니다. \n");
break;
}
}while( menu );
}
void createStack( Stack *s );
void initStack( Stack *s )
{ // top 포인터를 -1로 초기화
s->top = -1;
}
bool isEmpty(){
return s.top==-1;
}
bool isFull(){
return s.top == STACK_SIZE-1;
}
void Push(element data)
{ //스택이 가득 차 있으면 문구 출력 후 리턴, 아니면 push
if(isFull())
{
printf("stack is full\n");
return;
}
else
{
s.num[++s.top] = data;
printf("%d\n",s.top);
}
}
element Pop()
{ //스택이 비어 있으면 문구 출력 후 0 리턴, 아니면 맨 위 데이터 리턴
if(isEmpty())
{
printf("stack is empty\n");
return 0;
}
else return s.num[s.top--];
}