DilyaraL

16.25

Apr 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. struct node
  7. {
  8. int info;
  9. node * next;
  10. };
  11. void setNull(node * & p)
  12. {
  13. p = NULL;
  14. }
  15. void createElem(node * & p, int y)
  16. {
  17. p = new node;
  18. p->info = y;
  19. p->next = NULL;
  20. }
  21. void addFirst(node * & head1, node * p)//добавление в начало
  22. {
  23. p->next = head1;
  24. head1 = p;
  25. }
  26. void createListFromFile(node * & head1, ifstream & f)
  27. {
  28. node * q;
  29. int x;
  30. setNull(head1);
  31. while (f >> x)
  32. {
  33. createElem(q, x);
  34. addFirst(head1, q);
  35. }
  36. }
  37. void setHead(node * head1, node * & p)
  38. {
  39. p = head1;
  40. }
  41. bool nodeIsNull(node * p)
  42. {
  43. return p == NULL;
  44. }
  45. void showNode(node * p)
  46. {
  47. cout << p->info << ' ';
  48. }
  49. void moove(node * & p)
  50. {
  51. p = p->next;
  52. }
  53. void showList(node * head)
  54. {
  55. node * q;
  56. setHead(head, q);
  57. while (!nodeIsNull(q))
  58. {
  59. showNode(q);
  60. moove(q);
  61. }
  62. cout << endl;
  63. }
  64.  
  65. bool compInfo(node * p1, node * p2)
  66. {
  67. return p1->info == p2->info;
  68. }
  69. void Del(node * &q, node * &temp)
  70. {
  71. temp->next = q->next;
  72. }
  73.  
  74. void K(node* &head1)
  75. {
  76. node * q1, *q2, *temp;
  77. setHead(head1, q1);
  78. if (!nodeIsNull(head1))
  79. if (!nodeIsNull(head1->next))
  80. {
  81. while (!nodeIsNull(q1->next))
  82. {
  83. q2 = q1->next;
  84. temp = q1;
  85. while (!nodeIsNull(q2))
  86. {
  87. if (compInfo(q1, q2))
  88. {
  89.  
  90. Del(q2, temp);
  91. q2 = temp->next;
  92. }
  93. else
  94. {
  95. temp = q2;
  96. moove(q2);
  97. }
  98.  
  99. }
  100. if (!nodeIsNull(q1->next))
  101. moove(q1);
  102. }
  103. }
  104. }
  105.  
  106. node* taskA(node * head1, node * head2)
  107. {
  108. node * q1,* head3,*q3;
  109. setNull(head3);
  110. setHead(head1, q1);
  111. if (!nodeIsNull(q1))
  112. {
  113. while (!nodeIsNull(q1->next))
  114. {
  115. createElem(q3, q1->info);
  116. addFirst(head3, q3);
  117. moove(q1);
  118. }
  119. }
  120. setHead(head2, q1);
  121. while (!nodeIsNull(q1))
  122. {
  123. createElem(q3, q1->info);
  124. addFirst(head3, q3);
  125. moove(q1);
  126. }
  127. K(head3);
  128. return head3;
  129. }
  130.  
  131. node* taskB(node * head1, node * head2)
  132. {
  133. node * q1, *q2,*p, *head3=new node;
  134. K(head1);
  135. K(head2);
  136. setHead(head1, q1);
  137. setNull(head3);
  138. while (!nodeIsNull(q1))
  139. {
  140. setHead(head2, q2);
  141. while (!nodeIsNull(q2) && !compInfo(q1, q2))
  142. moove(q2);
  143. if (!nodeIsNull(q2))
  144. {
  145. createElem(p, q2->info);
  146. addFirst(head3, p);
  147. }
  148. moove(q1);
  149. }
  150. return head3;
  151. }
  152.  
  153. node* taskC(node * head1, node * head2)
  154. {
  155. node * q1, *q2, *p, *head3 = new node;
  156. K(head1);
  157. K(head2);
  158. setHead(head1, q1);
  159. setNull(head3);
  160. while (!nodeIsNull(q1))
  161. {
  162. setHead(head2, q2);
  163. while (!nodeIsNull(q2) && !compInfo(q1, q2))
  164. moove(q2);
  165. if (nodeIsNull(q2))
  166. {
  167. createElem(p, q1->info);
  168. addFirst(head3, p);
  169. }
  170. moove(q1);
  171. }
  172. return head3;
  173. }
  174.  
  175. node* taskD(node * head1, node * head2)
  176. {
  177. node * q1, *q2, *p, *head3 = new node;
  178. K(head1);
  179. K(head2);
  180. setHead(head1, q1);
  181. setNull(head3);
  182. while (!nodeIsNull(q1))
  183. {
  184. setHead(head2, q2);
  185. while (!nodeIsNull(q2) && !compInfo(q1, q2))
  186. moove(q2);
  187. if (nodeIsNull(q2))
  188. {
  189. createElem(p, q1->info);
  190. addFirst(head3, p);
  191. }
  192. moove(q1);
  193. }
  194. setHead(head2, q2);
  195. while (!nodeIsNull(q2))
  196. {
  197. setHead(head1, q1);
  198. while (!nodeIsNull(q1) && !compInfo(q1, q2))
  199. moove(q1);
  200. if (nodeIsNull(q1))
  201. {
  202. createElem(p, q2->info);
  203. addFirst(head3, p);
  204. }
  205. moove(q2);
  206. }
  207. return head3;
  208. }
  209.  
  210. int main()
  211. {
  212. ifstream f("Text.txt");
  213. node * head1;
  214. createListFromFile(head1, f);
  215. showList(head1);
  216.  
  217. ifstream g("Text1.txt");
  218. node * head2;
  219. createListFromFile(head2, g);
  220. showList(head2);
  221.  
  222. //showList(taskB(head1, head2));
  223. showList(taskD(head1, head2));
  224. system("pause");
  225. return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment