﻿1
00:00:00,400 --> 00:00:04,630
‫So now we're going to build our constructor for our linked list.

2
00:00:04,630 --> 00:00:11,050
‫But before we create the constructor, we need to create the class first and then the constructor is

3
00:00:11,050 --> 00:00:13,030
‫part of that class.

4
00:00:13,540 --> 00:00:20,920
‫But before we get into creating the constructor, I want to talk about three other member functions

5
00:00:20,920 --> 00:00:28,540
‫that have a couple of things in common with this constructor, and those functions are append.

6
00:00:29,230 --> 00:00:30,280
‫Pretend.

7
00:00:30,700 --> 00:00:31,900
‫And insert.

8
00:00:32,720 --> 00:00:37,190
‫So the first thing these have in common is they all get past a value.

9
00:00:37,670 --> 00:00:43,670
‫But it is the reason that these all get passed to value that's going to be important to us here.

10
00:00:44,150 --> 00:00:50,540
‫So the way we're going to create our linked list constructor in this course is we're going to create

11
00:00:50,540 --> 00:00:56,180
‫the first node in the linked list at the time that we create the linked list.

12
00:00:56,600 --> 00:01:01,010
‫Now, let's look quickly at what these other three functions do.

13
00:01:01,280 --> 00:01:06,650
‫First, append will create a new node and then add that node to the end.

14
00:01:07,100 --> 00:01:13,040
‫Prepared will create a new node and add that node to the beginning and then insert will create a new

15
00:01:13,040 --> 00:01:17,900
‫node and then insert that node at a particular index.

16
00:01:18,230 --> 00:01:25,520
‫So the other thing these four functions have in common is they all create a new node.

17
00:01:25,820 --> 00:01:33,650
‫So what we're going to do is create a separate class that just creates nodes and we'll call that class

18
00:01:34,160 --> 00:01:35,060
‫node.

19
00:01:35,690 --> 00:01:43,280
‫Then when any of these four functions needs to create a node, they will call on the node class to do

20
00:01:43,280 --> 00:01:43,850
‫that.

21
00:01:44,450 --> 00:01:50,930
‫So before we build out this node class, let's talk about what a node is.

22
00:01:51,350 --> 00:02:00,380
‫So we've discussed previously that a node is very similar to an unordered map with value and next.

23
00:02:00,830 --> 00:02:07,460
‫So our node class is going to have member variables of value and next.

24
00:02:08,110 --> 00:02:15,640
‫So value will be an integer and next will be a pointer that will point to a node.

25
00:02:16,180 --> 00:02:21,730
‫And the node class will also have a constructor that we pass a value to.

26
00:02:22,330 --> 00:02:27,130
‫And then we'll say This value equals value.

27
00:02:27,700 --> 00:02:36,850
‫So we're passing this node constructor of value that is this value down here, but this value is something

28
00:02:36,850 --> 00:02:37,300
‫different.

29
00:02:37,300 --> 00:02:44,950
‫When we set this value to be equal to value, this value is the value up here.

30
00:02:44,950 --> 00:02:47,980
‫It's the member variable for the node class.

31
00:02:48,430 --> 00:02:52,300
‫And that's how we assign a value to a new node.

32
00:02:52,870 --> 00:02:59,860
‫And we have to use the keyword this to specify that we're talking about the member variable value up

33
00:02:59,860 --> 00:03:00,490
‫here.

34
00:03:00,940 --> 00:03:06,820
‫And the reason we need to do that is we need to clarify that we're not talking about these variables

35
00:03:06,820 --> 00:03:08,560
‫that are called value.

36
00:03:08,770 --> 00:03:14,350
‫So for example, we could change the name of these to Val instead of value.

37
00:03:14,350 --> 00:03:22,060
‫And if we did that, we wouldn't need the key word this because it will be clear that this variable

38
00:03:22,060 --> 00:03:28,780
‫here and this variable here are different things because they have a different name.

39
00:03:29,230 --> 00:03:38,380
‫But we have to use the key word this to go with value if this variable name is also value, and that's

40
00:03:38,380 --> 00:03:43,300
‫the way we're going to do it here, and that is the standard naming convention.

41
00:03:43,960 --> 00:03:48,760
‫So then we're going to say this next equals null pointer.

42
00:03:49,420 --> 00:03:54,520
‫And this next is referring to this variable up here.

43
00:03:54,970 --> 00:03:59,170
‫But in this case, we can actually remove the key word this.

44
00:03:59,170 --> 00:04:05,710
‫And the reason for that is we don't have next as a parameter for the node function.

45
00:04:06,310 --> 00:04:12,880
‫So now that we have our node class built, we can start building out our length list class.

46
00:04:13,390 --> 00:04:15,820
‫So we're going to have a few class variables.

47
00:04:15,820 --> 00:04:17,260
‫We'll make these private.

48
00:04:17,260 --> 00:04:24,340
‫The class variables will be head, which is a pointer to a node, and then we'll have tail, which is

49
00:04:24,340 --> 00:04:30,670
‫also a pointer to a node, and then we'll keep track of the length and that will be an integer.

50
00:04:31,180 --> 00:04:33,840
‫So now we can create our constructor.

51
00:04:33,850 --> 00:04:38,920
‫That constructor will be public and we'll start building the constructor like this.

52
00:04:39,280 --> 00:04:40,900
‫And so we have more room.

53
00:04:40,900 --> 00:04:46,000
‫I'm just going to focus in on this constructor and move that up here.

54
00:04:46,450 --> 00:04:52,360
‫So the first thing we'll do when we create a linked list is we'll create the first node in the linked

55
00:04:52,360 --> 00:04:59,530
‫list and we'll do that like this and we will pass that the value that we pass the link list constructor

56
00:04:59,530 --> 00:05:02,140
‫and that will create our node.

57
00:05:02,560 --> 00:05:07,930
‫So if you've never seen a line of code like this before, this might be a little confusing.

58
00:05:07,930 --> 00:05:14,200
‫It might even look redundant where each side of the equals sign that we're basically doing the same

59
00:05:14,200 --> 00:05:18,250
‫thing, but that is actually not the case.

60
00:05:18,610 --> 00:05:25,840
‫So on the left side of the equals sign, what we're doing is we're creating a variable that can point

61
00:05:25,840 --> 00:05:26,980
‫to a node.

62
00:05:27,250 --> 00:05:34,990
‫Then on the right side of the equals sign, we have this new keyword which is going to run the constructor

63
00:05:34,990 --> 00:05:37,480
‫for creating a node.

64
00:05:37,750 --> 00:05:44,410
‫So everything on the right of the equals sign is what actually creates this node.

65
00:05:45,020 --> 00:05:47,570
‫And that equal sign that's in the middle.

66
00:05:47,840 --> 00:05:53,180
‫That's what points that variable at that new node that is being created.

67
00:05:53,510 --> 00:05:58,640
‫So the next line of the constructor sets head to be equal to new node.

68
00:05:59,090 --> 00:06:03,560
‫So I'm going to bring back the rest of our code and talk about this.

69
00:06:03,920 --> 00:06:10,670
‫So head is a pointer to a node and new node is a pointer to a node.

70
00:06:10,850 --> 00:06:17,330
‫So when you set head to be equal to new node, what you're saying is that you want to point this variable

71
00:06:17,330 --> 00:06:22,130
‫head at the same node that new node is pointing to.

72
00:06:22,640 --> 00:06:26,860
‫So let's bring this back and that does this.

73
00:06:26,870 --> 00:06:31,670
‫It just points this to the same node that new node is pointing to.

74
00:06:32,120 --> 00:06:35,540
‫So I'm going to drop that new node out of the graphic.

75
00:06:35,540 --> 00:06:42,140
‫And moving forward in the course, I'll do something that looks like this tail equals new node, and

76
00:06:42,140 --> 00:06:46,370
‫we'll know that that is the new node that we're pointing tail to.

77
00:06:46,790 --> 00:06:52,430
‫But I wanted to walk through the mechanics of what we're actually doing when we do that.

78
00:06:52,730 --> 00:06:59,420
‫And because we now have one node on our linked list, we'll set the length to be equal to one.

79
00:06:59,930 --> 00:07:02,370
‫So let's bring back the rest of our code.

80
00:07:02,390 --> 00:07:04,870
‫This is our entire length list class.

81
00:07:04,880 --> 00:07:12,380
‫So far we have these three class variables up here and then our constructor down here.

82
00:07:13,160 --> 00:07:16,040
‫Let's focus back in on that constructor.

83
00:07:16,460 --> 00:07:21,830
‫And the way that we would create a new linked list would be something like this.

84
00:07:22,280 --> 00:07:30,800
‫So I have my variable name is my linked list and the variable type is that it is a pointer to a linked

85
00:07:30,800 --> 00:07:31,550
‫list.

86
00:07:32,090 --> 00:07:36,440
‫And on this side over here, we're creating our new length list.

87
00:07:36,950 --> 00:07:42,020
‫That new keyword means we are running this constructor up here.

88
00:07:42,440 --> 00:07:46,610
‫And when we run this, we get all of this.

89
00:07:46,610 --> 00:07:54,800
‫We get a new node with a value of four head and tail are pointed at that new node, and our length is

90
00:07:54,800 --> 00:07:56,300
‫equal to one.

91
00:07:56,840 --> 00:08:01,590
‫So we'll look at all of this code in the next video in voice code.

92
00:08:01,610 --> 00:08:05,840
‫But for now, that is our length list constructor.

