1 00:00:00,860 --> 00:00:03,140 Iterating through the list is simple. 2 00:00:03,170 --> 00:00:07,250 You follow the next pointer from one link to the next. 3 00:00:07,280 --> 00:00:13,700 Before doing this, let's first fix the memory leak introduced in the previous lecture. 4 00:00:13,700 --> 00:00:15,770 So above the main function. 5 00:00:16,940 --> 00:00:21,250 Uh, we're going to add the following function named bool. 6 00:00:21,390 --> 00:00:23,630 Remove head here. 7 00:00:23,750 --> 00:00:28,160 So in this function we're going to add the if here if statement. 8 00:00:28,190 --> 00:00:34,190 If nullptr or the nullptr equals to p hat. 9 00:00:35,840 --> 00:00:37,250 Don't return. 10 00:00:37,250 --> 00:00:37,940 False. 11 00:00:41,030 --> 00:00:43,580 And then ask. 12 00:00:45,400 --> 00:00:46,420 P task. 13 00:00:48,400 --> 00:00:49,150 He hit. 14 00:00:50,500 --> 00:00:56,740 As always, I'm going to explain all of these codes after writing this here and then. 15 00:00:57,670 --> 00:00:58,270 He had. 16 00:00:58,300 --> 00:00:59,650 We're going to assign P hat. 17 00:00:59,680 --> 00:01:00,820 To P hat. 18 00:01:01,330 --> 00:01:04,350 The next variable of P hat here. 19 00:01:04,360 --> 00:01:07,780 And then we're going to delete the P task. 20 00:01:11,840 --> 00:01:14,970 And we're going to return the P hat. 21 00:01:14,990 --> 00:01:19,520 As you can see here, we're going to we have to return the P, we have to return the Boolean. 22 00:01:19,520 --> 00:01:22,460 So that's why we're going to use the. 23 00:01:23,960 --> 00:01:25,790 Logical operator here. 24 00:01:27,300 --> 00:01:29,150 Um, no, Peter. 25 00:01:30,920 --> 00:01:39,260 So this function will remove the link at the beginning of the list and make sure that the P hat. 26 00:01:40,010 --> 00:01:41,890 Pointer points to the next link. 27 00:01:41,900 --> 00:01:45,350 So which will become the new beginning of the list. 28 00:01:45,590 --> 00:01:48,110 So the function returns a bool. 29 00:01:48,200 --> 00:01:54,400 So the function returns a bool value indicating if there are any more links in the list. 30 00:01:54,410 --> 00:01:59,980 So if this function returns false, then it means the entire list has been deleted. 31 00:01:59,990 --> 00:02:07,760 So the first line of the code checks to see if the function has been called with an empty list. 32 00:02:07,760 --> 00:02:15,830 So once we are reassured that the list has at least one link, we create a temporary copy of Pointer. 33 00:02:15,920 --> 00:02:23,480 So the reason is that the intention is to delete the first item and make hit point to next item. 34 00:02:23,480 --> 00:02:26,870 And to do that we have to do these steps in reverse. 35 00:02:26,870 --> 00:02:37,700 So firstly, make P head pointer to the next item and then delete the P previously pointed on so that 36 00:02:37,730 --> 00:02:45,960 to delete the entire list you need to iterate through the links and this can be carried out using the 37 00:02:45,960 --> 00:02:46,800 while loop. 38 00:02:46,800 --> 00:02:51,870 So below this function we're going to add the destroy list function as well. 39 00:02:51,900 --> 00:02:55,050 Void destroy list. 40 00:02:55,800 --> 00:03:02,520 And while while this remove hit function returns true. 41 00:03:03,630 --> 00:03:07,500 Uh, just while the remove function returns. 42 00:03:07,500 --> 00:03:07,800 True. 43 00:03:08,130 --> 00:03:13,410 And we're going to call the destroy list function here. 44 00:03:15,840 --> 00:03:16,410 Okay. 45 00:03:25,830 --> 00:03:26,280 Here. 46 00:03:26,910 --> 00:03:30,810 So you can now compile the code, run it. 47 00:03:30,840 --> 00:03:39,060 However, you will see no output because all the course does is to create list and then delete delete 48 00:03:39,120 --> 00:03:39,570 it.