Advertisement
adwas33

Listy Jednostronnie wiązane

May 7th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct Node {
  5. int val;
  6. Node * next;
  7.  
  8. };
  9.  
  10. void add(Node *& H,int value)
  11. {
  12. Node * p= new Node;
  13. p-> val=value;
  14. p->next=H;
  15. H=p;
  16.  
  17. }
  18. void deleteFirst(Node *&H)
  19. {
  20. if(H!= nullptr)
  21. {
  22. Node *p=H;
  23. H=H->next;
  24. delete p;
  25. }
  26. }
  27. void addNodeToEnd(Node *&head,Node *& tail , Node * e )
  28. {
  29. e->next= nullptr;
  30. if(head== nullptr)
  31. {
  32. head=e;
  33. tail=e;
  34. } else
  35. {
  36. tail->next=e;
  37. tail=e;
  38. }
  39.  
  40. }
  41. void swap(Node *& h)
  42. {
  43. if(h!= nullptr&&h->next!= nullptr)
  44. {
  45. Node *p=h;
  46. h=p->next;
  47. p->next=h->next;
  48. h->next=p;
  49. }
  50. }
  51. void show_list_rek(Node * H)
  52. {
  53. if(H!= nullptr)
  54. {
  55. cout<<H->val<<"->";
  56. if(H->next!= nullptr)
  57. show_list_rek(H->next);
  58. else cout<<"nullptr";
  59. }
  60. }
  61. void swapSecond(Node *&head)
  62. {
  63. if(head&&head->next)
  64. {
  65. swap(head);
  66. Node * p=head->next;
  67. while(p->next&&p->next->next)
  68. {
  69. swap(p->next);
  70. p=p->next->next;
  71. }
  72. }
  73. }
  74. void deleteEven(Node *&H)
  75. {
  76. Node * head= nullptr;
  77. Node *p= H;
  78. int i=0;
  79. while(p!= nullptr)
  80. {
  81. if(i%2==0)
  82. add(head,p->val);
  83. i++;
  84. p= p->next;
  85. }
  86. H=head;
  87. }
  88.  
  89. void multipleByTwo(Node *&h)
  90. {
  91. if(h!= nullptr)
  92. {
  93. Node * head= nullptr;
  94. Node *p= h;
  95. while(p!= nullptr)
  96. {
  97.  
  98. add(head,p->val);
  99. add(head,p->val);
  100.  
  101. p= p->next;
  102. }
  103. show_list_rek(head);
  104.  
  105. }
  106. }
  107. void multipleByValueNumber(Node *&h)
  108. {
  109. if(h!= nullptr)
  110. {
  111. Node * head= nullptr;
  112. Node *p= h;
  113. while(p!= nullptr)
  114. {
  115. for(int i =0;i<p->val;i++)
  116. {
  117. add(head,p->val);
  118. }
  119. p= p->next;
  120. }
  121. // show_list_rek(head);
  122. h=head;
  123. }
  124. }
  125. void swapFirstWithSecond(Node* & head)
  126. {
  127. if(head!= nullptr && head-> next != nullptr)
  128. {
  129. Node * forgotten=head;
  130. head=forgotten->next;
  131. forgotten->next=head->next;
  132. head->next=forgotten;
  133. }
  134. }
  135. void swapFirstWithLast(Node * & head)
  136. {
  137. if(head!= nullptr)
  138. {
  139. Node * iterator=head;
  140. Node * headCopy=head;
  141. while(iterator!= nullptr)
  142. iterator=iterator->next;
  143. iterator->next=head->next;
  144. head->next=iterator;
  145.  
  146. }
  147.  
  148.  
  149. }
  150. void swap2(Node *&H,Node *&replaced)
  151. {
  152. if(H!= nullptr&&replaced!= nullptr)
  153. {
  154. Node *kopia_Glowy=H;
  155. Node *kopia_nastepnika_Glowy=kopia_Glowy->next;
  156. kopia_Glowy=replaced;
  157. replaced->next=kopia_nastepnika_Glowy;
  158.  
  159. }
  160. }
  161.  
  162. //void swap(Node *&H)
  163. //{
  164. // if(H!= nullptr&&H->next!= nullptr)
  165. // {
  166. // Node *p=H;
  167. // H=p->next;
  168. // p->next=H->next;
  169. // H->next=p;
  170. // }
  171. //}
  172. void zamianaPierwszegoElementuZOstatnimNaLiscie(Node *&head)
  173. {
  174. if(head!= nullptr)
  175. {
  176. Node *iterator=head;
  177. while(iterator->next!= nullptr)
  178. {
  179. cout<<iterator->val<<" ";
  180. iterator=iterator->next;
  181.  
  182. }
  183. swap2(head,iterator->next);
  184.  
  185. }
  186.  
  187. }
  188.  
  189.  
  190. void replaceXWithHisNext ( Node *& head ,int x)
  191. {
  192. if(head!= nullptr&&head->next!= nullptr)
  193. {
  194. Node * n=head;
  195. if(head->val==x)
  196. {
  197. swap(head);
  198. }else
  199. {
  200. Node *p=head;
  201. while(p->next!= nullptr&&p->next->val!=x)
  202. {
  203. p=p->next;
  204. }
  205. if(p->next!= nullptr&&p->next->next!= nullptr)
  206. {
  207. Node *e=p->next;
  208. p->next=e->next;
  209. e->next=e->next->next;
  210. p->next->next=e;
  211. }
  212. }
  213. }
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220. void replaceXWithHisPervious ( Node *& head ,int x)
  221. {
  222. if(head!= nullptr&&head->next!= nullptr)
  223. {
  224. Node * n=head;
  225. while(n->next!= nullptr)
  226. {
  227. if(n->next->val==x) {
  228. cout<<"\nZnalazlem"<<endl;
  229. Node * forgotten=n;
  230. n=forgotten->next;
  231. forgotten->next=n->next;
  232. n->next=forgotten;
  233. cout<<forgotten->val<<endl<<n->val<<endl;
  234. break;
  235. }
  236. else n=n->next;
  237. }
  238. }
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. void nextTryReplace(Node *&head,int x)
  246. {
  247. if(head!= nullptr)
  248. {
  249. Node * p=head;
  250. if(head->next== nullptr)
  251. {
  252. return;
  253. }
  254. if(head->val==x)
  255. {
  256. return;
  257. }else while(p->next!= nullptr&&p->next->val!=x)
  258. {
  259. p=p->next;
  260. }
  261. if(p->next== nullptr)
  262. {
  263. return;
  264. } else
  265. {
  266. Node* copy= p;
  267. p->next;
  268. p=p->next;
  269.  
  270.  
  271. }
  272.  
  273.  
  274. }
  275.  
  276.  
  277. }
  278. //KUSTRA
  279. void funkcja1(Node *&H)
  280. {
  281.  
  282. }
  283.  
  284.  
  285.  
  286.  
  287. void copy(Node *h)
  288. {
  289. if(h)
  290. {
  291. Node *h2= nullptr;
  292. Node *p=h;
  293. while(p->next!= nullptr)
  294. {
  295. add(h2,p->val);
  296. p=p->next;
  297. }
  298. add(h2,p->val);
  299. p->next=h2;
  300. h2= nullptr;
  301. }
  302.  
  303. }
  304. // zamienię miejscami całą tablicę
  305. void reverse(Node *&h)
  306. {
  307. if(h!= nullptr)
  308. {
  309. Node* p=h;
  310. Node *h2= nullptr;
  311. while(p!= nullptr)
  312. {
  313. add(h2,p->val);
  314. p=p->next;
  315. }
  316. h=h2;
  317.  
  318. }
  319. }
  320.  
  321.  
  322. //podziele na pół
  323. void split(Node *&h)
  324. {
  325. Node * p=h;
  326. Node * copy= nullptr;
  327. int i=0;
  328. while(p) // policz liczbę elementów
  329. {
  330. p=p->next;
  331. i++;
  332. }
  333. i/=2;
  334. while(i!=0)
  335. {
  336. i--;
  337. add(copy,h->val);
  338. h=h->next;
  339. }
  340. h=copy;
  341.  
  342. }
  343. void split(Node *&h,Node *& first,Node *&second)
  344. {
  345. Node * p=h;
  346. Node * iterator=h;
  347. first= nullptr;
  348. second = nullptr;
  349. int i=0;
  350. while(p!= nullptr) // policz liczbę elementów
  351. {
  352. p=p->next;
  353. i++;
  354. }
  355. i/=2;
  356. while(i!=0)
  357. {
  358. i--;
  359. add(first,iterator->val);
  360. iterator=iterator->next;
  361. }
  362. while(iterator!= nullptr)
  363. {
  364. add(second,iterator->val);
  365. iterator=iterator->next;
  366. }
  367.  
  368. }
  369.  
  370. void merge (Node *& head,Node *& first,Node *&second)
  371. {
  372. //head= nullptr;
  373. //first = wartości
  374. //second = wartości
  375. //1) Głowy NIE RUSZAMY DO Glownej tablicy
  376. head= nullptr;
  377. Node * supporter1=first;
  378. Node * supporter2=second;
  379. while(supporter1||supporter2)
  380. {
  381. if(supporter1&&supporter2== nullptr)
  382. {
  383. add(head,supporter1->val);
  384. cout<<"pusty drugi"<<supporter1->val<<" ";
  385. supporter1=supporter1->next;
  386. }else
  387. if(supporter2&&supporter1== nullptr)
  388. {
  389. add(head,supporter2->val);
  390. cout<<"pusty pierwszy"<<supporter2->val<<" ";
  391. supporter2=supporter2->next;
  392. }else
  393. if(supporter1->val>=supporter2->val)
  394. {
  395. add(head,supporter1->val);
  396. cout<<"Pierwszy warunek pelny"<<supporter1->val<<" ";
  397. supporter1=supporter1->next;
  398. } else
  399. {
  400. add(head,supporter2->val);
  401. cout<<"warunek else"<<supporter2->val<<" ";
  402. supporter2=supporter2->next;
  403. }
  404. }
  405. }
  406.  
  407. void bubleSort(Node *&head)//////////// nwm czy DB
  408. {
  409. if(head)
  410. {
  411. Node * p=head;
  412. Node * e= nullptr;
  413.  
  414. int i=0;
  415. bool czyzachodzizamiana= true;
  416. while(czyzachodzizamiana)
  417. {
  418. if(e==head) return;
  419. if(p==e)
  420. {
  421. p=head;
  422. continue;
  423. }
  424. if(p->val>p->next->val)
  425. {
  426. swap(p);
  427. e=p;
  428.  
  429. }
  430. p=p->next;
  431.  
  432. }
  433. }
  434.  
  435. }
  436.  
  437. //void swapNodes(Node*& head_ref,int numberOfPlacesAhead, int x, int y)
  438. //{
  439. //
  440. //
  441. // // Search for x (keep track of prevX and CurrX
  442. // Node *prevX = NULL, *currX = head_ref;
  443. //
  444. //
  445. // // Search for y (keep track of prevY and CurrY
  446. // Node *prevY = NULL, *currY = head_ref;
  447. // while (currY && numberOfPlacesAhead!=0) {
  448. // prevY = currY;
  449. // currY = currY->next;
  450. // numberOfPlacesAhead--;
  451. // }
  452. //
  453. // // If either x or y is not present, nothing to do
  454. // if (currX == NULL || currY == NULL)
  455. // return;
  456. //
  457. // // If x is not head of linked list
  458. // if (prevX != NULL)
  459. // prevX->next = currY;
  460. // else // Else make y as new head
  461. // head_ref = currY;
  462. //
  463. // // If y is not head of linked list
  464. // if (prevY != NULL)
  465. // prevY->next = currX;
  466. // else // Else make x as new head
  467. // head_ref = currX;
  468. //
  469. // // Swap next pointers
  470. // Node* temp = currY->next;
  471. // currY->next = currX->next;
  472. // currX->next = temp;
  473. //}
  474.  
  475. void sortowanieGrzebieniowe(Node *&head)
  476. {
  477. if(head)
  478. {
  479. if(head->next== nullptr)///jednoelementowa tablica
  480. {
  481. return;
  482. } else
  483. {
  484. int licznik=0;
  485. Node * p=head;
  486. while(p) // liczę ilość elementów w tablicy
  487. {
  488. licznik++;
  489. p=p->next;
  490. }
  491. int gap = licznik/1.3;
  492.  
  493. add(head,0);//fake element
  494. for(;gap!=1;gap=gap/1.3)
  495. {
  496. int gapCopy=gap-1;
  497. Node* iterator=head;//szukamy poprzednika tej listy
  498. Node* iterator2=head;
  499. while(iterator&&gapCopy!=0)
  500. {
  501. iterator=iterator->next;
  502. gapCopy--;
  503. }
  504.  
  505. while(iterator!= nullptr)
  506. {
  507.  
  508. if(iterator->val>iterator2->val)
  509. {
  510. iterator->next=iterator2->next;
  511. iterator2->next=iterator;
  512. }
  513. iterator=iterator->next;
  514. iterator2=iterator2->next;
  515. }
  516.  
  517. if(gap==1)
  518. {
  519. //bublesort
  520. return;
  521. }
  522.  
  523.  
  524. }
  525.  
  526. }
  527. }
  528.  
  529. }
  530.  
  531. int main() {
  532.  
  533. Node * h= nullptr;
  534. Node * first= nullptr;
  535. Node * second= nullptr;
  536. add(h,3);
  537. sortowanieGrzebieniowe(h);
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544. return 0;
  545. }
  546.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement