Modern C++:Efficient and Scalable Application Development
上QQ阅读APP看书,第一时间看更新

Adding a Task Object to the List

The next thing to do to provide the code is to add a new task to the task list. This needs to create a new task object and initialize it appropriately and then add it to the list by altering the last link in the list to point to the new link.

Above the main function, add the following function:

    void queue_task(const string& name) 
{
...
}

The parameter is a const reference because we will not change the parameter and we do not want the overhead of a copy being made. The first thing this function must do is create a new link, so add the following lines:

    void queue_task(const string& name) 
{
task* pTask = new task;
pTask->description = name;

pTask->pNext = nullptr;

}

The first line creates a new link on the free store, and the following lines initialize it. This is not necessarily the best way of initializing such an object, and a better mechanism, a constructor, will be covered in Chapter 4, Classes. Notice that the pNext item is initialized to nullptr; this indicates that the link will be at the end of the list.

The final part of this function adds the link to the list, that is, it makes the link the last in the list. However, if the list is empty, it means that this link is also the first link in the list. The code must perform both actions. Add the following code to the end of the function:

    if (nullptr == pHead) 
{
pHead = pTask;
pCurrent = pTask;
}
else
{
pCurrent->pNext = pTask;
pCurrent = pTask;
}

The first line checks to see if the list is empty. If pHead is nullptr, it means that there are no other links and so the current link is the first link, and so both pHead and pCurrent are initialized to the new link pointer. If there are existing links in the list, the link has to be added to the last link, so in the else clause the first line makes the last link point to the new link and the second line initializes pCurrent with the new link pointer, making the new link the last link for any new insertions to the list.

The items are added to the list by calling this function in the main function. In this example, we will queue the tasks to wallpaper a room. This involves removing the old wallpaper, filling any holes in the wall, sizing the wall (painting it with diluted paste to make the wall sticky), and then hanging the pasted wallpaper to the wall. You have to do these tasks in this order, you cannot change the order, so these tasks are ideal for a linked list. In the main function add the following lines:

    queue_task("remove old wallpaper"); 
queue_task("fill holes");
queue_task("size walls");
queue_task("hang new wallpaper");

After the last line, the list has been created. The pHead variable points to the first item in the list and you can access any other item in the list simply by following the pNext member from one link to the next.

You can compile the code, but there is no output. Worse, as the code stands, there is a memory leak. The program has no code to delete the memory occupied by the task objects created on the free store by the new operator.