用数组模拟栈的实现:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define STACK_SIZE 100 4 typedef struct Stack 5 { 6 int top; 7 int stack[STACK_SIZE]; 8 }Stack; 9 void InitStack(Stack *s) 10 { 11 s->top=-1; 12 } 13 int IsEmpty(Stack *s) 14 { 15 return s->top==-1? 1:0; 16 } 17 int IsFull(Stack *s) 18 { 19 if(s->top>=STACK_SIZE-1) 20 return 1; 21 return 0; 22 } 23 int Pop(Stack *s) 24 { 25 if(!IsEmpty(s)) 26 { 27 s->top--; 28 return s->stack[s->top+1]; 29 } 30 return -1; 31 } 32 void Push(Stack *s,int m) 33 { 34 if(!IsFull(s)) 35 { 36 s->top++; 37 s->stack[s->top]=m; 38 } 39 } 40 int main() 41 { 42 Stack *s=(Stack*)malloc(sizeof(Stack)); 43 InitStack(s); 44 Push(s,2); 45 Push(s,8); 46 Push(s,10); 47 printf("%3d",Pop(s)); 48 return 0; 49 }
仅供参考。。