193030

SAA IX. Zadacha 3

Oct 28th, 2020 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. /*
  2.     Задача IX. 3
  3.     Да се разработи рекурсивна фукнция и рекурсивна процедура,
  4.     която да намира максималния елемент.
  5.  
  6.  
  7. */
  8.  
  9. #include <iostream>
  10. #include <bits/stdc++.h>
  11.  
  12. struct Node
  13. {
  14.     int numberCount;
  15.     int data;
  16.     struct Node* next;
  17. } *first = NULL, *last = NULL;
  18. using namespace std;
  19.  
  20. void addNumbers(int x) // if number is repeated then the numberCount != 1
  21. {
  22.     if(first == NULL)
  23.     {
  24.         first = new  Node;
  25.         first->data = x;
  26.         first->next = NULL;
  27.         first->numberCount = 1;
  28.         last = first;
  29.     }
  30.     else
  31.     {
  32.  
  33.         Node *t = new Node;
  34.         t->data = x;
  35.         t->next = NULL;
  36.         t->numberCount = 1;
  37.         last->next = t;
  38.         last = t;
  39.     }
  40.  
  41. }
  42.  
  43.  
  44. void displayList(struct Node *p)
  45. {
  46.     while(p)
  47.     {
  48.         cout << p->data << " ";
  49.         p = p->next;
  50.     }
  51.     cout << endl;
  52.  
  53. }
  54.  
  55. int countList(struct Node *p)
  56. {
  57.     static int counterList = 0;
  58.     if(p)
  59.     {
  60.         counterList++;
  61.         countList(p->next);
  62.     }
  63.     else
  64.      return counterList;
  65. }
  66.  
  67. int maxNumberRecursive(struct Node *p)
  68. {
  69.     static int maxNumber = INT_MIN;
  70.     if(p)
  71.     {
  72.      if(p->data > maxNumber)
  73.         maxNumber = p->data;
  74.      maxNumberRecursive(p->next);
  75.     }
  76.     else
  77.         return maxNumber;
  78.  
  79. }
  80. void maxNumberRecursiveProcedure(struct Node *p)
  81. {
  82.     static int maxNumber = INT_MIN;
  83.     if(p)
  84.     {
  85.      if(p->data > maxNumber)
  86.         maxNumber = p->data;
  87.      maxNumberRecursive(p->next);
  88.     }
  89.     else
  90.       cout << "The max number is: " << maxNumber<< endl;
  91.  
  92. }
  93.  
  94. int main()
  95. {
  96.     addNumbers(1);
  97.     addNumbers(2);
  98.     addNumbers(2);
  99.     addNumbers(55);
  100.     cout << countList(first) << endl;
  101.     cout << "The max number is: " << maxNumberRecursive(first) << endl;
  102.  
  103.  
  104. }
  105.  
Add Comment
Please, Sign In to add comment