Garey

Test_3_Dancho

Apr 16th, 2018
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct List
  6. {
  7.     int data;
  8.     List *next;
  9. };
  10.  
  11. auto push(List** head_ref, int new_data) -> void {
  12.     List* new_list = new List;
  13.  
  14.     new_list->data = new_data;
  15.  
  16.     new_list->next = (*head_ref);
  17.  
  18.     (*head_ref) = new_list;
  19. }
  20.  
  21. auto push_at_end(List** head_ref, int new_data) -> void {
  22.     List* new_list = new List;
  23.  
  24.     List *last = *head_ref;
  25.  
  26.     new_list->data = new_data;
  27.  
  28.     new_list->next = nullptr;
  29.  
  30.     if (*head_ref == nullptr)
  31.     {
  32.         *head_ref = new_list;
  33.         return;
  34.     }
  35.  
  36.     while (last->next != nullptr)
  37.         last = last->next;
  38.  
  39.     last->next = new_list;
  40.     return;
  41. }
  42.  
  43. auto printList(List *list) -> void {
  44.     while (list != nullptr)
  45.     {
  46.         cout << list->data << "  ";
  47.         list = list->next;
  48.     }
  49. }
  50.  
  51. // Задачката от Упражненията 7.1 -- Вариант с глобална променлива:
  52. // Изкарваш size_t count = 0 извън функцията и пак ще бачка хехе
  53. // Другото просто го местиш във main и тука пишеш auto count_of_elements(List *head, size_t &count) -> int и пак работи
  54. auto count_of_elements(List* head) -> int {
  55.     size_t count = 0;
  56.     List* current = head;
  57.     while (current != nullptr)
  58.     {
  59.         count++;
  60.         current = current->next;
  61.     }
  62.     return count;
  63. }
  64.  
  65. auto max_element(List* head) -> int
  66. {
  67.     // INT_MIN е -32767. Ползваме го за начална стойност на максималният елемент
  68.     int max = INT_MIN;
  69.  
  70.     while (head != nullptr) {
  71.  
  72.         if (max < head->data)
  73.             max = head->data;
  74.         head = head->next;
  75.     }
  76.     return max;
  77. }
  78.  
  79. auto most_occuring_element(List* head) -> int {
  80.     List* p = head;
  81.  
  82.     int total_count = 0, max_count = 0, res = -1;
  83.     while (p != nullptr) {
  84.  
  85.         int count = 1;
  86.         List* q = p->next;
  87.         while (q != nullptr) {
  88.             if (p->data == q->data)
  89.                 count++;
  90.             q = q->next;
  91.         }
  92.  
  93.         if (count > max_count)
  94.         {
  95.             max_count = count;
  96.             res = p->data;
  97.         }
  98.  
  99.         p = p->next;
  100.         total_count++;
  101.     }
  102.  
  103.     if (max_count >= total_count / 2)
  104.         return res;
  105.  
  106.     return -1; // Няма най-често срещан елемент, който да е над другите(тоест са по равно всичките)
  107. }
  108.  
  109. auto delete_first(List **head_ref, List *del) -> void {
  110.     if (*head_ref == nullptr || del == nullptr)
  111.         return;
  112.  
  113.     if (*head_ref == del)
  114.         *head_ref = del->next;
  115.  
  116.     delete del;
  117.     return;
  118. }
  119.  
  120. auto remove_duplicates(List *start) -> void {
  121.     List *ptr1, *ptr2, *dup;
  122.     ptr1 = start;
  123.  
  124.     while (ptr1 != nullptr && ptr1->next != nullptr)
  125.     {
  126.         ptr2 = ptr1;
  127.  
  128.         while (ptr2->next != nullptr)
  129.         {
  130.             if (ptr1->data == ptr2->next->data)
  131.             {
  132.                 dup = ptr2->next;
  133.                 ptr2->next = ptr2->next->next;
  134.                 delete dup;
  135.             }
  136.             else
  137.                 ptr2 = ptr2->next;
  138.         }
  139.         ptr1 = ptr1->next;
  140.     }
  141. }
  142.  
  143. int main()
  144. {
  145.     List* list = nullptr;
  146.  
  147.     push_at_end(&list, 6);
  148.  
  149.     push(&list, 7);
  150.  
  151.     push(&list, 1);
  152.  
  153.     push_at_end(&list, 4);
  154.  
  155.     push(&list, 4);
  156.     push(&list, 4);
  157.     push_at_end(&list, 1);
  158.     push(&list, 1);
  159.  
  160.     cout << "\nLinked list: ";
  161.     printList(list);
  162.  
  163.     cout << "\n\nCount of elements: " << count_of_elements(list);
  164.     cout << "\n\nMax element: " << max_element(list);
  165.  
  166.     if (most_occuring_element(list) != (-1))
  167.         cout << "\n\nMost occuring element: " << most_occuring_element(list);
  168.     else
  169.         cout << "\n\nNo most occurring element!";
  170.  
  171.     cout << "\n\nLinked list without first element: ";
  172.     delete_first(&list, list);
  173.     printList(list);
  174.  
  175.     cout << "\n\nList with removed duplicates: ";
  176.     remove_duplicates(list);
  177.     printList(list);
  178.  
  179.     return 0;
  180. }
Add Comment
Please, Sign In to add comment