Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
6.9KB

  1. #include <stdio.h>
  2. #include "utlist.h"
  3. typedef struct el {
  4. int id;
  5. struct el *next, *prev;
  6. } el;
  7. int main()
  8. {
  9. int i;
  10. int count;
  11. el els[10], *e;
  12. el *head = NULL;
  13. el *zeroptr = NULL;
  14. for(i=0; i<10; i++) {
  15. els[i].id=(int)'a'+i;
  16. }
  17. /* test CDL macros */
  18. printf("CDL appends\n");
  19. CDL_APPEND(head,&els[0]);
  20. CDL_APPEND(head,&els[1]);
  21. CDL_APPEND(head,&els[2]);
  22. CDL_FOREACH(head,e) {
  23. printf("%c ", e->id);
  24. }
  25. printf("\n");
  26. CDL_COUNT(head,e, count);
  27. printf("count = %d\n", count);
  28. printf("Test CDL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
  29. CDL_PREPEND_ELEM(head, zeroptr, &els[3]);
  30. CDL_FOREACH(head,e) {
  31. printf("%c ", e->id);
  32. }
  33. printf("\n");
  34. printf("Test CDL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
  35. CDL_PREPEND_ELEM(head, &els[1], &els[4]);
  36. CDL_FOREACH(head,e) {
  37. printf("%c ", e->id);
  38. }
  39. printf("\n");
  40. printf("Test CDL_APPEND_ELEM %c with elt NULL\n", els[5].id);
  41. CDL_APPEND_ELEM(head, zeroptr, &els[5]);
  42. CDL_FOREACH(head,e) {
  43. printf("%c ", e->id);
  44. }
  45. printf("\n");
  46. printf("Test CDL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
  47. CDL_APPEND_ELEM(head, &els[1], &els[6]);
  48. CDL_FOREACH(head,e) {
  49. printf("%c ", e->id);
  50. }
  51. printf("\n");
  52. CDL_COUNT(head,e, count);
  53. printf("count = %d\n", count);
  54. /* point head to head->next */
  55. printf("advancing head pointer\n");
  56. head = head->next;
  57. CDL_FOREACH(head,e) {
  58. printf("%c ", e->id);
  59. }
  60. printf("\n");
  61. /* follow circular loop a few times */
  62. for(i=0,e=head; e && i<20; i++,e=e->next) {
  63. printf("%c ", e->id);
  64. }
  65. printf("\n");
  66. /* follow circular loop backwards a few times */
  67. for(i=0,e=head; e && i<10; i++,e=e->prev) {
  68. printf("%c ", e->id);
  69. }
  70. printf("\n");
  71. printf("deleting (b)\n");
  72. CDL_DELETE(head,&els[1]);
  73. CDL_FOREACH(head,e) {
  74. printf("%c ", e->id);
  75. }
  76. printf("\n");
  77. printf("deleting (a)\n");
  78. CDL_DELETE(head,&els[0]);
  79. CDL_FOREACH(head,e) {
  80. printf("%c ", e->id);
  81. }
  82. printf("\n");
  83. printf("deleting (c)\n");
  84. CDL_DELETE(head,&els[2]);
  85. CDL_FOREACH(head,e) {
  86. printf("%c ", e->id);
  87. }
  88. printf("\n");
  89. printf("deleting (g)\n");
  90. CDL_DELETE(head,&els[6]);
  91. CDL_FOREACH(head,e) {
  92. printf("%c ", e->id);
  93. }
  94. printf("\n");
  95. printf("deleting (e)\n");
  96. CDL_DELETE(head,&els[4]);
  97. CDL_FOREACH(head,e) {
  98. printf("%c ", e->id);
  99. }
  100. printf("\n");
  101. printf("deleting (d)\n");
  102. CDL_DELETE(head,&els[3]);
  103. CDL_FOREACH(head,e) {
  104. printf("%c ", e->id);
  105. }
  106. printf("deleting (f)\n");
  107. CDL_DELETE(head,&els[5]);
  108. CDL_FOREACH(head,e) {
  109. printf("%c ", e->id);
  110. }
  111. printf("\n");
  112. /* test DL macros */
  113. printf("DL appends\n");
  114. DL_APPEND(head,&els[0]);
  115. DL_APPEND(head,&els[1]);
  116. DL_APPEND(head,&els[2]);
  117. DL_FOREACH(head,e) {
  118. printf("%c ", e->id);
  119. }
  120. printf("\n");
  121. DL_COUNT(head,e, count);
  122. printf("count = %d\n", count);
  123. printf("Test DL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
  124. DL_PREPEND_ELEM(head, zeroptr, &els[3]);
  125. DL_FOREACH(head,e) {
  126. printf("%c ", e->id);
  127. }
  128. printf("\n");
  129. printf("Test DL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
  130. DL_PREPEND_ELEM(head, &els[1], &els[4]);
  131. DL_FOREACH(head,e) {
  132. printf("%c ", e->id);
  133. }
  134. printf("\n");
  135. printf("Test DL_APPEND_ELEM %c with elt NULL\n", els[5].id);
  136. DL_APPEND_ELEM(head, zeroptr, &els[5]);
  137. DL_FOREACH(head,e) {
  138. printf("%c ", e->id);
  139. }
  140. printf("\n");
  141. printf("Test DL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
  142. DL_APPEND_ELEM(head, &els[1], &els[6]);
  143. DL_FOREACH(head,e) {
  144. printf("%c ", e->id);
  145. }
  146. printf("\n");
  147. DL_COUNT(head,e, count);
  148. printf("count = %d\n", count);
  149. printf("deleting (b)\n");
  150. DL_DELETE(head,&els[1]);
  151. DL_FOREACH(head,e) {
  152. printf("%c ", e->id);
  153. }
  154. printf("\n");
  155. printf("deleting (a)\n");
  156. DL_DELETE(head,&els[0]);
  157. DL_FOREACH(head,e) {
  158. printf("%c ", e->id);
  159. }
  160. printf("\n");
  161. printf("deleting (c)\n");
  162. DL_DELETE(head,&els[2]);
  163. DL_FOREACH(head,e) {
  164. printf("%c ", e->id);
  165. }
  166. printf("\n");
  167. printf("deleting (g)\n");
  168. DL_DELETE(head,&els[6]);
  169. DL_FOREACH(head,e) {
  170. printf("%c ", e->id);
  171. }
  172. printf("\n");
  173. printf("deleting (e)\n");
  174. DL_DELETE(head,&els[4]);
  175. DL_FOREACH(head,e) {
  176. printf("%c ", e->id);
  177. }
  178. printf("\n");
  179. printf("deleting (d)\n");
  180. DL_DELETE(head,&els[3]);
  181. DL_FOREACH(head,e) {
  182. printf("%c ", e->id);
  183. }
  184. printf("deleting (f)\n");
  185. DL_DELETE(head,&els[5]);
  186. DL_FOREACH(head,e) {
  187. printf("%c ", e->id);
  188. }
  189. printf("\n");
  190. /* test LL macros */
  191. printf("LL appends\n");
  192. LL_APPEND(head,&els[0]);
  193. LL_APPEND(head,&els[1]);
  194. LL_APPEND(head,&els[2]);
  195. LL_FOREACH(head,e) {
  196. printf("%c ", e->id);
  197. }
  198. printf("\n");
  199. LL_COUNT(head,e, count);
  200. printf("count = %d\n", count);
  201. printf("Test LL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
  202. LL_PREPEND_ELEM(head, zeroptr, &els[3]);
  203. LL_FOREACH(head,e) {
  204. printf("%c ", e->id);
  205. }
  206. printf("\n");
  207. printf("Test LL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
  208. LL_PREPEND_ELEM(head, &els[1], &els[4]);
  209. LL_FOREACH(head,e) {
  210. printf("%c ", e->id);
  211. }
  212. printf("\n");
  213. printf("Test LL_APPEND_ELEM %c with elt NULL\n", els[5].id);
  214. LL_APPEND_ELEM(head, zeroptr, &els[5]);
  215. LL_FOREACH(head,e) {
  216. printf("%c ", e->id);
  217. }
  218. printf("\n");
  219. printf("Test LL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
  220. LL_APPEND_ELEM(head, &els[1], &els[6]);
  221. LL_FOREACH(head,e) {
  222. printf("%c ", e->id);
  223. }
  224. printf("\n");
  225. LL_COUNT(head,e, count);
  226. printf("count = %d\n", count);
  227. printf("deleting (b)\n");
  228. LL_DELETE(head,&els[1]);
  229. LL_FOREACH(head,e) {
  230. printf("%c ", e->id);
  231. }
  232. printf("\n");
  233. printf("deleting (a)\n");
  234. LL_DELETE(head,&els[0]);
  235. LL_FOREACH(head,e) {
  236. printf("%c ", e->id);
  237. }
  238. printf("\n");
  239. printf("deleting (c)\n");
  240. LL_DELETE(head,&els[2]);
  241. LL_FOREACH(head,e) {
  242. printf("%c ", e->id);
  243. }
  244. printf("\n");
  245. printf("deleting (g)\n");
  246. LL_DELETE(head,&els[6]);
  247. LL_FOREACH(head,e) {
  248. printf("%c ", e->id);
  249. }
  250. printf("\n");
  251. printf("deleting (e)\n");
  252. LL_DELETE(head,&els[4]);
  253. LL_FOREACH(head,e) {
  254. printf("%c ", e->id);
  255. }
  256. printf("\n");
  257. printf("deleting (d)\n");
  258. LL_DELETE(head,&els[3]);
  259. LL_FOREACH(head,e) {
  260. printf("%c ", e->id);
  261. }
  262. printf("deleting (f)\n");
  263. LL_DELETE(head,&els[5]);
  264. LL_FOREACH(head,e) {
  265. printf("%c ", e->id);
  266. }
  267. printf("\n");
  268. return 0;
  269. }