Iterating through the list is simple.
You follow the next pointer from one link to the next.
Before doing this, let's first fix the memory leak introduced in the previous lecture.
So above the main function.
Uh, we're going to add the following function named bool.
Remove head here.
So in this function we're going to add the if here if statement.
If nullptr or the nullptr equals to p hat.
Don't return.
False.
And then ask.
P task.
He hit.
As always, I'm going to explain all of these codes after writing this here and then.
He had.
We're going to assign P hat.
To P hat.
The next variable of P hat here.
And then we're going to delete the P task.
And we're going to return the P hat.
As you can see here, we're going to we have to return the P, we have to return the Boolean.
So that's why we're going to use the.
Logical operator here.
Um, no, Peter.
So this function will remove the link at the beginning of the list and make sure that the P hat.
Pointer points to the next link.
So which will become the new beginning of the list.
So the function returns a bool.
So the function returns a bool value indicating if there are any more links in the list.
So if this function returns false, then it means the entire list has been deleted.
So the first line of the code checks to see if the function has been called with an empty list.
So once we are reassured that the list has at least one link, we create a temporary copy of Pointer.
So the reason is that the intention is to delete the first item and make hit point to next item.
And to do that we have to do these steps in reverse.
So firstly, make P head pointer to the next item and then delete the P previously pointed on so that
to delete the entire list you need to iterate through the links and this can be carried out using the
while loop.
So below this function we're going to add the destroy list function as well.
Void destroy list.
And while while this remove hit function returns true.
Uh, just while the remove function returns.
True.
And we're going to call the destroy list function here.
Okay.
Here.
So you can now compile the code, run it.
However, you will see no output because all the course does is to create list and then delete delete
it.