-
[자료구조] 기본 Stack 소스Programming/자료구조&알고리즘 2014. 3. 18. 03:14반응형
스택 소스 입니다.
#include <stdio.h> #include <stdlib.h> // 스택 크기 #define STACK_MAX (5) // 스택 선언 int arr[STACK_MAX]; int top = -1; void push(int data) { arr[++top] = data; } int pop() { return arr[top--]; } int is_full() { return top == STACK_MAX-1; } int is_empty() { return top == -1; } // 스택 출력 void display() { int i; system("cls"); // 콘솔창 초기화 printf("\n"); for(i = 0; i < STACK_MAX; i++) { if(STACK_MAX-1-i > top) printf("[ ]"); else printf("[ %2d ]", arr[STACK_MAX-1-i]); if(STACK_MAX-1-i == top) printf(" <- TOP"); printf("\n"); } if(is_full()) printf("STACK IS FULL\n"); if(is_empty()) printf("STACK IS EMPTY\n"); getchar(); } int main() { int i; display(); for( i=0; i<10; i++ ) { if(!is_full()) push(i+1); display(); } for( i=0; i<10; i++ ) { if(!is_empty()) printf("%d ", pop()); display(); } return 0; }
반응형'Programming > 자료구조&알고리즘' 카테고리의 다른 글
[자료구조] 기본 Queue 소스 (0) 2014.03.21