/*****************************************
** This program demonstrates an iterative
** and a recursive approach to defining
** the max function.
*****************************************/
#include <iostream>
#include <algorithm> // defines max and min for doubles!
using namespace std;
/*****************************************
** struct def's and function prototypes
*****************************************/
struct Node
{
double data;
Node *next;
};
Node* add2front(double val, Node* L);
// ITERATIVE VERSION OF MAX (assume L not empty!)
double max_i(Node* L);
/*****************************************
** MAIN
*****************************************/
int main()
{
// Get list of values from user
Node *L = 0;
double x;
while(cin >> x)
L = add2front(x,L);
// Print maximum using both iteration and recursion
cout << "Maximum (via iteration) is: "
<< max_i(L) << endl;
return 0;
}
/*****************************************
** Function definitions
*****************************************/
// ITERATIVE VERSION OF MAX (assume L not empty!)
double max_i(Node* L)
{
double m = L->data;
for(Node *p = L->next; p != 0; p = p->next)
m = max(m,p->data);
return m;
}
Node* add2front(double val, Node* L)
{
Node *T = new Node{val, L};
return T;
}