Deleting the task list

Iterating through the list is simple, you follow the pNext pointer from one link to the next. Before doing this, let's first fix the memory leak introduced in the last section. Above the main function, add the following function:

    bool remove_head()     {         if (nullptr == pHead) return false;         task* pTask = pHead;         pHead = pHead->pNext;         delete pTask;         return (pHead != nullptr);     }

This function will remove the link at the beginning of the list and make sure that the pHead pointer points to the next link, which will become the new beginning of the list. The function returns a bool value indicating if there are any more links in the list. If this function returns false then it means the entire list has been deleted.

The ...

Get Beginning C++ Programming 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.