Advertisement
Garey

SAA_Danny_Spisuk

Apr 2nd, 2018
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 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.     /* base case */
  111.     if (*head_ref == nullptr || del == nullptr)
  112.         return;
  113.  
  114.     /* If node to be deleted is head node */
  115.     if (*head_ref == del)
  116.         *head_ref = del->next;
  117.  
  118.     /* Finally, free the memory occupied by del*/
  119.     delete del;
  120.     return;
  121. }
  122.  
  123. auto remove_duplicates(List *start) -> void {
  124.     List *ptr1, *ptr2, *dup;
  125.     ptr1 = start;
  126.  
  127.     while (ptr1 != nullptr && ptr1->next != nullptr)
  128.     {
  129.         ptr2 = ptr1;
  130.  
  131.         while (ptr2->next != nullptr)
  132.         {
  133.             if (ptr1->data == ptr2->next->data)
  134.             {
  135.                 dup = ptr2->next;
  136.                 ptr2->next = ptr2->next->next;
  137.                 delete(dup);
  138.             }
  139.             else
  140.                 ptr2 = ptr2->next;
  141.         }
  142.         ptr1 = ptr1->next;
  143.     }
  144. }
  145.  
  146. int main()
  147. {
  148.     List* list = nullptr;
  149.  
  150.     push_at_end(&list, 6);
  151.  
  152.     push(&list, 7);
  153.  
  154.     push(&list, 1);
  155.  
  156.     push_at_end(&list, 4);
  157.  
  158.     push(&list, 4);
  159.     push(&list, 4);
  160.     push_at_end(&list, 1);
  161.     push(&list, 1);
  162.  
  163.     cout << "\nLinked list: ";
  164.     printList(list);
  165.  
  166.     cout << "\n\nCount of elements: " << count_of_elements(list);
  167.     cout << "\n\nMax element: " << max_element(list);
  168.  
  169.     if (most_occuring_element(list) != (-1))
  170.         cout << "\n\nMost occuring element: " << most_occuring_element(list);
  171.     else
  172.         cout << "\n\nNo most occurring element!";
  173.  
  174.     cout << "\n\nLinked list without first element: ";
  175.     delete_first(&list, list);
  176.     printList(list);
  177.  
  178.     cout << "\n\nList with removed duplicates: ";
  179.     remove_duplicates(list);
  180.     printList(list);
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement