Premetto che l'ho buttata giù senza pensarci troppo quindi fai attenzioni ad eventuali errori:
Codice PHP:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
struct node
{
int data;
node* next;
};
node* head = NULL;
void print(node* start)
{
node* iter = start;
while(iter != NULL)
{
cout << iter->data << " | ";
iter = iter->next;
}
cout << endl;
}
void findMax(node* n, int** max)
{
if(n == NULL)
return;
if(*max == NULL)
*max = new int(n->data);
else if( n->data > **max)
**max = n->data;
findMax(n->next, max);
}
void f(int** n)
{
*n = new int(10);
}
int main(int argc, char *argv[])
{
srand( (unsigned)time(0) );
for(int i = 0; i < 10; i++)
{
node* newNode = new node;
newNode->data = (rand() % 10) + 1;
newNode->next = head;
head = newNode;
}
print(head);
int* max = NULL;
findMax(head, &max);
cout << endl << "Max: " << *max;
//******* VERIFICA **********
node* iter = head;
bool ok = true;
while(iter != NULL)
{
if(*max < head->data)
{
ok = false;
break;
}
iter = iter->next;
}
if(ok)
cout << " - OK" << endl << endl;
else
cout << " - NO" << endl << endl;
//*******************************
delete max;
}