Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Задача IX. 3
- Да се разработи рекурсивна фукнция и рекурсивна процедура,
- която да намира максималния елемент.
- */
- #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 maxNumberRecursive(struct Node *p)
- {
- static int maxNumber = INT_MIN;
- if(p)
- {
- if(p->data > maxNumber)
- maxNumber = p->data;
- maxNumberRecursive(p->next);
- }
- else
- return maxNumber;
- }
- void maxNumberRecursiveProcedure(struct Node *p)
- {
- static int maxNumber = INT_MIN;
- if(p)
- {
- if(p->data > maxNumber)
- maxNumber = p->data;
- maxNumberRecursive(p->next);
- }
- else
- cout << "The max number is: " << maxNumber<< endl;
- }
- int main()
- {
- addNumbers(1);
- addNumbers(2);
- addNumbers(2);
- addNumbers(55);
- cout << countList(first) << endl;
- cout << "The max number is: " << maxNumberRecursive(first) << endl;
- }
Add Comment
Please, Sign In to add comment