반응형
SMALL
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 80 81 82 83 84 85 86 87 | #include <stdio.h> #define SZ 5 int stack[5]; int top = -1; // check the stack is empty // it returns 1 if so, // returns 0 otherwise int isEmpty() { return (top == -1); } // check the stack is full // it returns 1 if so, // returns 0 otherwise int isFull() { // (SZ-1) is the last index of the stack return (top == (SZ - 1)); } // put the data d into the stack // if the stack is full, do nothing, just return void push(int d) { if (isFull() == 1) { return; } top++; // because the initial value of top is -1 stack[top] = d; // stack[++top] = d; the above two lines can be // written in one line. return; } // take out one item from the stack // returns -999 if the stack is empty. int pop() { if (isEmpty() == 1) { return -999; // -999 means the stack is empty. } top--; return stack[top + 1]; /* the above two lines mean the following. { int temp = stack[top]; top--; return temp; } */ } int main(void) { #if 0 // test case forf the full stack for (int i = 0; i <10; i++) { push(i); } printf("%d\n", pop()); #endif #if 0 // test case for the empty stack printf("%d \n", pop()); #endif #if 0 push(1); push(2); push(3); for (int i = 0; i < 3; i++) { printf("%d\n", pop()); } #endif return 0; } |
반응형
LIST
'데이터구조 > 소스코드' 카테고리의 다른 글
OJ 1132번 해답 (0) | 2016.04.11 |
---|---|
Online Judge 1117 문제의 해답 (0) | 2016.04.04 |
Queue 구현코드 (큐) (0) | 2016.04.04 |
Doubly Linked List (DLL) 소스코드 (0) | 2016.03.28 |
Singly Linked List (SLL) 소스코드 (Source code) (0) | 2016.03.24 |