... std::cout << "Destroying nodes ...\n";
16
17             ListNode<NODETYPE>* currentPtr{firstPtr};
18             ListNode<NODETYPE>* tempPtr{nullptr};
19
20             while (currentPtr != nullptr) { // delete remaining nodes
21                tempPtr = currentPtr;
22                std::cout << tempPtr->data << "\n";
23                currentPtr = currentPtr->nextPtr;
24                delete tempPtr;
25             }
26          } 
27
28          std::cout << "All nodes destroyed\n\n";
29       }
30
31       // insert node at front of list
32       void insertAtFront(const NODETYPE& value) {
33          ListNode<NODETYPE>* newPtr{getNewNode(value)}; // new node
34
35          if (isEmpty()) { // List is empty
36             firstPtr = lastPtr = newPtr; // new list has only one node
37          } 
38          else { // List is not empty
39             newPtr->nextPtr = firstPtr; // point new node to old 1st node
40             firstPtr = newPtr; // aim ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.