Advertisement
193030

SAA IX. Zadacha 2

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