Data Structure - Queue (C)

柔光的暖阳◎ 2023-01-18 14:20 288阅读 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. * Queue.h - by FreeMan
  10. */
  11. typedef int ElementType;
  12. #ifndef _Queue_H_
  13. #define _Queue_H_
  14. struct QueueRecord;
  15. typedef struct QueueRecord *Queue;
  16. int IsQueueEmpty(Queue Q);
  17. int IsQueueFull(Queue Q);
  18. Queue CreateQueue(int MaxElements);
  19. void DisposeQueue(Queue Q);
  20. void MakeQueueEmpty(Queue Q);
  21. void Enqueue(ElementType X, Queue Q);
  22. ElementType FrontOfQueue(Queue Q);
  23. void Dequeue(Queue Q);
  24. ElementType FrontAndDequeue(Queue Q);
  25. #endif
  26. /*
  27. * Queue.c - by FreeMan
  28. */
  29. #include "Queue.h"
  30. #include "..\Fatal.h"
  31. #include <stdlib.h>
  32. #define MinQueueSize ( 5 )
  33. struct QueueRecord
  34. {
  35. int Capacity;
  36. int Front;
  37. int Rear;
  38. int Size;
  39. ElementType *Array;
  40. };
  41. int IsQueueEmpty(Queue Q)
  42. {
  43. return Q->Size == 0;
  44. }
  45. int IsQueueFull(Queue Q)
  46. {
  47. return Q->Size == Q->Capacity;
  48. }
  49. Queue CreateQueue(int MaxElements)
  50. {
  51. Queue Q;
  52. if (MaxElements < MinQueueSize)
  53. {
  54. Error("Queue size is too small!");
  55. }
  56. Q = malloc(sizeof(struct QueueRecord));
  57. if (Q == NULL)
  58. {
  59. FatalError("Out of memory!!");
  60. }
  61. Q->Array = malloc(sizeof(ElementType) * MaxElements);
  62. if (Q->Array == NULL)
  63. {
  64. FatalError("Out of memory!!");
  65. }
  66. Q->Capacity = MaxElements;
  67. MakeQueueEmpty(Q);
  68. return Q;
  69. }
  70. void MakeQueueEmpty(Queue Q)
  71. {
  72. Q->Size = 0;
  73. Q->Front = 1;
  74. Q->Rear = 0;
  75. }
  76. void DisposeQueue(Queue Q)
  77. {
  78. if (Q != NULL)
  79. {
  80. free(Q->Array);
  81. free(Q);
  82. }
  83. }
  84. static int Successor(int Value, Queue Q)
  85. {
  86. if (++Value == Q->Capacity)
  87. {
  88. Value = 0;
  89. }
  90. return Value;
  91. }
  92. void Enqueue(ElementType X, Queue Q)
  93. {
  94. if (IsQueueFull(Q))
  95. {
  96. Error("Full queue!");
  97. }
  98. else
  99. {
  100. Q->Rear = Successor(Q->Rear, Q);
  101. Q->Array[Q->Rear] = X;
  102. Q->Size++;
  103. }
  104. }
  105. ElementType FrontOfQueue(Queue Q)
  106. {
  107. if (!IsQueueEmpty(Q))
  108. {
  109. return Q->Array[Q->Front];
  110. }
  111. Error("Empty queue!");
  112. return 0; /* Return value used to avoid warning */
  113. }
  114. void Dequeue(Queue Q)
  115. {
  116. if (IsQueueEmpty(Q))
  117. {
  118. Error("Empty queue!");
  119. }
  120. else
  121. {
  122. Q->Front = Successor(Q->Front, Q);
  123. Q->Size--;
  124. }
  125. }
  126. ElementType FrontAndDequeue(Queue Q)
  127. {
  128. ElementType X = 0;
  129. if (IsQueueEmpty(Q))
  130. {
  131. Error("Empty queue!");
  132. }
  133. else
  134. {
  135. X = Q->Array[Q->Front];
  136. Q->Front = Successor(Q->Front, Q);
  137. Q->Size--;
  138. }
  139. return X;
  140. }
  141. /*
  142. * QueueTest.c - by FreeMan
  143. */
  144. #include "Queue.h"
  145. #include <stdio.h>
  146. main()
  147. {
  148. Queue Q;
  149. int i;
  150. Q = CreateQueue(12);
  151. for (i = 0; i < 10; i++)
  152. {
  153. Enqueue(i, Q);
  154. }
  155. while (!IsQueueEmpty(Q))
  156. {
  157. printf("%d\n", FrontOfQueue(Q));
  158. Dequeue(Q);
  159. }
  160. for (i = 0; i < 10; i++)
  161. {
  162. Enqueue(i, Q);
  163. }
  164. while (!IsQueueEmpty(Q))
  165. {
  166. printf("%d\n", FrontOfQueue(Q));
  167. Dequeue(Q);
  168. }
  169. DisposeQueue(Q);
  170. return 0;
  171. }
  172. // Output:
  173. /*
  174. 0
  175. 1
  176. 2
  177. 3
  178. 4
  179. 5
  180. 6
  181. 7
  182. 8
  183. 9
  184. 0
  185. 1
  186. 2
  187. 3
  188. 4
  189. 5
  190. 6
  191. 7
  192. 8
  193. 9
  194. */

发表评论

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

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

相关阅读