193030

SAA VII. Zadacha 7

Oct 27th, 2020 (edited)
882
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.      Задача VII.7:
  3.      Да се разработи подпрограма, която добавя нов елемент в края на линеен
  4.      едносвързан списък с цели числа.
  5. */
  6.  
  7. #include <iostream>
  8. #include <bits/stdc++.h>
  9.  
  10. struct Node
  11. {
  12.     int numberCount;
  13.     int data;
  14.     struct Node* next;
  15. } *first = NULL, *last = NULL;
  16. using namespace std;
  17.  
  18. void addNumbers(int x) // if number is repeated then the numberCount != 1
  19. {
  20.     if(first == NULL)
  21.     {
  22.         first = new  Node;
  23.         first->data = x;
  24.         first->next = NULL;
  25.         first->numberCount = 1;
  26.         last = first;
  27.     }
  28.     else
  29.     {
  30.         int numberExist = 1;
  31.         struct Node*p = first;
  32.         while(p)
  33.         {
  34.             if(p->data == x)
  35.             {
  36.                 p->numberCount = p->numberCount + 1;
  37.                 numberExist = 0;
  38.                 break;
  39.             }
  40.             p =p->next;
  41.         }
  42.         if(numberExist)
  43.         {
  44.             Node *t = new Node;
  45.             t->data = x;
  46.             t->next = NULL;
  47.             t->numberCount = 1;
  48.             last->next = t;
  49.             last = t;
  50.         }
  51.  
  52.     }
  53. }
  54.  
  55. void displayList(struct Node *p)
  56. {
  57.     while(p)
  58.     {
  59.         cout << p->data << " ";
  60.         p = p->next;
  61.     }
  62.     cout << endl;
  63.  
  64. }
  65. void countNumber(struct Node *p)
  66. {
  67.     int numberOfElements = 0;
  68.     while(p)
  69.     {
  70.         p = p->next;
  71.         numberOfElements++;
  72.     }
  73.     cout << "The number of elements in the list is: " << numberOfElements << endl;
  74.     cout << endl;
  75.  
  76. }
  77.  
  78. void countSpecificNumbers(struct Node *p, int specifiedNumber)
  79. {
  80.     int numberOfElements = 0;
  81.     while(p)
  82.     {
  83.        if(p->data == specifiedNumber)
  84.             numberOfElements++;
  85.         p = p->next;
  86.  
  87.     }
  88.     cout << "The number of elements with value "<< specifiedNumber << " in the list is: " << numberOfElements << endl;
  89.  
  90. }
  91.  
  92. void findMaxList(struct Node*p)
  93. {
  94.     int maxNumber = INT_MIN;
  95.     while(p)
  96.     {
  97.        if(p->data > maxNumber)
  98.             maxNumber = p->data;
  99.         p = p->next;
  100.  
  101.     }
  102.     cout << "The maximum number is "<< maxNumber << endl;
  103.  
  104. }
  105.  
  106. void displayNonrepeatedNumbers(struct Node*p)
  107. {
  108.     cout << "Numbers that doesn't repeat " << endl;
  109.     while(p)
  110.     {
  111.         if(p->numberCount == 1)
  112.         {
  113.             cout << p->data << " ";
  114.         }
  115.         p = p->next;
  116.     }
  117.     cout << endl;
  118. }
  119.  
  120. void mostCommonElement(struct Node*p)
  121. {
  122.     int mostCommonNumber = p->data;
  123.     int mostCommonElementcounter = p->numberCount;
  124.     while(p)
  125.     {
  126.         if(p->numberCount > mostCommonElementcounter)
  127.         {
  128.             mostCommonNumber = p->data;
  129.         }
  130.         p = p->next;
  131.     }
  132.     cout << "The most common number is " << mostCommonNumber << endl;
  133. }
  134.  
  135. int main()
  136. {
  137.     //2, 4, 0, 2, 5, 4, 7
  138.     addNumbers(2);
  139.     addNumbers(4);
  140.     addNumbers(0);
  141.     addNumbers(2);
  142.     addNumbers(5);
  143.     addNumbers(4);
  144.     addNumbers(7);
  145.     displayList(first);
  146.  
  147.  
  148. }
  149.  
RAW Paste Data