1 #include "stdio.h" 2 3 #define MAX 5 // stack size 4 5 int push(int); 6 void show_stack(void); 7 void init_stack(void); 8 int pop(void); 9 10 int top=-1; // index , orignal empty 11 int stack[MAX]; 12 13 int main(){ 14 15 int i; 16 init_stack(); 17 18 for (i=0; i < 7; i++){ 19 push(i); 20 } 21 22 for (i=0; i<5; i++){ 23 pop(); 24 } 25 26 show_stack(); 27 28 return 0; 29 } //end main 30 31 // return 0 if succeed and 1 for fail 32 // allow only positive number include 0 33 34 int push(int data){ 35 // FULL 36 if (top == MAX-1){ 37 return 1; 38 } 39 else { 40 stack[++top]=data; 41 return 0; 42 } 43 } //end push 44 45 // return 0 if ok 46 int pop(){ 47 48 int r; 49 50 if (top == -1){ 51 return -1; 52 } 53 else { 54 r=stack[top]; 55 stack[top]=-1; 56 top--; 57 return r; 58 } 59 } //end pop 60 61 void show_stack(){ 62 int i; 63 64 for (i=0; i