Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ЗЗадача VIII.2:
- Да се разработи рекурсивна функция
- bool Find(int X, point P)
- */
- #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(first == NULL)
- {
- first = new Node;
- first->data = x;
- first->next = NULL;
- first->numberCount = 1;
- last = first;
- }
- else
- {
- int numberExist = 1;
- struct Node*p = first;
- while(p)
- {
- if(p->data == x)
- {
- p->numberCount = p->numberCount + 1;
- numberExist = 0;
- break;
- }
- p =p->next;
- }
- if(numberExist)
- {
- Node *t = new Node;
- t->data = x;
- t->next = NULL;
- t->numberCount = 1;
- last->next = t;
- last = t;
- }
- }
- }
- void displayList(struct Node *p) // recursive
- {
- if(p)
- {
- cout << p->data << " ";
- displayList(p->next);
- }
- //cout << endl;
- }
- bool Find(int x, struct Node*p)
- {
- if(p)
- {
- if(p->data == x)
- return true;
- else
- Find(x,p->next);
- }
- else
- return false;
- }
- int main()
- {
- addNumbers(2);
- addNumbers(3);
- addNumbers(4);
- displayList(first);
- int res = Find(0,first);
- cout << endl;
- cout << res << endl;
- }
Add Comment
Please, Sign In to add comment