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

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

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

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

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

    主要操作如下:

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