WEBVTT

00:00.860 --> 00:03.140
Iterating through the list is simple.

00:03.170 --> 00:07.250
You follow the next pointer from one link to the next.

00:07.280 --> 00:13.700
Before doing this, let's first fix the memory leak introduced in the previous lecture.

00:13.700 --> 00:15.770
So above the main function.

00:16.940 --> 00:21.250
Uh, we're going to add the following function named bool.

00:21.390 --> 00:23.630
Remove head here.

00:23.750 --> 00:28.160
So in this function we're going to add the if here if statement.

00:28.190 --> 00:34.190
If nullptr or the nullptr equals to p hat.

00:35.840 --> 00:37.250
Don't return.

00:37.250 --> 00:37.940
False.

00:41.030 --> 00:43.580
And then ask.

00:45.400 --> 00:46.420
P task.

00:48.400 --> 00:49.150
He hit.

00:50.500 --> 00:56.740
As always, I'm going to explain all of these codes after writing this here and then.

00:57.670 --> 00:58.270
He had.

00:58.300 --> 00:59.650
We're going to assign P hat.

00:59.680 --> 01:00.820
To P hat.

01:01.330 --> 01:04.350
The next variable of P hat here.

01:04.360 --> 01:07.780
And then we're going to delete the P task.

01:11.840 --> 01:14.970
And we're going to return the P hat.

01:14.990 --> 01:19.520
As you can see here, we're going to we have to return the P, we have to return the Boolean.

01:19.520 --> 01:22.460
So that's why we're going to use the.

01:23.960 --> 01:25.790
Logical operator here.

01:27.300 --> 01:29.150
Um, no, Peter.

01:30.920 --> 01:39.260
So this function will remove the link at the beginning of the list and make sure that the P hat.

01:40.010 --> 01:41.890
Pointer points to the next link.

01:41.900 --> 01:45.350
So which will become the new beginning of the list.

01:45.590 --> 01:48.110
So the function returns a bool.

01:48.200 --> 01:54.400
So the function returns a bool value indicating if there are any more links in the list.

01:54.410 --> 01:59.980
So if this function returns false, then it means the entire list has been deleted.

01:59.990 --> 02:07.760
So the first line of the code checks to see if the function has been called with an empty list.

02:07.760 --> 02:15.830
So once we are reassured that the list has at least one link, we create a temporary copy of Pointer.

02:15.920 --> 02:23.480
So the reason is that the intention is to delete the first item and make hit point to next item.

02:23.480 --> 02:26.870
And to do that we have to do these steps in reverse.

02:26.870 --> 02:37.700
So firstly, make P head pointer to the next item and then delete the P previously pointed on so that

02:37.730 --> 02:45.960
to delete the entire list you need to iterate through the links and this can be carried out using the

02:45.960 --> 02:46.800
while loop.

02:46.800 --> 02:51.870
So below this function we're going to add the destroy list function as well.

02:51.900 --> 02:55.050
Void destroy list.

02:55.800 --> 03:02.520
And while while this remove hit function returns true.

03:03.630 --> 03:07.500
Uh, just while the remove function returns.

03:07.500 --> 03:07.800
True.

03:08.130 --> 03:13.410
And we're going to call the destroy list function here.

03:15.840 --> 03:16.410
Okay.

03:25.830 --> 03:26.280
Here.

03:26.910 --> 03:30.810
So you can now compile the code, run it.

03:30.840 --> 03:39.060
However, you will see no output because all the course does is to create list and then delete delete

03:39.120 --> 03:39.570
it.
