DilyaraL

16.23 конечный

Apr 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7. struct node
  8. {
  9. int info;
  10. node * next;
  11. };
  12. void setNull(node * & p)
  13. {
  14. p = NULL;
  15. }
  16. void createElem(node * & p, int y)
  17. {
  18. p = new node;
  19. p->info = y;
  20. p->next = NULL;
  21. }
  22. void addFirst(node * & head1, node * p)//добавление в начало
  23. {
  24. p->next = head1;
  25. head1 = p;
  26. }
  27. void createListFromFile(node * & head1, ifstream & f)
  28. {
  29. node * q;
  30. int x;
  31. setNull(head1);
  32. while (f >> x)
  33. {
  34. createElem(q, x);
  35. addFirst(head1, q);
  36. }
  37. }
  38. void setHead(node * head1, node * & p)
  39. {
  40. p = head1;
  41. }
  42. bool nodeIsNull(node * p)
  43. {
  44. return p == NULL;
  45. }
  46. void showNode(node * p)
  47. {
  48. cout << p->info << ' ';
  49. }
  50. void moove(node * & p)
  51. {
  52. p = p->next;
  53. }
  54. void showList(node * head)
  55. {
  56. node * q;
  57. setHead(head, q);
  58. while (!nodeIsNull(q))
  59. {
  60. showNode(q);
  61. moove(q);
  62. }
  63. cout << endl;
  64. }
  65.  
  66. bool listIsEmpty(node * head1)
  67. {
  68. return head1 == NULL;
  69. }
  70. bool compInfo(node * p1, node * p2)
  71. {
  72. return p1->info == p2->info;
  73. }
  74.  
  75. bool taskA(node * head1, node * head2)
  76. {
  77. node * q1, *q2;
  78. setHead(head1, q1);
  79. setHead(head2, q2);
  80. bool t = true;
  81. while (!nodeIsNull(q1) && !nodeIsNull(q2) && t)
  82. {
  83. if (!compInfo(q1, q2))
  84. t = false;
  85. moove(q1);
  86. moove(q2);
  87. }
  88. return t && nodeIsNull(q1) && nodeIsNull(q2);
  89. }
  90.  
  91. bool taskB(node * head)
  92. {
  93. node * q,*p;
  94. setHead(head, q);
  95. bool t = false;
  96. if (!listIsEmpty(head))
  97. if (!listIsEmpty(head->next))//один элемент
  98. while (!listIsEmpty(q->next) && !t)
  99. {
  100. p = q->next;
  101. while (!listIsEmpty(p) && !t)
  102. {
  103. if (q->info == p->info)
  104. t = true;
  105. moove(p);
  106. }
  107. moove(q);
  108. }
  109. return t;
  110. }
  111.  
  112. void First_in_End(node * &head)
  113. {
  114. node * q, *p;
  115. setHead(head, q);
  116.  
  117. if (!listIsEmpty(q->next))
  118. {
  119. moove(head);
  120. setHead(head, p);
  121. while (!listIsEmpty(p->next))//походим до последнего
  122. moove(p);
  123. p->next = q;
  124. setNull(q->next);
  125. }
  126. }
  127.  
  128. void Last_in_begin(node * &head)
  129. {
  130. node * q, *p;
  131. setHead(head, q);
  132. setHead(head, p);
  133. if (!listIsEmpty(q->next))
  134. {
  135. while (!listIsEmpty(q->next->next))//походим до предпоследнего
  136. moove(q);
  137. /*p = q->next;//
  138. setNull(q->next);
  139. p->next = head;
  140. head = p;*/
  141. head = q->next;
  142. head->next = p;
  143. setNull(q->next);
  144.  
  145.  
  146. }
  147. }
  148.  
  149. bool vhodit_splosh_neuporyad(node * head1, node *head2)
  150. {
  151. node * q1, *q2, *temp = new node;
  152. setHead(head1, q1);
  153. setHead(head2, q2);
  154.  
  155. bool t, f = false;
  156. while (!nodeIsNull(q1->next) && !f)
  157. {
  158. temp = q1;
  159. t = true;
  160. while (!nodeIsNull(q1) && !nodeIsNull(q2) && t)
  161. {
  162. if (!compInfo(q1, q2))
  163. t = false;
  164. else
  165. {
  166. move(q1);
  167. move(q2);
  168. }
  169. }
  170. if (t&& nodeIsNull(q2))
  171. f = true;
  172. else {
  173. q1 = temp;
  174. move(q1);
  175. setHead(head2, q2);
  176. }
  177. }
  178. return f;
  179. }
  180.  
  181. bool taskb2(node * &head1, node * &head2)
  182. {
  183. bool f = true, t;
  184. node * q1, *q2;
  185. setHead(head1, q1);
  186. setHead(head2, q2);
  187. while (!nodeIsNull(q2) &&f)
  188. {
  189. t = false;
  190. while (!nodeIsNull(q1) && !nodeIsNull(q2) && !t)//цикл для одного элмента
  191. {
  192. if (!compInfo(q1, q2))
  193. {
  194. moove(q1);
  195. }
  196. else
  197. {
  198. t = true;
  199. moove(q1);
  200. moove(q2);
  201. }
  202. }
  203. if (!t&&nodeIsNull(q1))
  204. f = false;
  205. }
  206. return f;
  207. }
  208.  
  209. node* copy(node * head1)//но наоборот записывает
  210. {
  211. node* head2, *p, *q1;
  212. setHead(head1, q1);
  213. setNull(head2);
  214. setHead(head2, p);
  215. while (!nodeIsNull(q1))
  216. {
  217. createElem(p, q1->info);
  218. addFirst(head2, p);
  219. moove(q1);
  220. }
  221. return head2;
  222. }
  223.  
  224. void Del(node * &q, node * &temp)
  225. {
  226. temp->next = q->next;
  227. }
  228.  
  229. bool taskb3(node * &head1, node * head2)
  230. {
  231. bool f = true, t;
  232. node * q1, *q2,*q3, *temp;
  233. setHead(head1, q1);
  234. setHead(head2, q2);
  235. while (!nodeIsNull(q1) && f)
  236. {
  237. setHead(head2, q3);
  238. t = false;
  239. while (!nodeIsNull(q3) && !nodeIsNull(q1) && !t)//цикл для одного элмента
  240. {
  241. if (!compInfo(q3, q1))
  242. {
  243. temp = q3;
  244. moove(q3);
  245. }
  246. else
  247. {
  248. if(q3!=head2&!nodeIsNull(q3))
  249. Del(q3, temp);
  250. else
  251. {
  252. if (q3 == head2)
  253. head2 = q3->next;
  254.  
  255. }
  256. t = true;
  257. moove(q1);
  258. }
  259. }
  260. if (!t&&nodeIsNull(q3))
  261. f = false;
  262.  
  263. }
  264. return f;
  265.  
  266. }
  267.  
  268. void E(node * &head1, node * &head2)
  269. {
  270. if (nodeIsNull(head1))
  271. head1 = head2;
  272. else
  273. {
  274. node * q1, *q2;
  275. setHead(head1, q1);
  276. setHead(head2, q2);
  277. while (!nodeIsNull(q1->next))
  278. moove(q1);
  279. q1->next = head2;
  280. }
  281.  
  282. }
  283.  
  284. void Zh(node * &head1, node * &head2, int x)
  285. {
  286. if (!nodeIsNull(head1))
  287. {
  288. node * q1, *q2,*p;
  289. setHead(head1, q1);
  290. setHead(head2, q2);
  291. while (!nodeIsNull(q1)&&q1->info!=x)
  292. moove(q1);
  293. if (!nodeIsNull(q1))
  294. {
  295. while (!nodeIsNull(q2->next))
  296. moove(q2);
  297. p = q1->next;
  298.  
  299. q1->next = head2;
  300. q2->next = p;
  301. }
  302. }
  303. }
  304.  
  305. void Z(node * &head1)
  306. {
  307. node *q1, *q2,*q3,*temp=NULL;
  308. setHead(head1, q1);
  309. if (!nodeIsNull(head1))
  310. if (!nodeIsNull(head1->next))//один элемент
  311. {
  312. q2 = q1->next;
  313. q3 = q2->next;
  314. while (!nodeIsNull(q3))
  315. {
  316. q1->next = temp;
  317. q2->next = q1;
  318. temp = q1;
  319. q1 = q2;
  320. q2 = q3;
  321. moove(q3);
  322. }
  323. q1->next = temp;
  324. q2->next = q1;
  325. head1= q2;
  326. }
  327. }
  328.  
  329. void I(node* head1)
  330. {
  331. node * q1, *q2;
  332. setHead(head1, q1);
  333. if(!nodeIsNull(head1))
  334. if (!nodeIsNull(head1->next))
  335. {
  336. while (!nodeIsNull(q1->next))
  337. {
  338. if (compInfo(q1, q1->next))
  339. Del(q1->next, q1);
  340. else
  341. moove(q1);
  342. }
  343. }
  344.  
  345. }
  346.  
  347. void K(node* head1)
  348. {
  349. node * q1, *q2, *temp;
  350. setHead(head1, q1);
  351. if (!nodeIsNull(head1))
  352. if (!nodeIsNull(head1->next))
  353. {
  354. while (!nodeIsNull(q1->next->next))///
  355. {
  356. q2 = q1->next;
  357. temp = q1;
  358. while (!nodeIsNull(q2->next))///
  359. {
  360. if (compInfo(q1, q2))
  361. {
  362.  
  363. Del(q2, temp);
  364. q2 = temp->next;
  365. }
  366. else
  367. {
  368. temp = q2;
  369. moove(q2);
  370. }
  371.  
  372. }
  373. if (compInfo(q1, q2))
  374. temp->next = NULL;
  375. moove(q1);
  376. if (q1->next == NULL)
  377. break;
  378. }
  379. }
  380. }
  381. int main()
  382. {
  383. ifstream f("Text.txt");
  384. node * head1;
  385. createListFromFile(head1, f);
  386. showList(head1);
  387.  
  388. ifstream g("Text1.txt");
  389. node * head2;
  390. createListFromFile(head2, g);
  391. showList(head2);
  392.  
  393. /*if (!listIsEmpty(head1) && !listIsEmpty(head2))
  394. cout << taskA(head1, head2) << endl;
  395. else cout << "list Is Empty" << endl;*/
  396.  
  397. /*if (!listIsEmpty(head1))
  398. cout << taskB(head1) << endl;
  399. else cout << "list Is Empty" << endl;*/
  400.  
  401. /*First_in_End(head1);
  402. showList(head1);*/
  403.  
  404. /*Last_in_begin(head1);
  405. showList(head1);*/
  406.  
  407. //cout << taskb2(head1, head2) << endl;
  408.  
  409. /*E(head1, head2);
  410. showList(head1);*/
  411.  
  412. /*Zh(head1, head2, 8);
  413. showList(head1);*/
  414.  
  415. cout << taskb3(head1, head2) << endl;
  416.  
  417. /*I(head1);
  418. showList(head1);*/
  419.  
  420. //K(head1);
  421.  
  422. //Z(head1);
  423. //showList(head1);
  424.  
  425. system("pause");
  426. return 0;
  427. }
Advertisement
Add Comment
Please, Sign In to add comment