Advertisement
193030

DR 6.2 da reverse-na

Nov 28th, 2021
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node2
  5. {
  6. void* data;
  7. struct node2* one;
  8. struct node2* two;
  9. };
  10.  
  11. struct node1 {
  12. void* data;
  13. struct node1* next;
  14. };
  15.  
  16.  
  17. void prnint(void* p)
  18. {
  19. printf("%d ", *(int*)p);
  20. }
  21.  
  22. struct node2* newNode(int data)
  23. {
  24. void* p = malloc(sizeof(data));
  25. *(int*)p = data;
  26. struct node2* t = (struct node2*)malloc(sizeof(struct node2));
  27. t->data = p;
  28. t->one = NULL;
  29. t->two = NULL;
  30. return t;
  31. }
  32.  
  33. void printList(struct node2* p)
  34. {
  35. //printf("\n");
  36. static struct node2* root;
  37. if (root == NULL)
  38. root = p;
  39. while (p != NULL)
  40. {
  41. prnint(p->data);
  42. p = p->two;
  43. if (p == root) // Възлите на цикъла започват да се повтарят
  44. break;
  45. }
  46. }
  47.  
  48.  
  49.  
  50. void display(struct node1** head)
  51. {
  52. struct node1* t = *head;
  53. do {
  54. prnint(t->data);
  55. t= t->next;
  56. } while (t != *head);
  57. }
  58.  
  59. void add1(void* data, struct node1** head)
  60. {
  61. if(head == NULL)
  62. {
  63. struct node1 *p = (struct node1*)malloc(sizeof(struct node1));
  64. p->data = data;
  65. p->next = NULL;
  66. *head = p;
  67. return;
  68. }
  69. else
  70. {
  71. struct node1* p = head;
  72. while(p->next)
  73. {
  74. p = p->next;
  75. }
  76. struct node1 *t = (struct node1*)malloc(sizeof(struct node1));
  77. t->data = data;
  78. t->next = NULL;
  79. p->next = t;
  80. }
  81. }
  82.  
  83. void add2(void* data, struct node1** head)
  84. {
  85. struct node1* p = (struct node1*)malloc(sizeof(struct node1));
  86. p->data = data;
  87. if (head != NULL)
  88. p->next = *head;
  89. else
  90. p->next = NULL;
  91. *head = p;
  92. }
  93.  
  94. void preorder(struct node2* p, struct node1** listHead)
  95. {
  96. if (!p)
  97. return;
  98. add1(p->data, listHead);
  99. preorder(p->one, listHead);
  100. preorder(p->two, listHead);
  101. }
  102.  
  103. void postorder(struct node2* p, struct node1 **listHead)
  104. {
  105. if (!p)
  106. return;
  107. postorder(p->one, listHead);
  108. postorder(p->two, listHead);
  109. add2(p->data, listHead);
  110. }
  111.  
  112.  
  113. void postorderPrint(struct node2* p)
  114. {
  115. if (!p)
  116. return;
  117. postorderPrint(p->one);
  118. postorderPrint(p->two);
  119. prnint(p->data);
  120.  
  121. }
  122. void preorderPrint(struct node2* p)
  123. {
  124. if (!p)
  125. return;
  126. prnint(p->data);
  127. preorderPrint(p->one);
  128. preorderPrint(p->two);
  129. }
  130.  
  131.  
  132. void postorderFUN(struct node2* p, struct node1** listHead)
  133. {
  134. postorder(p, listHead);
  135. struct node1* t = *listHead;
  136. while (t->next != NULL)
  137. t = t->next;
  138. t->next = *listHead;
  139. putchar('\n');
  140. }
  141.  
  142.  
  143. void preorderFUN(struct node2* p, struct node1** listHead)
  144. {
  145. preorder(p, listHead);
  146. struct node1* t = *listHead;
  147. while (t->next != NULL)
  148. t = t->next;
  149. t->next = *listHead;
  150. putchar('\n');
  151. }
  152.  
  153.  
  154.  
  155.  
  156. int main()
  157. {
  158. struct node1* listHead = NULL;
  159.  
  160. struct node2* root = newNode(2);
  161. root->one = newNode(4);
  162. root->two = newNode(19);
  163. root->one->one = newNode(7);
  164. root->one->two = newNode(10);
  165. root->two->one = newNode(22);
  166. /*
  167. * 2
  168. * / \
  169. * 4 19
  170. * / \ /
  171. * 7 10 22
  172. */
  173. postorderPrint(root);
  174.  
  175. postorderFUN(root, &listHead);
  176. display(&listHead);
  177. listHead = NULL;
  178. preorderFUN(root, &listHead);
  179. display(&listHead);
  180. }
Advertisement
RAW Paste Data Copied
Advertisement