Data Structure - List (C)

客官°小女子只卖身不卖艺 2023-01-13 15:51 258阅读 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. * List.h - by FreeMan
  10. */
  11. typedef int ElementType;
  12. #ifndef _List_H_
  13. #define _List_H_
  14. struct Node;
  15. typedef struct Node *PtrToNode;
  16. typedef PtrToNode List;
  17. typedef PtrToNode Position;
  18. List MakeListEmpty(List L);
  19. int IsListEmpty(List L);
  20. int IsListLast(Position P, List L);
  21. Position FindInList(ElementType X, List L);
  22. void DeleteInList(ElementType X, List L);
  23. Position FindPreviousInList(ElementType X, List L);
  24. void InsertInList(ElementType X, List L, Position P);
  25. void DeleteList(List L);
  26. Position HeaderOfList(List L);
  27. Position FirstOfList(List L);
  28. Position AdvanceInList(Position P);
  29. ElementType RetrieveInList(Position P);
  30. #endif
  31. /*
  32. * List.c - by FreeMan
  33. */
  34. #include "List.h"
  35. #include "..\Fatal.h"
  36. #include <stdlib.h>
  37. struct Node
  38. {
  39. ElementType Element;
  40. Position Next;
  41. };
  42. List MakeListEmpty(List L)
  43. {
  44. if (L != NULL)
  45. {
  46. DeleteList(L);
  47. }
  48. L = malloc(sizeof(struct Node));
  49. if (L == NULL)
  50. {
  51. FatalError("Out of memory!");
  52. }
  53. L->Next = NULL;
  54. return L;
  55. }
  56. int IsListEmpty(List L)
  57. {
  58. return L->Next == NULL;
  59. }
  60. int IsListLast(Position P, List L)
  61. {
  62. return P->Next == NULL;
  63. }
  64. // Return Position of X in L; NULL if not found.
  65. Position FindInList(ElementType X, List L)
  66. {
  67. Position P;
  68. P = L->Next;
  69. while (P != NULL && P->Element != X)
  70. {
  71. P = P->Next;
  72. }
  73. return P;
  74. }
  75. void DeleteInList(ElementType X, List L)
  76. {
  77. Position P, TmpCell;
  78. P = FindPreviousInList(X, L);
  79. if (!IsListLast(P, L))
  80. {
  81. TmpCell = P->Next;
  82. P->Next = TmpCell->Next;
  83. free(TmpCell);
  84. }
  85. }
  86. Position FindPreviousInList(ElementType X, List L)
  87. {
  88. Position P;
  89. P = L;
  90. while (P->Next != NULL && P->Next->Element != X)
  91. {
  92. P = P->Next;
  93. }
  94. return P;
  95. }
  96. // Insert (after legal position P).
  97. void InsertInList(ElementType X, List L, Position P)
  98. {
  99. Position TmpCell;
  100. TmpCell = malloc(sizeof(struct Node));
  101. if (TmpCell == NULL)
  102. {
  103. FatalError("Out of memory!");
  104. }
  105. TmpCell->Element = X;
  106. TmpCell->Next = P->Next;
  107. P->Next = TmpCell;
  108. }
  109. void DeleteList(List L)
  110. {
  111. Position P, Tmp;
  112. P = L->Next;
  113. L->Next = NULL;
  114. while (P != NULL)
  115. {
  116. Tmp = P->Next;
  117. free(P);
  118. P = Tmp;
  119. }
  120. }
  121. Position HeaderOfList(List L)
  122. {
  123. return L;
  124. }
  125. Position FirstOfList(List L)
  126. {
  127. return L->Next;
  128. }
  129. Position AdvanceInList(Position P)
  130. {
  131. return P->Next;
  132. }
  133. ElementType RetrieveInList(Position P)
  134. {
  135. return P->Element;
  136. }
  137. /*
  138. * ListTest.c - by FreeMan
  139. */
  140. #include "List.h"
  141. #include <stdio.h>
  142. void PrintList(const List L)
  143. {
  144. Position P = HeaderOfList(L);
  145. if (IsListEmpty(L))
  146. {
  147. printf("Empty list\n");
  148. }
  149. else
  150. {
  151. do
  152. {
  153. P = AdvanceInList(P);
  154. printf("%d ", RetrieveInList(P));
  155. } while (!IsListLast(P, L));
  156. printf("\n");
  157. }
  158. }
  159. main()
  160. {
  161. List L;
  162. Position P;
  163. int i;
  164. L = MakeListEmpty(NULL);
  165. P = HeaderOfList(L);
  166. PrintList(L);
  167. for (i = 0; i < 10; i++)
  168. {
  169. InsertInList(i, L, P);
  170. PrintList(L);
  171. P = AdvanceInList(P);
  172. }
  173. for (i = 0; i < 10; i += 2)
  174. {
  175. DeleteInList(i, L);
  176. }
  177. printf("Finished deletions\n");
  178. for (i = 0; i < 10; i++)
  179. {
  180. if ((i % 2 == 0) == (FindInList(i, L) != NULL))
  181. {
  182. printf("Find fails\n");
  183. }
  184. }
  185. PrintList(L);
  186. DeleteList(L);
  187. return 0;
  188. }
  189. // Output:
  190. /*
  191. Empty list
  192. 0
  193. 0 1
  194. 0 1 2
  195. 0 1 2 3
  196. 0 1 2 3 4
  197. 0 1 2 3 4 5
  198. 0 1 2 3 4 5 6
  199. 0 1 2 3 4 5 6 7
  200. 0 1 2 3 4 5 6 7 8
  201. 0 1 2 3 4 5 6 7 8 9
  202. Finished deletions
  203. 1 3 5 7 9
  204. */

发表评论

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

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

相关阅读