Advertisement
adwas33

Templatka na kolokwium nr 2 ( i moja postawa haker vs haker ) oraz kolokwium kustry po ///

May 7th, 2022
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <utility>
  4.  
  5. #include <algorithm>
  6.  
  7. #include <random>
  8.  
  9. #include <chrono>
  10.  
  11. #include <ctime>
  12.  
  13. using namespace std;
  14. ///Punktacja od kustry za zadania pierwsze - 7/10 pkt bo nie skończyłem
  15. ///Drugie 9/10 (bo się gdzieś na nullu wywala)
  16. ////////////////////////Użytkowe//////////////////////////////////
  17.  
  18.  
  19. template < typename T >
  20. ostream & operator << (ostream & os,
  21.                        const vector < T > & vec) {
  22.     for (const auto & item: vec) {
  23.         os << item << " ";
  24.     }
  25.     os << endl;
  26.     return os;
  27. }
  28.  
  29. template < typename T >
  30. T * copy(T array[], long length) {
  31.     auto resoult = new T[length];
  32.     for (long i = 0; i < length; i++) {
  33.         resoult[i] = array[i];
  34.     }
  35.  
  36.     return resoult;
  37. }
  38. template < typename T >
  39. vector < T > copy(vector < T > array) {
  40.     vector < T > resoult;
  41.  
  42.     return resoult.copy(array);
  43.  
  44. }
  45.  
  46. template < typename T >
  47. T * generate(long length) {
  48.     default_random_engine engine;
  49.     uniform_int_distribution < T > distribution(-10000, 10000);
  50.     auto resoult = new T[length];
  51.     for (long i = 0; i < length; i++) {
  52.         resoult[i] = distribution(engine);
  53.     }
  54.  
  55.     return resoult;
  56. }
  57. template < typename T >
  58. vector < T > generate_vec(long length) {
  59.     default_random_engine engine;
  60.     uniform_int_distribution < T > distribution(-10000, 10000);
  61.     auto resoult = vector < T > (length);
  62.     for (long i = 0; i < length; i++) {
  63.         resoult[i] = distribution(engine);
  64.     }
  65.  
  66.     return resoult;
  67. }
  68.  
  69. template < typename T >
  70. void sout(T arr, long length) {
  71.     for (long l = 0; l < length - 1; l++) {
  72.         cout << arr[l] << " ";
  73.     }
  74.     cout << endl;
  75.  
  76. }
  77.  
  78. template < typename T >
  79. void sout(vector < T > arr) {
  80.     cout << arr;
  81. }
  82. ////////////////////////////////////////////////SORTOWANIA////////////////////////////////
  83. template < typename T >
  84. long partition(T * A, long l, long r) {
  85.     T x = A[l];
  86.     long i = l, j = r + 1;
  87.  
  88.     for (;;) {
  89.         do ++i; while (i <= r && A[i] < x);
  90.         do --j; while (A[j] > x);
  91.         if (i > j) {
  92.             break;
  93.         }
  94.         swap(A[i], A[j]);
  95.     }
  96.     A[l] = A[j];
  97.     A[j] = x;
  98.     return j;
  99. }
  100. template < typename T >
  101. long partition(vector < T > A, long l, long r) {
  102.     T x = A[l];
  103.     long i = l, j = r + 1;
  104.  
  105.     for (;;) {
  106.         do ++i; while (i <= r && A[i] < x);
  107.         do --j; while (A[j] > x);
  108.         if (i > j) {
  109.             break;
  110.         }
  111.         swap(A[i], A[j]);
  112.     }
  113.     A[l] = A[j];
  114.     A[j] = x;
  115.     return j;
  116. }
  117.  
  118. template < typename T >
  119. long partition2(T * A, long l, long r) {
  120.     T x = A[r];
  121.     long i = l - 1;
  122.     for (; l < r; ++l) {
  123.         if (A[l] <= x)
  124.             swap(A[++i], A[l]);
  125.     }
  126.     swap(A[i + 1], A[r]);
  127.     return i + 1;
  128. }
  129.  
  130. template < typename T >
  131. long partition2(vector < T > A, long l, long r) {
  132.     T x = A[r];
  133.     long i = l - 1;
  134.     for (; l < r; ++l) {
  135.         if (A[l] <= x)
  136.             swap(A[++i], A[l]);
  137.     }
  138.     swap(A[i + 1], A[r]);
  139.     return i + 1;
  140. }
  141. template < typename T >
  142. long partition3(T* tab, long l, long r)
  143. {
  144.     if (r - l == 1)
  145.     {
  146.         if (tab[r] < tab[l])
  147.         {
  148.             swap(tab[r], tab[l]);
  149.         }
  150.         return l;
  151.     }
  152.     swap(tab[l + 1], tab[(l + r) >> 1]);
  153.  
  154.     if (tab[l + 1] > tab[r])
  155.     {
  156.         swap(tab[l + 1], tab[r]);
  157.     }
  158.  
  159.     if (tab[l] > tab[r])
  160.     {
  161.         swap(tab[l], tab[r]);
  162.     }
  163.  
  164.     if (tab[l + 1] > tab[l])
  165.     {
  166.         swap(tab[l + 1], tab[l]);
  167.     }
  168.  
  169.     long x = tab[l];
  170.     long i = l + 1, j = r;
  171.     for (;;)
  172.     {
  173.         do ++i; while (tab[i] < x);
  174.         do --j; while (tab[j] > x);
  175.         if (i > j)
  176.             break;
  177.         swap(tab[i], tab[j]);
  178.     }
  179.     tab[l] = tab[j];
  180.     tab[j] = x;
  181.     return j;
  182. }
  183. template < typename T >
  184. long partition3(vector<T> tab, long l, long r)
  185. {
  186.     if (r - l == 1)
  187.     {
  188.         if (tab[r] < tab[l])
  189.         {
  190.             swap(tab[r], tab[l]);
  191.         }
  192.         return l;
  193.     }
  194.     swap(tab[l + 1], tab[(l + r) >> 1]);
  195.  
  196.     if (tab[l + 1] > tab[r])
  197.     {
  198.         swap(tab[l + 1], tab[r]);
  199.     }
  200.  
  201.     if (tab[l] > tab[r])
  202.     {
  203.         swap(tab[l], tab[r]);
  204.     }
  205.  
  206.     if (tab[l + 1] > tab[l])
  207.     {
  208.         swap(tab[l + 1], tab[l]);
  209.     }
  210.  
  211.     long x = tab[l];
  212.     long i = l + 1, j = r;
  213.     for (;;)
  214.     {
  215.         do ++i; while (tab[i] < x);
  216.         do --j; while (tab[j] > x);
  217.         if (i > j)
  218.             break;
  219.         swap(tab[i], tab[j]);
  220.     }
  221.     tab[l] = tab[j];
  222.     tab[j] = x;
  223.     return j;
  224. }
  225. template < typename T >
  226. void quickSort(T arr[], long poczatek, long koniec) {
  227.     if (poczatek < koniec) {
  228.         long index = partition(arr, poczatek, koniec);
  229.         quickSort(arr, poczatek, index - 1);
  230.         quickSort(arr, index + 1, koniec);
  231.     }
  232. }
  233. template < typename T >
  234. void quickSort(vector < T > arr, long poczatek, long koniec) {
  235.     if (poczatek < koniec) {
  236.         long index = partition(arr, poczatek, koniec);
  237.         quickSort(arr, poczatek, index - 1);
  238.         quickSort(arr, index + 1, koniec);
  239.     }
  240. }
  241.  
  242. template < typename T >
  243. void hybridQuickSort(T arr[], long poczatek, long koniec) {
  244.     if (poczatek - koniec <= 10) {
  245.  
  246.         bubbleSort(arr, poczatek, koniec);
  247.     } else
  248.     if (poczatek < koniec) {
  249.         long index = partition(arr, poczatek, koniec);
  250.         hybridQuickSort(arr, poczatek, index - 1);
  251.         hybridQuickSort(arr, index + 1, koniec);
  252.     }
  253. }
  254. template < typename T >
  255. void hybridQuickSort(vector < T > arr, long poczatek, long koniec) {
  256.     if (poczatek - koniec <= 10) {
  257.  
  258.         bubbleSort(arr, poczatek, koniec);
  259.     } else
  260.     if (poczatek < koniec) {
  261.         long index = partition(arr, poczatek, koniec);
  262.         hybridQuickSort(arr, poczatek, index - 1);
  263.         hybridQuickSort(arr, index + 1, koniec);
  264.     }
  265. }
  266.  
  267. template < typename T >
  268. void bubbleSort(T arr[], long poczatek, long koniec) {
  269.     int i, j, n = koniec;
  270.     for (i = poczatek; i < n; i++)
  271.         for (j = poczatek; j < n - i; j++)
  272.             if (arr[j] > arr[j + 1])
  273.                 swap(arr[j], arr[j + 1]);
  274. }
  275. template < typename T >
  276. void bubbleSort(vector < T > arr, long poczatek, long koniec) {
  277.     int i, j, n = koniec;
  278.     for (i = poczatek; i < n; i++)
  279.         for (j = poczatek; j < n - i; j++)
  280.             if (arr[j] > arr[j + 1])
  281.                 swap(arr[j], arr[j + 1]);
  282. }
  283.  
  284. ///////////////////// DRZEWA BST //////////////////////////////
  285.  
  286.  
  287. struct drzewoBST {
  288.     friend ostream &operator<<(ostream &os,  drzewoBST *&bst) {
  289.         if (bst != nullptr) {
  290.             os<<(bst -> left);
  291.             os << bst -> val << " ";
  292.             os<<(bst -> right);
  293.         } else
  294.             return os;
  295.     }
  296.  
  297.     drzewoBST * up;
  298.     drzewoBST * left;
  299.     drzewoBST * right;
  300.     int val;
  301.     ///Wypisanie w porządku INORDER
  302.  
  303. };
  304.  
  305. ///SPRAWDZONA
  306. void addBstNode(drzewoBST * & root, int x, drzewoBST * lastRoot) {
  307.     if (root == nullptr) {
  308.         auto * w = new drzewoBST();
  309.         w -> left = nullptr;
  310.         w -> right = nullptr;
  311.         w -> up = lastRoot;
  312.         w -> val = x;
  313.  
  314.         root = w;
  315.  
  316.     } else {
  317.         x>=root->val ? addBstNode(root -> right, x, root): addBstNode(root -> left, x, root);
  318.     }
  319. }
  320.  
  321. ///SPRAWDZONA
  322. void addBstNode(drzewoBST * & root, int x) {
  323.     if (root == nullptr) {
  324.         auto * w = new drzewoBST();
  325.         w -> left = nullptr;
  326.         w -> right = nullptr;
  327.         w -> val = x;
  328.  
  329.         root = w;
  330.  
  331.     } else {
  332.         x>=root->val ? addBstNode(root -> right, x, root): addBstNode(root -> left, x, root);
  333.  
  334.     }
  335. }
  336.  
  337. ///SPRAWDZONA
  338. drzewoBST * znajdzMin(drzewoBST * root) {
  339.     if (root != nullptr) {
  340.         if (root -> left != nullptr)
  341.             znajdzMin(root -> left);
  342.         else return root;
  343.  
  344.     }
  345.  
  346. }
  347. ///SPRAWDZONA
  348. drzewoBST * znajdzMax(drzewoBST * root) {
  349.     if (root != nullptr) {
  350.         if (root -> right != nullptr)
  351.             znajdzMax(root -> right);
  352.         else return root;
  353.  
  354.     }
  355.  
  356. }
  357. ///SPRAWDZONA
  358. drzewoBST * nastepnik(drzewoBST * wezel) {
  359.     drzewoBST * r;
  360.     if (wezel) {
  361.         if (wezel -> right) return znajdzMin(wezel -> right);
  362.         else {
  363.             r = wezel -> up;
  364.             while (r && (wezel == r -> right)) {
  365.                 wezel = r;
  366.                 r = r -> up;
  367.             }
  368.             return r;
  369.  
  370.         }
  371.     }
  372.     return nullptr;
  373. }
  374. ///SPRAWDZONA
  375. drzewoBST * poprzednik(drzewoBST * wezel) {
  376.     drzewoBST * r;
  377.     if (wezel) {
  378.         if (wezel -> left) return znajdzMax(wezel -> left);
  379.         else {
  380.             r = wezel -> up;
  381.             while (r && (wezel == r -> left)) {
  382.                 wezel = r;
  383.                 r = r -> up;
  384.             }
  385.             return r;
  386.         }
  387.  
  388.     }
  389.     return nullptr;
  390. }
  391. ///SPRAWDZONA
  392. drzewoBST * szukaj(drzewoBST * root, int wartoscSzukana) {
  393.     if (root != nullptr) {
  394.         if (root -> val == wartoscSzukana) {
  395.             return root;
  396.         }
  397.         if (wartoscSzukana > root -> val) {
  398.             szukaj(root -> right, wartoscSzukana);
  399.         } else szukaj(root -> left, wartoscSzukana);
  400.  
  401.         return nullptr;
  402.     }
  403.  
  404. }
  405. //funkcja usuwająca liście tego drzewa
  406. void usunTo(drzewoBST * & root) {
  407.     delete root;
  408.     root = nullptr;
  409. }
  410. ////Sprawdzona
  411. void usunLiscie(drzewoBST * & root) {
  412.     if (root != nullptr) {
  413.         if (root -> left == nullptr && root -> right == nullptr) //gdy nie ma możliwości zejścia w dół
  414.         {
  415.             usunTo(root);
  416.         } else {
  417.             if (root -> left != nullptr) {
  418.                 usunLiscie(root -> left);
  419.             }
  420.             if (root -> right != nullptr) {
  421.                 usunLiscie(root -> right);
  422.             }
  423.         }
  424.  
  425.     }
  426. }
  427. ///SPRAWDZONA
  428. //inorder ( wyświetlanie )
  429. void inorder(drzewoBST * & root) {
  430.     if (root != nullptr) {
  431.         inorder(root -> left);
  432.         cout << root -> val << " ";
  433.         inorder(root -> right);
  434.     } else
  435.         return;
  436. }
  437.  
  438. void rotacjaWLewo(drzewoBST * & root, drzewoBST * & zamieniany) { //////////Nie przetestowana najprawdopodobniej TODO przetestować
  439.     drzewoBST * b, * parent;
  440.     b = zamieniany -> left;
  441.     if (b == nullptr) {
  442.         return;
  443.     }
  444.     parent = zamieniany -> up;
  445.     zamieniany -> left = b -> right;
  446.     if (zamieniany != nullptr) {
  447.         zamieniany -> left -> up = zamieniany;
  448.     }
  449.     b -> right = zamieniany;
  450.     b -> up = parent;
  451.     zamieniany -> up = b;
  452.     if (parent == nullptr) {
  453.         root = b;
  454.         return;
  455.     }
  456.     if (parent -> left == zamieniany) {
  457.         parent -> left = b;
  458.     } else parent -> right = b;
  459.  
  460. }
  461.  
  462. drzewoBST * usunElement(drzewoBST * &root, drzewoBST * x)
  463. {
  464.     drzewoBST * y = x->up, * z;
  465.  
  466.     if((x->left) && (x->right))
  467.     {
  468.         z = (rand() % 2) ? usunElement(root, poprzednik(x)) : usunElement(root, nastepnik(x));
  469.         z->left = x->left;   if(z->left)  z->left->up  = z;
  470.         z->right = x->right; if(z->right) z->right->up = z;
  471.     }
  472.     else z = (x->left) ? x->left : x->right;
  473.  
  474.     if(z) z->up = y;
  475.  
  476.     if(!y) root = z;
  477.     else if(y->left == x) y->left = z; else y->right = z;
  478.  
  479.     return x;
  480. }
  481.  
  482. //////////////////////////LISTY JEDNOSTRONNIE WIĄZANE /////////////////////////////////////////
  483.  
  484. struct Node {
  485.     friend ostream & operator << (ostream & os, Node * node) {
  486.         Node * p = node;
  487.         while (p != nullptr) {
  488.             os << p -> val << "->";
  489.             p = p -> next;
  490.         }
  491.         os << "nullptr";
  492.         return os;
  493.     }
  494.  
  495.     int val;
  496.     Node * next;
  497.  
  498. };
  499. ///SPRAWDZONA
  500. void add(Node * & H, int value) {
  501.     Node * p = new Node;
  502.     p -> val = value;
  503.     p -> next = H;
  504.     H = p;
  505.  
  506. }
  507. ///SPRAWDZONA
  508. void deleteFirst(Node * & H) {
  509.     if (H != nullptr) {
  510.         Node * p = H;
  511.         H = H -> next;
  512.         delete p;
  513.     }
  514. }
  515. ///SPRAWDZONA
  516. void addNodeToEnd(Node * & head, Node * & tail, Node * e) {
  517.     e -> next = nullptr;
  518.     if (head == nullptr) {
  519.         head = e;
  520.         tail = e;
  521.     } else {
  522.         tail -> next = e;
  523.         tail = e;
  524.     }
  525.  
  526. }
  527. ///SPRAWDZONA
  528. void swap(Node * & h) {
  529.     if (h != nullptr && h -> next != nullptr) {
  530.         Node * p = h;
  531.         h = p -> next;
  532.         p -> next = h -> next;
  533.         h -> next = p;
  534.     }
  535. }
  536.  
  537. ///SPRAWDZONA
  538. void show_list_rek(Node * H) {
  539.     if (H != nullptr) {
  540.         cout << H -> val << "->";
  541.         if (H -> next != nullptr)
  542.             show_list_rek(H -> next);
  543.         else cout << "nullptr";
  544.     }
  545. }
  546.  
  547. void swapSecond(Node * & head) {
  548.     if (head && head -> next) {
  549.         swap(head);
  550.         Node * p = head -> next;
  551.         while (p -> next && p -> next -> next) {
  552.             swap(p -> next);
  553.             p = p -> next -> next;
  554.         }
  555.     }
  556. }
  557. void deleteEven(Node * & H) {
  558.     Node * head = nullptr;
  559.     Node * p = H;
  560.     int i = 0;
  561.     while (p != nullptr) {
  562.         if (i % 2 == 0)
  563.             add(head, p -> val);
  564.         i++;
  565.         p = p -> next;
  566.     }
  567.     H = head;
  568. }
  569. ///SPRAWDZONA
  570. void multipleByTwo(Node * & h) {
  571.     if (h != nullptr) {
  572.         Node * head = nullptr;
  573.         Node * p = h;
  574.         while (p != nullptr) {
  575.  
  576.             add(head, p -> val);
  577.             add(head, p -> val);
  578.  
  579.             p = p -> next;
  580.         }
  581.         show_list_rek(head);
  582.  
  583.     }
  584. }
  585. ///SPRAWDZONA
  586. void multipleByValueNumber(Node * & h) {
  587.     if (h != nullptr) {
  588.         Node * head = nullptr;
  589.         Node * p = h;
  590.         while (p != nullptr) {
  591.             for (int i = 0; i < p -> val; i++) {
  592.                 add(head, p -> val);
  593.             }
  594.             p = p -> next;
  595.         }
  596.  
  597.         h = head;
  598.     }
  599. }
  600. void swapFirstWithSecond(Node * & head) {
  601.     if (head != nullptr && head -> next != nullptr) {
  602.         Node * forgotten = head;
  603.         head = forgotten -> next;
  604.         forgotten -> next = head -> next;
  605.         head -> next = forgotten;
  606.     }
  607. }
  608. void swapFirstWithLast(Node * & head) {
  609.     if (head != nullptr) {
  610.         Node * iterator = head;
  611.         Node * headCopy = head;
  612.         while (iterator != nullptr)
  613.             iterator = iterator -> next;
  614.         iterator -> next = head -> next;
  615.         head -> next = iterator;
  616.  
  617.     }
  618.  
  619. }
  620. ///SPRAWDZONA
  621. void swap2(Node * & H, Node * & replaced) {
  622.     if (H != nullptr && replaced != nullptr) {
  623.         Node * kopia_Glowy = H;
  624.         Node * kopia_nastepnika_Glowy = kopia_Glowy -> next;
  625.         kopia_Glowy = replaced;
  626.         replaced -> next = kopia_nastepnika_Glowy;
  627.  
  628.     }
  629. }
  630.  
  631. //void swap(Node *&H)
  632. //{
  633. //    if(H!= nullptr&&H->next!= nullptr)
  634. //    {
  635. //        Node *p=H;
  636. //        H=p->next;
  637. //        p->next=H->next;
  638. //        H->next=p;
  639. //    }
  640. //}
  641.  
  642. void zamianaPierwszegoElementuZOstatnimNaLiscie(Node * & head) {
  643.     if (head != nullptr) {
  644.         Node * iterator = head;
  645.         while (iterator -> next != nullptr) {
  646.             cout << iterator -> val << " ";
  647.             iterator = iterator -> next;
  648.  
  649.         }
  650.         swap2(head, iterator -> next);
  651.  
  652.     }
  653.  
  654. }
  655.  
  656. void replaceXWithHisNext(Node * & head, int x) {
  657.     if (head != nullptr && head -> next != nullptr) {
  658.         Node * n = head;
  659.         if (head -> val == x) {
  660.             swap(head);
  661.         } else {
  662.             Node * p = head;
  663.             while (p -> next != nullptr && p -> next -> val != x) {
  664.                 p = p -> next;
  665.             }
  666.             if (p -> next != nullptr && p -> next -> next != nullptr) {
  667.                 Node * e = p -> next;
  668.                 p -> next = e -> next;
  669.                 e -> next = e -> next -> next;
  670.                 p -> next -> next = e;
  671.             }
  672.         }
  673.     }
  674. }
  675.  
  676. void replaceXWithHisPervious(Node * & head, int x) {
  677.     if (head != nullptr && head -> next != nullptr) {
  678.         Node * n = head;
  679.         while (n -> next != nullptr) {
  680.             if (n -> next -> val == x) {
  681.                 cout << "\nZnalazlem" << endl;
  682.                 Node * forgotten = n;
  683.                 n = forgotten -> next;
  684.                 forgotten -> next = n -> next;
  685.                 n -> next = forgotten;
  686.                 cout << forgotten -> val << endl << n -> val << endl;
  687.                 break;
  688.             } else n = n -> next;
  689.         }
  690.     }
  691. }
  692.  
  693. void nextTryReplace(Node * & head, int x) {
  694.     if (head != nullptr) {
  695.         Node * p = head;
  696.         if (head -> next == nullptr) {
  697.             return;
  698.         }
  699.         if (head -> val == x) {
  700.             return;
  701.         } else
  702.             while (p -> next != nullptr && p -> next -> val != x) {
  703.                 p = p -> next;
  704.             }
  705.         if (p -> next == nullptr) {
  706.             return;
  707.         } else {
  708.             Node * copy = p;
  709.             p -> next;
  710.             p = p -> next;
  711.  
  712.         }
  713.  
  714.     }
  715.  
  716. }
  717.  
  718. void copy(Node * h) {
  719.     if (h!= nullptr) {
  720.         Node * h2 = nullptr;
  721.         Node * p = h;
  722.         while (p -> next != nullptr) {
  723.             add(h2, p -> val);
  724.             p = p -> next;
  725.         }
  726.         add(h2, p -> val);
  727.         p -> next = h2;
  728.         h2 = nullptr;
  729.     }
  730.  
  731. }
  732. ///SPRAWDZONA
  733. // zamienię miejscami całą tablicę
  734. void reverse(Node * & h) {
  735.     if (h != nullptr) {
  736.         Node * p = h;
  737.         Node * h2 = nullptr;
  738.         while (p != nullptr) {
  739.             add(h2, p -> val);
  740.             p = p -> next;
  741.         }
  742.         h = h2;
  743.  
  744.     }
  745. }
  746. ///SPRAWDZONA
  747. //podziele na pół
  748. void split(Node * & h) {
  749.     Node * p = h;
  750.     Node * copy = nullptr;
  751.     int i = 0;
  752.     while (p) // policz liczbę elementów
  753.     {
  754.         p = p -> next;
  755.         i++;
  756.     }
  757.     i /= 2;
  758.     while (i != 0) {
  759.         i--;
  760.         add(copy, h -> val);
  761.         h = h -> next;
  762.     }
  763.     h = copy;
  764.  
  765. }
  766. ///SPRAWDZONA
  767. void split(Node * & h, Node * & first, Node * & second) {
  768.     Node * p = h;
  769.     Node * iterator = h;
  770.     first = nullptr;
  771.     second = nullptr;
  772.     int i = 0;
  773.     while (p != nullptr) // policz liczbę elementów
  774.     {
  775.         p = p -> next;
  776.         i++;
  777.     }
  778.     i /= 2;
  779.     while (i != 0) {
  780.         i--;
  781.         add(first, iterator -> val);
  782.         iterator = iterator -> next;
  783.     }
  784.     while (iterator != nullptr) {
  785.         add(second, iterator -> val);
  786.         iterator = iterator -> next;
  787.     }
  788.  
  789. }
  790.  
  791.  
  792.  
  793. void swapValue( Node *a, Node *b)
  794. {
  795.     int temp = a->val;
  796.     a->val = b->val;
  797.     b->val = temp;
  798. }
  799.  
  800.  
  801. void bubbleSort(Node *&head)
  802. {
  803.     int i;
  804.     bool isSwapNessesary;
  805.     struct Node *p;
  806.     struct Node *lastStand = nullptr;
  807.     if (head == nullptr)
  808.         return;
  809.     do
  810.     {
  811.         isSwapNessesary = false;
  812.         p = head;
  813.  
  814.         while (p->next != lastStand)
  815.         {
  816.             if (p->val > p->next->val)
  817.             {
  818.                 swapValue(p, p->next);
  819.                 isSwapNessesary = true;
  820.             }
  821.             p = p->next;
  822.         }
  823.         lastStand = p;
  824.     }
  825.     while (isSwapNessesary);
  826. }
  827.  
  828. int calculateNumberOfElementsInLinkedList(Node *&head) {
  829.     int licznik = 0;
  830.     Node * p = head;
  831.     while (p) // liczę ilość elementów w tablicy
  832.     {
  833.         licznik++;
  834.         p = p -> next;
  835.  
  836.     }
  837.     return licznik;
  838. }
  839.  
  840. Node *jumpOverGapRange(int gap, int iterator, Node *tmpHead, Node *endingElement) {
  841.     endingElement = tmpHead;
  842.     iterator = 1;
  843.     while (endingElement->next && iterator < gap) {
  844.         endingElement = endingElement->next;
  845.         iterator++;
  846.     }
  847.     return endingElement;
  848. }
  849. void sortowanieGrzebieniowe(Node*& head) {
  850.  
  851.     if (head) {
  852.         if (head -> next == nullptr) ///jednoelementowa tablica
  853.         {
  854.             return;
  855.         }
  856.         else
  857.         {
  858.     int gap = calculateNumberOfElementsInLinkedList(head);
  859.     int iterator = 0;
  860.     bool replace = true;
  861.     Node* before = nullptr;
  862.     Node* tmp = nullptr;
  863.     Node* tmpHead;
  864.     Node* endingElement;
  865.     Node* afterEndingElement = nullptr;
  866.  
  867.     while (gap > 1 || replace)
  868.     {
  869.         gap = gap * 10 / 13;
  870.         if(gap==0)
  871.         {
  872.             gap=1;
  873.         }
  874.         endingElement =head;
  875.         tmpHead = head;
  876.         replace = false;
  877.         if (gap == 1) {
  878.             bubbleSort(head);
  879.             continue;
  880.         }
  881.         while (tmpHead && endingElement->next->next) {
  882.  
  883.             endingElement = jumpOverGapRange(gap, iterator, tmpHead, endingElement);
  884.  
  885.             if (tmpHead->val > endingElement->next->val) {
  886.                 if (before == nullptr)
  887.                 {
  888.                     tmp = endingElement->next;
  889.                     afterEndingElement = tmp->next;
  890.                     tmp->next = tmpHead->next;
  891.                     endingElement->next = tmpHead;
  892.                     tmpHead->next = afterEndingElement;
  893.  
  894.                     tmpHead = tmp;
  895.                     head = tmp;
  896.                 }
  897.                 else
  898.                 {
  899.                     tmp = endingElement->next;
  900.                     afterEndingElement = tmp->next;
  901.                     tmp->next = tmpHead->next;
  902.                     endingElement->next = tmpHead;
  903.                     before->next = tmp;
  904.                     tmpHead->next = afterEndingElement;
  905.  
  906.                     tmpHead = tmp;
  907.                 }
  908.             }
  909.             before = tmpHead;
  910.             tmpHead = tmpHead->next;
  911.         }
  912.     }
  913. }}}
  914.  
  915. Node* sortedMerge(Node* a, Node* b)
  916. {
  917.     Node* result = nullptr;
  918.  
  919.  
  920.     if (a == nullptr)
  921.         return(b);
  922.     else if (b == nullptr)
  923.         return(a);
  924.  
  925.     if (a->val <= b->val)
  926.     {
  927.         result = a;
  928.         result->next = sortedMerge(a->next, b);
  929.     }
  930.     else
  931.     {
  932.         result = b;
  933.         result->next = sortedMerge(a, b->next);
  934.     }
  935.     return(result);
  936. }
  937.  
  938.  
  939.  
  940. ////////////////////////////////////////
  941.  
  942.  
  943. struct Data
  944. {
  945.  
  946. public:
  947.     friend ostream &operator<<(ostream &os, const Data &data) {
  948.         os<<"Rezerwacja: "  << data.godziny_poczatek << ":" <<  data.minuty_poczatek<< "-" << data.godziny_koniec << ":" << data.minuty_koniec;
  949.         return os;
  950.     }
  951. public:
  952.     Data(){}
  953.     bool operator==(const Data &rhs) const {
  954.         return minuty_poczatek == rhs.minuty_poczatek &&
  955.                minuty_koniec == rhs.minuty_koniec &&
  956.                godziny_poczatek == rhs.godziny_poczatek &&
  957.                godziny_koniec == rhs.godziny_koniec;
  958.     }
  959.  
  960.     bool operator!=(const Data &rhs) const {
  961.         return !(rhs == *this);
  962.     }
  963.  
  964.     Data(int godziny_poczatek,int minuty_poczatek,int godziny_koniec,int minuty_koniec)
  965.     {
  966.         this->minuty_poczatek=minuty_poczatek;
  967.         this->minuty_koniec=minuty_koniec;
  968.         this->godziny_poczatek=godziny_poczatek;
  969.         this->godziny_koniec=godziny_koniec;
  970.         poczatek=minuty_poczatek+godziny_poczatek*60 -5;
  971.         koniec=minuty_koniec+godziny_koniec*60 +5;
  972.     }
  973.  
  974. public:
  975.     bool operator<(const Data &rhs) const {
  976.         if (poczatek < rhs.poczatek)
  977.             return true;
  978.         if (rhs.poczatek < poczatek)
  979.             return false;
  980.         return koniec < rhs.koniec;
  981.     }
  982.  
  983.     bool operator>(const Data &rhs) const {
  984.         return rhs < *this;
  985.     }
  986.  
  987.     bool operator<=(const Data &rhs) const {
  988.         return !(rhs < *this);
  989.     }
  990.  
  991.     bool operator>=(const Data &rhs) const {
  992.         return !(*this < rhs);
  993.     }
  994.  
  995. private:
  996.     int minuty_poczatek;
  997.     int minuty_koniec;
  998.     int godziny_poczatek;
  999.     int godziny_koniec;
  1000.     int poczatek;
  1001.     int koniec;
  1002.  
  1003. };
  1004.  
  1005.  
  1006.  
  1007.  
  1008. struct drzewoBSTDaty {
  1009.  
  1010.     friend ostream &operator<<(ostream &os, drzewoBSTDaty *&bst) {
  1011.         if (bst != nullptr) {
  1012.             os<<(bst -> left);
  1013.             os << bst -> val << " ";
  1014.             os<<(bst -> right);
  1015.         } else
  1016.             return os;
  1017.     }
  1018.  
  1019.     drzewoBSTDaty * up;
  1020.     drzewoBSTDaty * left;
  1021.     drzewoBSTDaty * right;
  1022.     Data val;
  1023.     ///Wypisanie w porządku INORDER
  1024.  
  1025. };
  1026.  
  1027. ///SPRAWDZONA
  1028. void addBstNodeData(drzewoBSTDaty * & root, Data &x, drzewoBSTDaty * lastRoot) {
  1029.     if (root == nullptr) {
  1030.         auto * w = new drzewoBSTDaty();
  1031.         w -> left = nullptr;
  1032.         w -> right = nullptr;
  1033.         w -> up = lastRoot;
  1034.         w -> val = x;
  1035.  
  1036.         root = w;
  1037.  
  1038.     } else {
  1039.         x>=root->val ? addBstNodeData(root -> right, x, root): addBstNodeData(root -> left, x, root);
  1040.     }
  1041. }
  1042.  
  1043. ///SPRAWDZONA
  1044. void addBstNodeData(drzewoBSTDaty * & root, Data &x) {
  1045.     if (root == nullptr) {
  1046.         auto * w = new drzewoBSTDaty();
  1047.         w -> left = nullptr;
  1048.         w -> right = nullptr;
  1049.         w -> val = x;
  1050.  
  1051.         root = w;
  1052.  
  1053.     } else {
  1054.         x>=root->val ? addBstNodeData(root -> right, x, root): addBstNodeData(root -> left, x, root);
  1055.  
  1056.     }
  1057. }
  1058. drzewoBSTDaty * poprzednikData(drzewoBSTDaty * wezel);
  1059. drzewoBSTDaty * nastepnikData(drzewoBSTDaty * wezel);
  1060. drzewoBSTDaty * usunElementData(drzewoBSTDaty * &root, drzewoBSTDaty * x)
  1061. {
  1062.     drzewoBSTDaty * y = x->up, * z;
  1063.  
  1064.     if((x->left) && (x->right))
  1065.     {
  1066.         z = (rand() % 2) ? usunElementData(root, poprzednikData(x)) : usunElementData(root, nastepnikData(x));
  1067.         z->left = x->left;   if(z->left)  z->left->up  = z;
  1068.         z->right = x->right; if(z->right) z->right->up = z;
  1069.     }
  1070.     else z = (x->left) ? x->left : x->right;
  1071.  
  1072.     if(z) z->up = y;
  1073.  
  1074.     if(!y) root = z;
  1075.     else if(y->left == x) y->left = z; else y->right = z;
  1076.  
  1077.     return x;
  1078. }
  1079.  
  1080. ///SPRAWDZONA
  1081. drzewoBSTDaty * znajdzMaxData(drzewoBSTDaty * root) {
  1082.     if (root != nullptr) {
  1083.         if (root -> right != nullptr)
  1084.             znajdzMaxData(root -> right);
  1085.         else return root;
  1086.  
  1087.     }
  1088.  
  1089. }
  1090. drzewoBSTDaty * znajdzMinData(drzewoBSTDaty * root) {
  1091.     if (root != nullptr) {
  1092.         if (root -> left != nullptr)
  1093.             znajdzMinData(root -> left);
  1094.         else return root;
  1095.  
  1096.     }
  1097.  
  1098. }
  1099. drzewoBSTDaty * poprzednikData(drzewoBSTDaty * wezel) {
  1100.     drzewoBSTDaty * r;
  1101.     if (wezel) {
  1102.         if (wezel -> left) return znajdzMaxData(wezel -> left);
  1103.         else {
  1104.             r = wezel -> up;
  1105.             while (r && (wezel == r -> left)) {
  1106.                 wezel = r;
  1107.                 r = r -> up;
  1108.             }
  1109.             return r;
  1110.         }
  1111.  
  1112.     }
  1113.     return nullptr;
  1114. }
  1115.  
  1116. drzewoBSTDaty * nastepnikData(drzewoBSTDaty * wezel) {
  1117.     drzewoBSTDaty * r;
  1118.     if (wezel) {
  1119.         if (wezel -> right) return znajdzMinData(wezel -> right);
  1120.         else {
  1121.             r = wezel -> up;
  1122.             while (r && (wezel == r -> right)) {
  1123.                 wezel = r;
  1124.                 r = r -> up;
  1125.             }
  1126.             return r;
  1127.  
  1128.         }
  1129.     }
  1130.     return nullptr;
  1131. }
  1132.  
  1133. drzewoBSTDaty * szukajData(drzewoBSTDaty * root, Data wartoscSzukana) {
  1134.     if (root != nullptr) {
  1135.         if (root -> val == wartoscSzukana) {
  1136.             return root;
  1137.         }
  1138.         if (wartoscSzukana > root -> val) {
  1139.             szukajData(root -> right, wartoscSzukana);
  1140.         } else szukajData(root -> left, wartoscSzukana);
  1141.  
  1142.         return nullptr;
  1143.     }
  1144.  
  1145. }
  1146.  
  1147. bool czy_cos_sie_naklada(drzewoBSTDaty *&root,Data &dataRezerwacji)
  1148. {
  1149.  
  1150.         if (root != nullptr) {
  1151.             czy_cos_sie_naklada(root -> left,dataRezerwacji);
  1152. //            if() {
  1153. //tutaj warunek gdy się nakładają ..... wydarzenia
  1154. //            }
  1155.             czy_cos_sie_naklada(root -> right, dataRezerwacji);
  1156.         } else
  1157.             return false;
  1158.     }
  1159.  
  1160.  
  1161. void dodajRezerwacje(drzewoBSTDaty *&root,Data &dataRezerwacji)
  1162. {
  1163.     if(root== nullptr)
  1164.     {
  1165.         addBstNodeData(root, dataRezerwacji);
  1166.     } else
  1167.     {
  1168. //        if(szukajData(root,dataRezerwacji)!= nullptr)
  1169. //        {
  1170. //            nullptr;
  1171. //        }
  1172.     }
  1173.  
  1174. }
  1175. void usunRezerwacje(drzewoBSTDaty *&root,Data &dataRezerwacji)
  1176. {
  1177.     if (root != nullptr) {
  1178.         usunRezerwacje(root -> left,dataRezerwacji);
  1179.         if(root->val==dataRezerwacji) {
  1180.             drzewoBSTDaty * poddrzewo= nullptr;
  1181.             addBstNodeData(poddrzewo,dataRezerwacji);
  1182.            delete usunElementData(root,poddrzewo);
  1183.             return;
  1184.         }
  1185.         usunRezerwacje(root -> right, dataRezerwacji);
  1186.     } else
  1187.         return;
  1188. }
  1189.  
  1190. Node* findBeforeMax(Node* head)
  1191. {
  1192.     Node* curr = head;
  1193.     if (curr == nullptr || curr->next == nullptr)
  1194.     {
  1195.         return head;
  1196.     }
  1197.     Node* currmax = head;
  1198.     while(curr->next != nullptr)
  1199.     {
  1200.         if (curr->next->val > currmax->val)
  1201.         {
  1202.             currmax = curr->next;
  1203.         }
  1204.         curr = curr->next;
  1205.     }
  1206.     return currmax;
  1207. }
  1208.  
  1209. Node* findBeforeMin(Node* head)
  1210. {
  1211.     Node* curr = head;
  1212.     if (curr->next == nullptr)
  1213.     {
  1214.         return head;
  1215.     }
  1216.     Node* currmin = head;
  1217.     while (curr->next != nullptr)
  1218.     {
  1219.         if (curr->next->val < currmin->val)
  1220.         {
  1221.             currmin = curr->next;
  1222.         }
  1223.         curr = curr->next;
  1224.     }
  1225.     return currmin;
  1226. }
  1227.  
  1228. void maxminSplit(Node*& head, Node*& head1, Node*& head2)
  1229. {
  1230.     while (head != nullptr)
  1231.     {
  1232.         Node* n = findBeforeMax(head);
  1233.         if (n != head)
  1234.         {
  1235.             Node* max = n->next;
  1236.             n->next = n->next->next;
  1237.             Node* tmp = head1;
  1238.             head1 = max;
  1239.             max->next = tmp;
  1240.         }
  1241.         else
  1242.         {
  1243.             if (n->val > n->next->val)
  1244.             {
  1245.                 Node* max = head1;
  1246.                 head = head->next;
  1247.                 Node* tmp = head1;
  1248.                 head1 = max;
  1249.                 max->next = tmp;
  1250.             }
  1251.             else
  1252.             {
  1253.                 Node* max = n->next;
  1254.                 n->next = n->next->next;
  1255.                 Node* tmp = head1;
  1256.                 head1 = max;
  1257.                 max->next = tmp;
  1258.             }
  1259.         }
  1260.  
  1261.         if (head != nullptr)
  1262.         {
  1263.             Node* m = findBeforeMin(head);
  1264.             if (m != head)
  1265.             {
  1266.                 Node* min = m->next;
  1267.                 m->next = m->next->next;
  1268.                 Node* tmp = head2;
  1269.                 head2 = min;
  1270.                 min->next = tmp;
  1271.             }
  1272.             else
  1273.             {
  1274.                 if (m->val < m->next->val)
  1275.                 {
  1276.                     Node* min = head2;
  1277.                     head = head->next;
  1278.                     Node* tmp = head2;
  1279.                     head2 = min;
  1280.                     min->next = tmp;
  1281.                 }
  1282.                 else
  1283.                 {
  1284.  
  1285.                     Node* min = m->next;
  1286.                     m->next = m->next->next;
  1287.                     Node* tmp = head2;
  1288.                     head2 = min;
  1289.                     min->next = tmp;
  1290.                 }
  1291.             }
  1292.         }
  1293.     }
  1294. }
  1295.  
  1296.  
  1297.  
  1298. void zadanie1(Node*& head)
  1299. {
  1300.     Node* head1 = nullptr;
  1301.     Node* head2 = nullptr;
  1302.     Node* head12 = nullptr;
  1303.     Node* head11 = nullptr;
  1304.     Node* head21 = nullptr;
  1305.     Node* head22 = nullptr;
  1306.  
  1307.  
  1308.     maxminSplit(head, head1, head2);
  1309.     maxminSplit(head1, head11, head12);
  1310.     maxminSplit(head2, head21, head22);
  1311.  
  1312.     bubbleSort(head11);
  1313.     bubbleSort(head12);
  1314.     bubbleSort(head21);
  1315.     bubbleSort(head22);
  1316.  
  1317.     head1 = sortedMerge(head11, head12);
  1318.     head2 = sortedMerge(head21, head22);
  1319.     head = sortedMerge(head1, head2);
  1320. }
  1321.  
  1322. int main() {
  1323.  
  1324.     Data test =  Data(12,15,13,00);
  1325.     Data test2 =  Data(12,55,13,40);
  1326.     Node * lista= nullptr;
  1327.     add(lista,5);
  1328.     add(lista,7);
  1329.     add(lista,8);
  1330.     add(lista,2);
  1331.     add(lista,3);
  1332.     add(lista,6);
  1333.     add(lista,2);
  1334.     add(lista,2);
  1335.     add(lista,1);
  1336.     add(lista,0);
  1337.     add(lista,0);
  1338.     add(lista,0);
  1339.     add(lista,0);
  1340.     zadanie1(lista);
  1341.     return 0;
  1342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement