Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Задача IX. 2
- Да се разработи рекурсивна фукнция, която намира броя на елементите
- в едносвързан списък от цели числа, имащи стойността x.
- */
- #include <iostream>
- #include <bits/stdc++.h>
- struct Node
- {
- int numberCount;
- int data;
- struct Node* next;
- } *first = NULL, *last = NULL;
- using namespace std;
- void addNumbers(int x) // if number is repeated then the numberCount != 1
- {
- if(first == NULL)
- {
- first = new Node;
- first->data = x;
- first->next = NULL;
- first->numberCount = 1;
- last = first;
- }
- else
- {
- Node *t = new Node;
- t->data = x;
- t->next = NULL;
- t->numberCount = 1;
- last->next = t;
- last = t;
- }
- }
- void displayList(struct Node *p)
- {
- while(p)
- {
- cout << p->data << " ";
- p = p->next;
- }
- cout << endl;
- }
- int countList(struct Node *p)
- {
- static int counterList = 0;
- if(p)
- {
- counterList++;
- countList(p->next);
- }
- else
- return counterList;
- }
- int main()
- {
- addNumbers(1);
- addNumbers(2);
- addNumbers(2);
- addNumbers(4);
- cout << countList(first);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement