上文提到过栈以及栈的基本操作。上文中是基于链表做的实现。但是这种方法会出现大量的malloc()和free()操作,这种开销是非常昂贵的。

    另外一种实现方式是基于数组的实现。这种实现方式需要预先制定一个栈的大小,此外还需要一个Top来记录栈顶元素下一个位置的数组索引值。如下图所示:

    有的教材将Top指向栈顶元素,也就是上图中X所在的数组单元。我们这里不这么认为。

    这种情况下栈的数据结构定义如下:

  1. typedef struct StackRecord *Stack; 
  2. struct StackRecord 
  3. { 
  4.     int Capacity; 
  5.     int Top; 
  6.     int *Array; 
  7. }; 

    主要操作如下:

  1. //判断栈是否为空 
  2. int IsEmpty(Stack S); 
  3. //判断栈是否已满 
  4. int IsFull(Stack S); 
  5. //创建栈 
  6. Stack CreateStack(int MaxElements); 
  7. //回收栈 
  8. void DisposeStack(Stack S); 
  9. //清空栈 
  10. void MakeEmpty(Stack S); 
  11. //进栈操作 
  12. void Push(int X, Stack S); 
  13. //返回栈顶元素 
  14. int Top(Stack S); 
  15. //出栈操作 
  16. void Pop(Stack S); 
  17. //出栈并且返回栈定元素 
  18. int PopAndTop(Stack S); 

    一个完整的例子代码如下:

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #define MIN_STACK_SIZE 5 
  4.  
  5. typedef struct StackRecord *Stack; 
  6.  
  7. struct StackRecord 
  8. { 
  9.     int Capacity; 
  10.     int Top; 
  11.     int *Array; 
  12. }; 
  13.  
  14. //判断栈是否为空 
  15. int IsEmpty(Stack S); 
  16. //判断栈是否已满 
  17. int IsFull(Stack S); 
  18. //创建栈 
  19. Stack CreateStack(int MaxElements); 
  20. //回收栈 
  21. void DisposeStack(Stack S); 
  22. //清空栈 
  23. void MakeEmpty(Stack S); 
  24. //进栈操作 
  25. void Push(int X, Stack S); 
  26. //返回栈顶元素 
  27. int Top(Stack S); 
  28. //出栈操作 
  29. void Pop(Stack S); 
  30. //出栈并且返回栈定元素 
  31. int PopAndTop(Stack S); 
  32.  
  33. int IsEmpty(Stack S) 
  34. { 
  35.     return S->Top == 0; 
  36. } 
  37.  
  38. int IsFull(Stack S) 
  39. { 
  40.     return S->Top == S->Capacity; 
  41. } 
  42.  
  43. void MakeEmpty(Stack S) 
  44. { 
  45.     S->Top = 0; 
  46. } 
  47.  
  48. Stack CreateStack(int MaxElements) 
  49. { 
  50.     if(MaxElements < MIN_STACK_SIZE) 
  51.     { 
  52.         fprintf(stderr, "Can't create a Stack less than %d elements\n",MIN_STACK_SIZE); 
  53.         exit(1); 
  54.     } 
  55.     else 
  56.     { 
  57.         Stack S = malloc(sizeof(struct StackRecord)); 
  58.         if(S == NULL) 
  59.         { 
  60.             fprintf(stderr, "Out of space!"); 
  61.             exit(1); 
  62.         } 
  63.         S->Array = malloc(sizeof(int)*MaxElements); 
  64.         S->Capacity = MaxElements; 
  65.         MakeEmpty(S); 
  66.         return S; 
  67.     } 
  68. } 
  69.  
  70.  
  71. void DisposeStack(Stack S) 
  72. { 
  73.     if(S != NULL) 
  74.     { 
  75.         free(S->Array); 
  76.         free(S); 
  77.     } 
  78. } 
  79.  
  80. void Push(int X, Stack S) 
  81. { 
  82.     if(IsFull(S)) 
  83.     { 
  84.         fprintf(stderr,"The Stack Is Full"); 
  85.     } 
  86.     else 
  87.     { 
  88.         //元素先入栈 
  89.         S->Array[S->Top++] = X; 
  90.     } 
  91. } 
  92.  
  93. int Top(Stack S) 
  94. { 
  95.     if(!IsEmpty(S)) 
  96.     { 
  97.         int tmp = S->Top - 1; 
  98.         return S->Array[tmp]; 
  99.     } 
  100.     else 
  101.     { 
  102.         fprintf(stderr,"The Stack Is Full"); 
  103.         exit(1); 
  104.     } 
  105.      
  106. } 
  107.  
  108. void Pop(Stack S) 
  109. { 
  110.     if(!IsEmpty(S)) 
  111.     { 
  112.         --(S->Top); 
  113.     } 
  114.     else 
  115.     { 
  116.         fprintf(stderr,"The Stack Is Full"); 
  117.         exit(1); 
  118.     } 
  119. } 
  120. int PopAndTop(Stack S) 
  121. { 
  122.     if(!IsEmpty(S)) 
  123.     { 
  124.         return S->Array[--S->Top]; 
  125.     } 
  126.     else 
  127.     { 
  128.         fprintf(stderr,"The Stack Is Full"); 
  129.         exit(1); 
  130.     } 
  131. } 
  132.  
  133. int main(void)  
  134. { 
  135.     Stack S = CreateStack(10); 
  136.     int i; 
  137.     for(i = 0; i < 10; i++) 
  138.     { 
  139.         Push(i,S); 
  140.     } 
  141.     while(!IsEmpty(S)) 
  142.     { 
  143.         printf("%d ",PopAndTop(S)); 
  144.     } 
  145.     printf("\n"); 
  146.     return 0; 
  147. }