개발하는중/자료구조
자료구조_c언어_배열스택_arrayStack_03
창자쏟고싶냐
2021. 2. 3. 10:26
728x90
반응형
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 | #include <stdio.h> #include <stdlib.h> #define MAX_STACK_SIZE 100 typedef int element; typedef struct { element stack[MAX_STACK_SIZE]; int top; }StackType; void push(StackType *s, element item); element pop(StackType *s); void init(StackType *s) { s->top = -1; } int is_empty(StackType *s) { return(s->top == -1); } int is_full(StackType *s) { return(s->top == (MAX_STACK_SIZE - 1)); } element peek(StackType *s) { if (is_empty(s)) { printf("스택 공백 에러\n"); exit(1); }else return s->stack[s->top]; } void push(StackType *s, element item) { if (is_full(s)) { printf("스택 포화 에러\n"); exit(1); } else return s->stack[++(s->top)] = item; } element pop(StackType *s) { if (is_empty(s)) { printf("스택 공백 에러\n"); exit(1); } else return s->stack[(s->top)--]; } int main() { StackType s; init(&s); push(&s, 1); printf("%d\n", s.top); push(&s, 2); printf("%d\n", s.top); push(&s, 3); printf("%d\n", s.top); // 데이터 입력할때마다 top값 증가 printf("%d\n", pop(&s)); printf("%d\n", pop(&s)); printf("%d\n", pop(&s)); printf("%d\n", is_empty(&s)); // 데이터 먼저 꺼내고 후위연산으로 top값 감소 마지막 is_empty에서 결과값 1은 모든 데이터 꺼내고 top값이 -1이되어 is_empty함수 와 동일하여 true=1을 반환함! return 0; } | cs |
728x90