Data Structure - List Stack (C)

àì夳堔傛蜴生んèń 2023-01-17 06:22 275阅读 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. * LiStack.h - by FreeMan
  10. */
  11. typedef int ElementType;
  12. #ifndef _LiStack_H_
  13. #define _LiStack_H_
  14. struct LiStackNode;
  15. typedef struct LiStackNode *PtrToNode;
  16. typedef PtrToNode LiStack;
  17. int IsLiStackEmpty(LiStack S);
  18. LiStack CreateLiStack(void);
  19. void DisposeLiStack(LiStack S);
  20. void MakeLiStackEmpty(LiStack S);
  21. void PushLiStack(ElementType X, LiStack S);
  22. ElementType TopOfLiStack(LiStack S);
  23. void PopLiStack(LiStack S);
  24. #endif
  25. /*
  26. * LiStack.c - by FreeMan
  27. */
  28. #include "LiStack.h"
  29. #include "..\Fatal.h"
  30. #include <stdlib.h>
  31. struct LiStackNode
  32. {
  33. ElementType Element;
  34. PtrToNode Next;
  35. };
  36. int IsLiStackEmpty(LiStack S)
  37. {
  38. return S->Next == NULL;
  39. }
  40. LiStack CreateLiStack(void)
  41. {
  42. LiStack S;
  43. S = malloc(sizeof(struct LiStackNode));
  44. if (S == NULL)
  45. {
  46. FatalError("Out of memory!!");
  47. }
  48. S->Next = NULL;
  49. MakeLiStackEmpty(S);
  50. return S;
  51. }
  52. void MakeLiStackEmpty(LiStack S)
  53. {
  54. if (S == NULL)
  55. {
  56. Error("Must use CreateLiStack first!");
  57. }
  58. else
  59. {
  60. while (!IsLiStackEmpty(S))
  61. {
  62. PopLiStack(S);
  63. }
  64. }
  65. }
  66. void DisposeLiStack(LiStack S)
  67. {
  68. MakeLiStackEmpty(S);
  69. free(S);
  70. }
  71. void PushLiStack(ElementType X, LiStack S)
  72. {
  73. PtrToNode TmpCell;
  74. TmpCell = malloc(sizeof(struct LiStackNode));
  75. if (TmpCell == NULL)
  76. {
  77. FatalError("Out of memory!!");
  78. }
  79. else
  80. {
  81. TmpCell->Element = X;
  82. TmpCell->Next = S->Next;
  83. S->Next = TmpCell;
  84. }
  85. }
  86. ElementType TopOfLiStack(LiStack S)
  87. {
  88. if (!IsLiStackEmpty(S))
  89. {
  90. return S->Next->Element;
  91. }
  92. Error("Empty stack!");
  93. return 0; /* Return value used to avoid warning */
  94. }
  95. void PopLiStack(LiStack S)
  96. {
  97. PtrToNode FirstCell;
  98. if (IsLiStackEmpty(S))
  99. {
  100. Error("Empty stack!");
  101. }
  102. else
  103. {
  104. FirstCell = S->Next;
  105. S->Next = S->Next->Next;
  106. free(FirstCell);
  107. }
  108. }
  109. /*
  110. * LiStackTest.c - by FreeMan
  111. */
  112. #include "LiStack.h"
  113. #include <stdio.h>
  114. main()
  115. {
  116. LiStack S;
  117. int i;
  118. S = CreateLiStack();
  119. for (i = 0; i < 10; i++)
  120. {
  121. PushLiStack(i, S);
  122. }
  123. while (!IsLiStackEmpty(S))
  124. {
  125. printf("%d\n", TopOfLiStack(S));
  126. PopLiStack(S);
  127. }
  128. DisposeLiStack(S);
  129. return 0;
  130. }
  131. // Output:
  132. /*
  133. 9
  134. 8
  135. 7
  136. 6
  137. 5
  138. 4
  139. 3
  140. 2
  141. 1
  142. 0
  143. */

发表评论

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

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

相关阅读