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.

154 lines
3.1KB

  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. el els[10], *e;
  11. el *head = NULL;
  12. for(i=0; i<10; i++) {
  13. els[i].id=(int)'a'+i;
  14. }
  15. /* test CDL macros */
  16. printf("CDL macros\n");
  17. CDL_PREPEND(head,&els[0]);
  18. CDL_PREPEND(head,&els[1]);
  19. CDL_PREPEND(head,&els[2]);
  20. CDL_PREPEND(head,&els[3]);
  21. CDL_FOREACH(head,e) {
  22. printf("%c ", e->id);
  23. }
  24. printf("\n");
  25. /* point head to head->next */
  26. printf("advancing head pointer\n");
  27. head = head->next;
  28. CDL_FOREACH(head,e) {
  29. printf("%c ", e->id);
  30. }
  31. printf("\n");
  32. /* follow circular loop a few times */
  33. for(i=0,e=head; e && i<10; i++,e=e->next) {
  34. printf("%c ", e->id);
  35. }
  36. printf("\n");
  37. /* follow circular loop backwards a few times */
  38. for(i=0,e=head; e && i<10; i++,e=e->prev) {
  39. printf("%c ", e->id);
  40. }
  41. printf("\n");
  42. printf("deleting b\n");
  43. CDL_DELETE(head,&els[1]);
  44. CDL_FOREACH(head,e) {
  45. printf("%c ", e->id);
  46. }
  47. printf("\n");
  48. printf("deleting (a)\n");
  49. CDL_DELETE(head,&els[0]);
  50. CDL_FOREACH(head,e) {
  51. printf("%c ", e->id);
  52. }
  53. printf("\n");
  54. printf("deleting (c)\n");
  55. CDL_DELETE(head,&els[2]);
  56. CDL_FOREACH(head,e) {
  57. printf("%c ", e->id);
  58. }
  59. printf("\n");
  60. printf("deleting (d)\n");
  61. CDL_DELETE(head,&els[3]);
  62. CDL_FOREACH(head,e) {
  63. printf("%c ", e->id);
  64. }
  65. printf("\n");
  66. /* test DL macros */
  67. printf("DL macros\n");
  68. DL_PREPEND(head,&els[0]);
  69. DL_PREPEND(head,&els[1]);
  70. DL_PREPEND(head,&els[2]);
  71. DL_APPEND(head,&els[3]);
  72. DL_FOREACH(head,e) {
  73. printf("%c ", e->id);
  74. }
  75. printf("\n");
  76. printf("deleting c\n");
  77. DL_DELETE(head,&els[2]);
  78. DL_FOREACH(head,e) {
  79. printf("%c ", e->id);
  80. }
  81. printf("\n");
  82. printf("deleting a\n");
  83. DL_DELETE(head,&els[0]);
  84. DL_FOREACH(head,e) {
  85. printf("%c ", e->id);
  86. }
  87. printf("\n");
  88. printf("deleting b\n");
  89. DL_DELETE(head,&els[1]);
  90. DL_FOREACH(head,e) {
  91. printf("%c ", e->id);
  92. }
  93. printf("\n");
  94. printf("deleting d\n");
  95. DL_DELETE(head,&els[3]);
  96. DL_FOREACH(head,e) {
  97. printf("%c ", e->id);
  98. }
  99. printf("\n");
  100. /* test LL macros */
  101. printf("LL macros\n");
  102. LL_PREPEND(head,&els[0]);
  103. LL_PREPEND(head,&els[1]);
  104. LL_PREPEND(head,&els[2]);
  105. LL_APPEND(head,&els[3]);
  106. LL_FOREACH(head,e) {
  107. printf("%c ", e->id);
  108. }
  109. printf("\n");
  110. printf("deleting c\n");
  111. LL_DELETE(head,&els[2]);
  112. LL_FOREACH(head,e) {
  113. printf("%c ", e->id);
  114. }
  115. printf("\n");
  116. printf("deleting a\n");
  117. LL_DELETE(head,&els[0]);
  118. LL_FOREACH(head,e) {
  119. printf("%c ", e->id);
  120. }
  121. printf("\n");
  122. printf("deleting b\n");
  123. LL_DELETE(head,&els[1]);
  124. LL_FOREACH(head,e) {
  125. printf("%c ", e->id);
  126. }
  127. printf("\n");
  128. printf("deleting d\n");
  129. LL_DELETE(head,&els[3]);
  130. LL_FOREACH(head,e) {
  131. printf("%c ", e->id);
  132. }
  133. printf("\n");
  134. return 0;
  135. }