#include #include #include using namespace std; struct link { char c; struct link* next; }; int main() { char c; struct link* first = NULL; /* Start of list */ struct link* current; /* End of list */ struct link* ptr; /* Temporary */ /* Loop reading characters until the end of file */ /* character is reached (EOF) */ /* Note that one cannot use eof() to check for */ /* EOF until one has actually tried to read it */ /* - unlike Pascal. */ while (cin >> c) { /* Loop for all characters */ ptr = new (link); if (ptr == NULL) { cout << " Insufficient memory\n"; exit(1); } /* Add new value and clear subsequent link */ ptr->c = c; ptr->next = NULL; /* Update pointers to add new value at end of list. */ /* The "if" statement could be omitted by making the */ /* first character entered a "special" case - as in */ /* Example 10.1.4 - but generality is a good idea! */ if (first == NULL) /* If this is the 1st character */ first = current = ptr; else /* Otherwise... */ current->next = ptr; current = ptr; } // end of while loop /* Now print out the list */ ptr = first; while (ptr != NULL) { cout << ptr->c; ptr = ptr->next; } cout << endl; return 0; }