C언어 스택 구현

(C언어 스택에서 넘어옴)

1 개념[ | ]

C stack 구현
C언어 스택 구현
#include <stdio.h>
#define MAX_N 100

int top = 0;
int stack[MAX_N];

void clear() {
    top = 0;
}

int empty() {
    return top == 0;
}

void push(int value) {
    if (top == MAX_N) {
        printf("overflow!!!\n");
        return;
    }
    stack[top] = value;
    top++;
}

int pop() {
    if (empty()) {
        printf("empty!!!\n");
        return 0;
    }
    top--;
    return stack[top];
}

int main() {
    push(1);
    push(2);
    push(3);
    while(!empty()) {
        printf("%d ", pop()); // 3 2 1
    }
    printf("\n");
    clear();
    push(3);
    push(2);
    push(1);
    while(!empty()) {
        printf("%d ", pop()); // 1 2 3
    }
    printf("\n");
    pop(); // empty!!!
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}