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.

131 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. 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_PREPEND2(head,&els[0],Prev,Next);
  18. CDL_PREPEND2(head,&els[1],Prev,Next);
  19. CDL_PREPEND2(head,&els[2],Prev,Next);
  20. CDL_FOREACH2(head,e,Next) {
  21. printf("%c ", e->id);
  22. }
  23. printf("\n");
  24. /* point head to head->next */
  25. printf("advancing head pointer\n");
  26. head = head->Next;
  27. CDL_FOREACH2(head,e,Next) {
  28. printf("%c ", e->id);
  29. }
  30. printf("\n");
  31. /* follow circular loop a few times */
  32. for(i=0,e=head; e && i<10; i++,e=e->Next) {
  33. printf("%c ", e->id);
  34. }
  35. printf("\n");
  36. /* follow circular loop backwards a few times */
  37. for(i=0,e=head; e && i<10; i++,e=e->Prev) {
  38. printf("%c ", e->id);
  39. }
  40. printf("\n");
  41. printf("deleting b\n");
  42. CDL_DELETE2(head,&els[1],Prev,Next);
  43. CDL_FOREACH2(head,e,Next) {
  44. printf("%c ", e->id);
  45. }
  46. printf("\n");
  47. printf("deleting (a)\n");
  48. CDL_DELETE2(head,&els[0],Prev,Next);
  49. CDL_FOREACH2(head,e,Next) {
  50. printf("%c ", e->id);
  51. }
  52. printf("\n");
  53. printf("deleting (c)\n");
  54. CDL_DELETE2(head,&els[2],Prev,Next);
  55. CDL_FOREACH2(head,e,Next) {
  56. printf("%c ", e->id);
  57. }
  58. printf("\n");
  59. /* test DL macros */
  60. printf("DL macros\n");
  61. DL_APPEND2(head,&els[0],Prev,Next);
  62. DL_APPEND2(head,&els[1],Prev,Next);
  63. DL_APPEND2(head,&els[2],Prev,Next);
  64. DL_FOREACH2(head,e,Next) {
  65. printf("%c ", e->id);
  66. }
  67. printf("\n");
  68. printf("deleting tail c\n");
  69. DL_DELETE2(head,&els[2],Prev,Next);
  70. DL_FOREACH2(head,e,Next) {
  71. printf("%c ", e->id);
  72. }
  73. printf("\n");
  74. printf("deleting head a\n");
  75. DL_DELETE2(head,&els[0],Prev,Next);
  76. DL_FOREACH2(head,e,Next) {
  77. printf("%c ", e->id);
  78. }
  79. printf("\n");
  80. printf("deleting head b\n");
  81. DL_DELETE2(head,&els[1],Prev,Next);
  82. DL_FOREACH2(head,e,Next) {
  83. printf("%c ", e->id);
  84. }
  85. printf("\n");
  86. /* test LL macros */
  87. printf("LL macros\n");
  88. LL_APPEND2(head,&els[0],Next);
  89. LL_APPEND2(head,&els[1],Next);
  90. LL_APPEND2(head,&els[2],Next);
  91. LL_FOREACH2(head,e,Next) {
  92. printf("%c ", e->id);
  93. }
  94. printf("\n");
  95. printf("deleting tail c\n");
  96. LL_DELETE2(head,&els[2],Next);
  97. LL_FOREACH2(head,e,Next) {
  98. printf("%c ", e->id);
  99. }
  100. printf("\n");
  101. printf("deleting head a\n");
  102. LL_DELETE2(head,&els[0],Next);
  103. LL_FOREACH2(head,e,Next) {
  104. printf("%c ", e->id);
  105. }
  106. printf("\n");
  107. printf("deleting head b\n");
  108. LL_DELETE2(head,&els[1],Next);
  109. LL_FOREACH2(head,e,Next) {
  110. printf("%c ", e->id);
  111. }
  112. printf("\n");
  113. return 0;
  114. }