#include #include using namespace std; //Store the incoming integers in a singly linked list, //putting each incoming integer at the tail of the list. int main() { struct node { int i; node *next; //the next node (or nullptr if no next node) }; node *head {nullptr}; //the first node, or nullptr if no nodes yet node *tail {nullptr}; //the last node, or nullptr if no nodes yet for (;;) { cout << "Input an int (or control-d when done): "; int i {0}; cin >> i; if (!cin) { //if the user typed control-d, break; //out of the for loop } //Create a new node to hold the value of i. node *const p {new node}; //not an array, no square brackets p->i = i; //means (*p).i = i; //Insert the new node at the tail of the list. p->next = nullptr; //nothing will come after p if (head == nullptr) { //if p is the first node, head = p; } if (tail != nullptr) { //if there already is a last node, tail->next = p; //p will come after it. } tail = p; } //Arrive here when the user typed control-d. cout << "\n"; cout << "Here are the ints you typed in:\n"; //Print all the integers. for (const node *p {head}; p != nullptr; p = p->next) { cout << p->i << "\n"; } //Deallocate all the nodes. for (node *p {head}; p != nullptr;) { node *const q {p->next}; //Save p->next before it's destroyed. delete p; //No square brackets to delete a scalar. p = q; } return EXIT_SUCCESS; }