﻿1
00:00:00,410 --> 00:00:03,980
‫So now we're going to write the destructor for our linked list.

2
00:00:04,280 --> 00:00:11,150
‫And a couple of videos ago we wrote this constructor and you always know what function is the constructor

3
00:00:11,150 --> 00:00:16,820
‫because it has the same name as the class and the same is true for the destructor.

4
00:00:16,820 --> 00:00:22,190
‫It also has the same name as the class, except it has this tilde at the front.

5
00:00:22,580 --> 00:00:29,390
‫One of the things that's confusing about just structures is that some classes like the Linked List class,

6
00:00:29,390 --> 00:00:34,430
‫has a written destructor, but the node class does not.

7
00:00:34,730 --> 00:00:40,970
‫So if you don't actually write a destructor for a class, it is the default destructor that will be

8
00:00:40,970 --> 00:00:45,080
‫run when you delete an instance of that class.

9
00:00:45,440 --> 00:00:50,210
‫So when we create a new linked list, it's going to look something like this.

10
00:00:50,420 --> 00:00:56,120
‫We have this new keyword and that means that we're going to run the constructor for that class.

11
00:00:56,120 --> 00:01:01,250
‫And in this case we're going to call this instance of Linked List, my linked list.

12
00:01:01,670 --> 00:01:07,010
‫So then if you're going to delete this linked list, you would do it like this and that is going to

13
00:01:07,010 --> 00:01:08,720
‫run the destructor.

14
00:01:08,720 --> 00:01:14,960
‫Or if you don't have code written for a destructor, it will run the default destructor to explain the

15
00:01:14,960 --> 00:01:16,340
‫default destructor list.

16
00:01:16,340 --> 00:01:22,160
‫Look at what would happen if we ran the default destructor on a linked list.

17
00:01:22,520 --> 00:01:28,520
‫So this linked list is head and tail and all of these nodes and then also the length.

18
00:01:28,790 --> 00:01:33,230
‫But there are two different classes that make up this linked list.

19
00:01:33,230 --> 00:01:37,100
‫There's the linked List class and there is the node class.

20
00:01:37,280 --> 00:01:43,790
‫And because these are two different classes, the linked List class, if you run the default destructor

21
00:01:44,030 --> 00:01:48,860
‫is just going to delete these items head, tail and length.

22
00:01:49,190 --> 00:01:55,760
‫Those nodes are created by a whole other class and the linked list class doesn't know how to delete

23
00:01:55,760 --> 00:01:56,210
‫them.

24
00:01:56,600 --> 00:02:03,380
‫So if you just run the default destructor for the linked list, these will all be deleted, but all

25
00:02:03,380 --> 00:02:06,290
‫of these nodes will remain in memory.

26
00:02:06,620 --> 00:02:12,110
‫So that is why we have to actually write a destructor for the linked list class.

27
00:02:12,620 --> 00:02:18,860
‫So now let's create the destructor I'm going to bring back Head and we'll start our code out like this.

28
00:02:19,370 --> 00:02:26,750
‫So we'll create a variable temp and we'll point it at head and then we need to loop through and individually

29
00:02:26,750 --> 00:02:28,880
‫delete each one of these nodes.

30
00:02:28,880 --> 00:02:31,580
‫So our loop is going to be while head.

31
00:02:31,760 --> 00:02:36,770
‫In other words, while head is pointing at a node, that conditional will be true.

32
00:02:37,040 --> 00:02:41,630
‫You could also write this as while head is not equal to null pointer.

33
00:02:41,990 --> 00:02:48,830
‫So I'm going to focus just in on this while loop and the first thing we'll do is move head over by setting

34
00:02:48,830 --> 00:02:51,650
‫it to be equal to head next.

35
00:02:51,650 --> 00:02:53,510
‫And that moves that over.

36
00:02:54,150 --> 00:02:56,490
‫Then we'll say delete temp.

37
00:02:56,700 --> 00:03:01,020
‫And this is what actually removes that L1 node from memory.

38
00:03:01,020 --> 00:03:03,690
‫So I'm just going to make that disappear.

39
00:03:03,960 --> 00:03:09,300
‫And then we need to move temp over and have it point to that same node that head is pointing to.

40
00:03:09,690 --> 00:03:11,910
‫So we'll do this again while head.

41
00:03:11,910 --> 00:03:17,550
‫And that is true, it is pointing to a node and that will set head to be equal to head.

42
00:03:17,550 --> 00:03:21,960
‫Next we're going to delete temp and move temp over.

43
00:03:22,440 --> 00:03:30,990
‫Then we'll do this again, we'll move head over, will delete temp and move temp over and we'll do this

44
00:03:30,990 --> 00:03:32,640
‫a couple more times here.

45
00:03:33,690 --> 00:03:37,920
‫And then move temp over and then we'll do this this one last time.

46
00:03:38,040 --> 00:03:45,810
‫Now, when we set head to be equal to head next head next as null pointer and that does this, then

47
00:03:45,810 --> 00:03:48,930
‫we're going to delete temp, we're going to move temp over.

48
00:03:48,930 --> 00:03:54,630
‫But now what do we come up here on the while loop head is pointing to null pointer that breaks us out

49
00:03:54,630 --> 00:03:59,040
‫of the while loop and we have now deleted all of the nodes.

50
00:03:59,340 --> 00:04:05,730
‫So let's bring back these two attributes for the linked list and bring in the rest of our code here.

51
00:04:05,940 --> 00:04:11,460
‫And notice that none of this code does anything to delete these three items.

52
00:04:11,670 --> 00:04:18,540
‫As I mentioned at the beginning of the video, these are all deleted with the default destructor, so

53
00:04:18,540 --> 00:04:22,500
‫you don't have to do anything extra to make sure those get deleted.

54
00:04:22,770 --> 00:04:28,650
‫So there will be a few other data structures in the course that have a written destructor, But this

55
00:04:28,650 --> 00:04:35,460
‫is the only one I'll actually walk through because all of the data structures that have a written destructor

56
00:04:35,460 --> 00:04:37,020
‫have one thing in common.

57
00:04:37,020 --> 00:04:38,730
‫They all have nodes.

58
00:04:39,060 --> 00:04:43,560
‫So that's why this is the only time I'll actually walk through this.

59
00:04:44,070 --> 00:04:47,460
‫And that is our linked list destructor.

