/*****************************************
** This program demonstrates an iterative
** approach to producing a reverse copy of
** a list, i.e. a new list whose nodes have
** all the same values in the reverse order.
** Doing this recursively is more difficult!
*****************************************/
#include <iostream>
using namespace std;
/*****************************************
** struct def's and function prototypes
*****************************************/
struct Node
{
double data;
Node *next;
};
Node* add2front(double val, Node* L);
void printlist(Node* L);
Node* reverse(Node* L);
/*****************************************
** MAIN
*****************************************/
int main()
{
// Get original list of values from user
Node* L = NULL;
double x;
while(cin >> x)
L = add2front(x,L);
// Make copy in reverse
Node *Lp = reverse(L);
// Print both to show they're reverses!
printlist(L);
cout << endl;
printlist(Lp);
cout << endl;
return 0;
}
/*****************************************
** Function definitions
*****************************************/
Node* reverse(Node* L)
{
Node* R = NULL;
for(Node *p = L; p != 0; p = p->next)
R = add2front(p->data,R);
return R;
}
Node* add2front(double val, Node* L)
{
Node* T = new Node{val, L};
return T;
}
void printlist(Node* L)
{
for(Node* T = L; T != NULL; T = T->next)
cout << T->data << ' ';
}