using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
struct Node {
int info;
Node* next ;
};
// recycleList -- recycle the nodes in the list referenced by current
void recycleList (Node * current) {
if (current != nullptr) {
recycleList (current->next);
delete (current); // return the storage ref'd by current
}
}
int main () {
int n;
cout << "How long a list do you want? " << endl;
cin >> n;
Node *head = new Node (); // dummy node
head->info =0; // irrelevant -- not used
head->next = nullptr;
Node *current = head; // start iterator at dummy node
for (int i = 1 ; i <= n; i ++) { // Append n nodes
current->next = new Node (); // append a node
current->next->info = i; // insert the value
current = current->next; // move to the next node
}
current->next = nullptr; // Terminate the list
cout << "Printing the list:" << endl;
current = head->next; // Start at appended list
while (current != nullptr) { // For each node in the list
cout << current->info << ' '; // print the current element
current = current->next; // move to the next element
}
cout << endl;
recycleList(head); // Not needed, just illustrating deleting the list
return 0;
}