Advertisement
Garey

Tedi_SAA

Mar 5th, 2019
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. /*! 2 */
  2. #include <iostream>
  3. #include <memory>
  4.  
  5. struct Queue {
  6.     std::size_t key;
  7.  
  8.     std::shared_ptr<Queue> next;
  9. };
  10.  
  11. std::shared_ptr<Queue> first = nullptr;
  12. std::shared_ptr<Queue> last = nullptr;
  13.  
  14. /*! Add Unsigned integer to the structure Queue */
  15. auto push(std::size_t number) -> decltype(void()) {
  16.     auto element = last;
  17.  
  18.     last = std::make_shared<Queue>();
  19.  
  20.     last->key = number;
  21.     last->next = nullptr;
  22.  
  23.     if(element) {
  24.         element->next = last;
  25.     }
  26.     else {
  27.         first = last;
  28.     }
  29.  
  30.     element.reset();
  31. }
  32.  
  33. /*!
  34.  * @brief Deletes the element which is equal to the number that is given
  35.  *  as a parameter and returns its' value into the variable that is set
  36.  *  to be the parameter.
  37.  *
  38.  *  @return status code - 0 for success - 1 for failure
  39.  */
  40. auto pop(std::size_t &number) -> decltype(std::size_t()) {
  41.  
  42.     if (first) {
  43.         auto ptr = first;
  44.  
  45.         number = first->key;
  46.         first = (first->next);
  47.  
  48.         if (first == nullptr)
  49.             last = nullptr;
  50.  
  51.         ptr.reset();
  52.  
  53.         return 0;
  54.     }
  55.  
  56.     return 1;
  57. }
  58.  
  59. /*! Print all elements */
  60. auto print() -> decltype(void()) {
  61.     if (first) {
  62.         auto ptr = first;
  63.  
  64.         while (ptr) {
  65.             std::cout << ptr->key << "\t";
  66.             ptr = ptr->next;
  67.         }
  68.     }
  69.     else
  70.         std::cout << "\n\nNo elements in the queue are present!\n\n";
  71. }
  72.  
  73. /*! Delete duplicates function */
  74. auto pop_duplicates() -> decltype(void()) {
  75.     auto ptr1 = first,
  76.          ptr2 = std::make_shared<Queue>(),
  77.          dupl = std::make_shared<Queue>();
  78.  
  79.     while (ptr1 != nullptr && ptr1->next != nullptr) {
  80.         ptr2 = ptr1;
  81.  
  82.         while (ptr2->next != nullptr) {
  83.             if (ptr1->key == ptr2->next->key) {
  84.                 dupl = ptr2->next;
  85.                 ptr2->next = ptr2->next->next;
  86.  
  87.                 dupl.reset();
  88.             }
  89.             else
  90.                 ptr2 = ptr2->next;
  91.         }
  92.  
  93.         ptr1 = ptr1->next;
  94.     }
  95. }
  96.  
  97. int main()
  98. {
  99.     push(5);
  100.     push(3);
  101.     push(4);
  102.     push(4);
  103.     push(4);
  104.  
  105.     pop_duplicates();
  106.  
  107.     print();
  108. }
  109.  
  110.  
  111. /*! 3 */
  112. #include <iostream>
  113. #include <memory>
  114. #include <functional>
  115.  
  116. struct List {
  117.     std::size_t key;
  118.  
  119.     std::shared_ptr<List> next;
  120.     std::shared_ptr<List> prev;
  121. };
  122.  
  123. /*!
  124.  * @brief Inserts an integer at the begining of the list
  125.  *
  126.  * @param obj - a List structure pointer
  127.  */
  128. auto push_front(std::shared_ptr<List> &obj, std::size_t number) -> decltype(void()) {
  129.     if (number < 0)
  130.         return;
  131.  
  132.     auto node = obj;
  133.  
  134.     obj.reset();
  135.  
  136.     obj = std::make_shared<List>();
  137.    
  138.     obj->key = number;
  139.     obj->prev = nullptr;
  140.     obj->next = node;
  141.  
  142.     if (node)
  143.         node->prev = obj;
  144. }
  145.  
  146. /*!
  147.  * @brief Inserts an integer at the end of the list
  148.  *
  149.  * @param obj - a List structure pointer
  150.  */
  151. auto push_back(std::shared_ptr<List> &obj, std::size_t number) -> decltype(void()) {
  152.     if (number < 0)
  153.         return;
  154.  
  155.     auto node = std::make_shared<List>();
  156.  
  157.     node->next = nullptr;
  158.     node->key = number;
  159.  
  160.     if (obj) {
  161.         auto ptr = obj;
  162.  
  163.         while (ptr->next)
  164.             ptr = ptr->next;
  165.  
  166.         ptr->next = node;
  167.         node->prev = ptr;
  168.     }
  169.     else {
  170.         obj = node;
  171.         node->prev = nullptr;
  172.     }
  173. }
  174.  
  175. /*!
  176.  * @brief Prints all the elements in the list
  177.  *
  178.  * @param obj - a List structure pointer
  179.  */
  180. auto print(std::shared_ptr<List> obj) -> decltype(void()) {
  181.     if (obj) {
  182.         auto ptr = obj;
  183.  
  184.         while (ptr) {
  185.             std::cout << ptr->key << "\t";
  186.             ptr = ptr->next;
  187.         }
  188.     }
  189.     else
  190.         std::cout << "\n\nThere are no present elements in the list!\n\n";
  191. }
  192.  
  193. /*!
  194.  * @brief Calculates the average of the elements in the list
  195.  *
  196.  * @param obj - a List structure pointer
  197.  *
  198.  * @return std::pair - two elements: first - count of elements, second - the average summary
  199.  */
  200. auto average(std::shared_ptr<List> obj) -> decltype(std::pair<std::size_t, std::size_t>()) {
  201.     auto avg(std::size_t(0)), counter(std::size_t(0));
  202.  
  203.     if (obj) {
  204.         auto ptr = obj;
  205.  
  206.         while (ptr) {
  207.  
  208.             avg += ptr->key;
  209.             counter++;
  210.  
  211.             ptr = ptr->next;
  212.  
  213.         }
  214.  
  215.         return std::make_pair( counter ,(avg / counter));
  216.     }
  217.  
  218.     return std::pair<std::size_t, std::size_t>();
  219. }
  220.  
  221. /*!
  222.  * @brief Appends all elements that are near the average at the end of the list.
  223.  *
  224.  * @param obj - a List structure pointer
  225.  */
  226. auto appendIfNear(std::shared_ptr<List> obj) -> decltype(void()) {
  227.     auto pair = average(obj);
  228.  
  229.     if (obj) {
  230.         auto ptr = obj;
  231.  
  232.         while (ptr && pair.first--) {
  233.             if (ptr->key == pair.second || ptr->key + 1 == pair.second || ptr->key - 1 == pair.second)
  234.                 push_back(obj, ptr->key);
  235.  
  236.             ptr = ptr->next;
  237.         }
  238.     }
  239. }
  240.  
  241. int main()
  242. {
  243.     auto list = std::make_shared<List>();
  244.  
  245.     push_front(list, 1);
  246.     push_front(list, 2);
  247.     push_front(list, 4);
  248.  
  249.     push_back(list, 3);
  250.     push_back(list, 5);
  251.     push_back(list, 6);
  252.  
  253.     appendIfNear(list);
  254.  
  255.     print(list);
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement