Data Structure - Array Stack (C)

╰+攻爆jí腚メ 2023-01-16 14:26 251阅读 0赞

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

  1. /*
  2. * Fatal.h - by FreeMan
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define Error( Str ) FatalError( Str )
  7. #define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
  8. /*
  9. * ArStack.h - by FreeMan
  10. */
  11. typedef int ElementType;
  12. #ifndef _ArStack_H_
  13. #define _ArStack_H_
  14. struct ArStackRecord;
  15. typedef struct ArStackRecord *ArStack;
  16. int IsArStackEmpty(ArStack S);
  17. int IsArStackFull(ArStack S);
  18. ArStack CreateArStack(int MaxElements);
  19. void DisposeArStack(ArStack S);
  20. void MakeArStackEmpty(ArStack S);
  21. void PushArStack(ElementType X, ArStack S);
  22. ElementType TopOfArStack(ArStack S);
  23. void PopArStack(ArStack S);
  24. ElementType TopAndPopArStack(ArStack S);
  25. #endif
  26. /*
  27. * ArStack.c - by FreeMan
  28. */
  29. #include "ArStack.h"
  30. #include "..\Fatal.h"
  31. #include <stdlib.h>
  32. #define EmptyTOS ( -1 )
  33. #define MinStackSize ( 5 )
  34. struct ArStackRecord
  35. {
  36. int Capacity;
  37. int TopOfStack;
  38. ElementType *Array;
  39. };
  40. int IsArStackEmpty(ArStack S)
  41. {
  42. return S->TopOfStack == EmptyTOS;
  43. }
  44. int IsArStackFull(ArStack S)
  45. {
  46. return S->TopOfStack == S->Capacity - 1;
  47. }
  48. ArStack CreateArStack(int MaxElements)
  49. {
  50. ArStack S;
  51. if (MaxElements < MinStackSize)
  52. {
  53. Error("Stack size is too small!");
  54. }
  55. S = malloc(sizeof(struct ArStackRecord));
  56. if (S == NULL)
  57. {
  58. FatalError("Out of memory!!");
  59. }
  60. S->Array = malloc(sizeof(ElementType) * MaxElements);
  61. if (S->Array == NULL)
  62. {
  63. FatalError("Out of memory!!");
  64. }
  65. S->Capacity = MaxElements;
  66. MakeArStackEmpty(S);
  67. return S;
  68. }
  69. void MakeArStackEmpty(ArStack S)
  70. {
  71. S->TopOfStack = EmptyTOS;
  72. }
  73. void DisposeArStack(ArStack S)
  74. {
  75. if (S != NULL)
  76. {
  77. free(S->Array);
  78. free(S);
  79. }
  80. }
  81. void PushArStack(ElementType X, ArStack S)
  82. {
  83. if (IsArStackFull(S))
  84. {
  85. Error("Full stack!");
  86. }
  87. else
  88. {
  89. S->Array[++S->TopOfStack] = X;
  90. }
  91. }
  92. ElementType TopOfArStack(ArStack S)
  93. {
  94. if (!IsArStackEmpty(S))
  95. {
  96. return S->Array[S->TopOfStack];
  97. }
  98. Error("Empty stack!");
  99. return 0;
  100. }
  101. void PopArStack(ArStack S)
  102. {
  103. if (IsArStackEmpty(S))
  104. {
  105. Error("Empty stack!");
  106. }
  107. else
  108. {
  109. S->TopOfStack--;
  110. }
  111. }
  112. ElementType TopAndPopArStack(ArStack S)
  113. {
  114. if (!IsArStackEmpty(S))
  115. {
  116. return S->Array[S->TopOfStack--];
  117. }
  118. Error("Empty stack!");
  119. return 0;
  120. }
  121. /*
  122. * ArStackTest.c - by FreeMan
  123. */
  124. #include "ArStack.h"
  125. #include <stdio.h>
  126. main()
  127. {
  128. ArStack S;
  129. int i;
  130. S = CreateArStack(12);
  131. for (i = 0; i < 10; i++)
  132. {
  133. PushArStack(i, S);
  134. }
  135. while (!IsArStackEmpty(S))
  136. {
  137. printf("%d\n", TopOfArStack(S));
  138. PopArStack(S);
  139. }
  140. DisposeArStack(S);
  141. return 0;
  142. }
  143. // Output:
  144. /*
  145. 9
  146. 8
  147. 7
  148. 6
  149. 5
  150. 4
  151. 3
  152. 2
  153. 1
  154. 0
  155. */

发表评论

表情:
评论列表 (有 0 条评论,251人围观)

还没有评论,来说两句吧...

相关阅读