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.

139 lines
2.8KB

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