193030

SAA VIII. Zadacha 2

Oct 27th, 2020 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. /*
  2.     ЗЗадача VIII.2:
  3.     Да се разработи рекурсивна функция
  4.     bool Find(int X, point P)
  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)
  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.  
  56.  
  57. void displayList(struct Node *p) // recursive
  58. {
  59.     if(p)
  60.     {
  61.         cout << p->data << " ";
  62.         displayList(p->next);
  63.     }
  64.     //cout << endl;
  65.  
  66. }
  67.  
  68. bool Find(int x, struct  Node*p)
  69. {
  70.     if(p)
  71.     {
  72.         if(p->data == x)
  73.          return true;
  74.         else
  75.         Find(x,p->next);
  76.  
  77.     }
  78.     else
  79.         return false;
  80.  
  81. }
  82.  
  83.  
  84. int main()
  85. {
  86.     addNumbers(2);
  87.     addNumbers(3);
  88.     addNumbers(4);
  89.     displayList(first);
  90.     int res = Find(0,first);
  91.     cout << endl;
  92.     cout << res << endl;
  93.  
  94. }
  95.  
Add Comment
Please, Sign In to add comment