Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,210 --> 00:00:07,050
What is going on, guys, so in this video, what we are going to do is to write a function that counts
2
00:00:07,050 --> 00:00:10,970
the number of nodes in a given linked list.
3
00:00:11,370 --> 00:00:19,320
These functions should basically just receive, receive, receive a list, OK?
4
00:00:19,730 --> 00:00:27,210
It should receive at least then you should count how many nodes are there in that list.
5
00:00:27,840 --> 00:00:31,470
And finally, it should return these count.
6
00:00:31,920 --> 00:00:33,100
Pretty simple, right?
7
00:00:33,120 --> 00:00:34,230
Nothing complicated.
8
00:00:34,410 --> 00:00:35,910
Just receive a list.
9
00:00:36,000 --> 00:00:37,080
OK, get a list.
10
00:00:37,440 --> 00:00:45,110
Count how many nodes there are in this particular list and return these count to whoever called these
11
00:00:45,120 --> 00:00:46,710
function in.
12
00:00:46,710 --> 00:00:53,940
The first thing to note here is that as opposed the two static arrays, for example, we cannot know
13
00:00:53,940 --> 00:00:56,960
in advance what is the size of a linked list.
14
00:00:57,600 --> 00:01:02,970
We simply usually have a pointer to the head of the list and that's it.
15
00:01:03,390 --> 00:01:11,430
We don't have the exact number of nodes that it currently has because we said that we will mostly use
16
00:01:11,430 --> 00:01:20,130
lists as our preferred data structure for tasks or where there are frequent changes in when we have
17
00:01:20,130 --> 00:01:25,140
frequent changes, we cannot know the size of the of the linked list.
18
00:01:25,150 --> 00:01:30,810
And also one question that I've been asked a lot is actually goes like this.
19
00:01:31,140 --> 00:01:33,300
Why do we have to write a function?
20
00:01:33,450 --> 00:01:35,250
Can't we just use size?
21
00:01:35,430 --> 00:01:38,070
So that's one of the questions I've been asked.
22
00:01:38,080 --> 00:01:46,110
So why do we have to write a dedicated function for that, something like that and dedicated function
23
00:01:46,110 --> 00:01:46,670
for that?
24
00:01:47,310 --> 00:01:56,670
Can't we just can't we just use size of OK, the size of operator and will tell us the exact size of
25
00:01:56,700 --> 00:01:57,620
the linked list.
26
00:01:57,720 --> 00:02:00,930
So actually we know that was linked list.
27
00:02:00,930 --> 00:02:02,580
We work with pointers.
28
00:02:02,730 --> 00:02:07,110
We have our head pointer pointing to the head of the list.
29
00:02:07,110 --> 00:02:13,440
For example, we know we have a list with a couple of nodes and we want to find out how many nodes are
30
00:02:13,440 --> 00:02:15,270
there in that list.
31
00:02:15,510 --> 00:02:23,820
We simply can just use the size of because what should we put in the parentheses of this, of the size
32
00:02:23,820 --> 00:02:28,910
of should we put just the hand if we do so, something like that.
33
00:02:28,920 --> 00:02:37,950
So size of size of had we had a pointer, we will simply get the size of a single pointer had and obviously
34
00:02:37,950 --> 00:02:42,270
not the size of the whole list, not even the size of the node itself.
35
00:02:42,270 --> 00:02:45,620
It will be just the size of the pointer.
36
00:02:45,660 --> 00:02:47,670
So I think that's a good idea.
37
00:02:47,670 --> 00:02:50,610
Why size of won't work here.
38
00:02:50,640 --> 00:02:51,770
The size of operator.
39
00:02:51,780 --> 00:02:57,140
But if you still think something can be done with that, please try it out and let me know.
40
00:02:57,450 --> 00:03:00,140
That's an important part of the starting process.
41
00:03:00,420 --> 00:03:05,580
OK, so let's move on and write our a simple function.
42
00:03:06,660 --> 00:03:08,160
OK, so there you go.
43
00:03:08,160 --> 00:03:16,920
So first of all, what we created here is the template structure for the node in our linked list in
44
00:03:16,920 --> 00:03:21,020
what we will do is just to write a function.
45
00:03:21,030 --> 00:03:23,670
OK, so we know that the function returns.
46
00:03:24,030 --> 00:03:25,500
What it returns, it returns.
47
00:03:25,500 --> 00:03:29,760
The count right account account is simply an integer.
48
00:03:29,760 --> 00:03:36,150
So the signature is going to be of an integer type and then we will specify the function names or for
49
00:03:36,150 --> 00:03:40,770
example, count nodes in list.
50
00:03:40,920 --> 00:03:44,220
OK, and what should the function receive?
51
00:03:44,640 --> 00:03:54,390
The function should receive some some address that in this address we have the first node in this list.
52
00:03:55,020 --> 00:04:03,600
So what we can do here is simply to use node star, OK, because we receive the address of the first
53
00:04:03,600 --> 00:04:10,980
element and we have here node star had OK, for example, we're receiving the address of the first node
54
00:04:10,980 --> 00:04:16,430
in the list and now what we want to do is to write the function body.
55
00:04:17,070 --> 00:04:24,000
So for that we will create int count, OK, we will set it to be zero and this will be the count of
56
00:04:24,030 --> 00:04:26,250
the nodes in this in the list.
57
00:04:26,250 --> 00:04:31,710
So Count will represent the count of the nodes and also just for simplicity.
58
00:04:31,710 --> 00:04:33,120
But it's not mandatory.
59
00:04:33,120 --> 00:04:39,680
Let's create some additional pointer, call it let's call it temp, OK.
60
00:04:39,690 --> 00:04:41,820
And we will set temp to point to head.
61
00:04:41,970 --> 00:04:43,530
OK, so we'll steal.
62
00:04:43,530 --> 00:04:49,320
Although it doesn't really matter in this function, we will still keep the head of the list pointing
63
00:04:49,320 --> 00:04:55,890
to the head of the list and temp will simply iterate, kind of iterate and run over each in any of the
64
00:04:55,890 --> 00:04:57,960
elements in this linked list.
65
00:04:58,360 --> 00:04:59,850
So now what do we.
66
00:04:59,910 --> 00:05:08,250
Have to do is to write a simple loop that will iterate over all the elements, and as long as we haven't
67
00:05:08,250 --> 00:05:12,270
reached the last element, we will use count plus plus.
68
00:05:12,310 --> 00:05:19,470
OK, so while in here will be some condition, as long as this condition is true, then count plus plus
69
00:05:20,220 --> 00:05:24,810
and we will use temp equal to town equals to temp next.
70
00:05:24,840 --> 00:05:32,940
OK, so every time we will take the 10 pointer and point to the next element in this linked list.
71
00:05:33,100 --> 00:05:38,970
OK, so the main question that should be asked here is basically what should be the condition in these
72
00:05:39,000 --> 00:05:40,130
while loop?
73
00:05:40,620 --> 00:05:48,030
So if we want to say what will be the condition in just our words, we will say that the condition would
74
00:05:48,030 --> 00:05:54,840
be as long as we haven't reached the end of the list.
75
00:05:54,960 --> 00:06:01,290
OK, so as long as this is OK, we will run the body of this loop.
76
00:06:01,290 --> 00:06:07,560
So while and how we will treat this part like haven't reached the end of the list.
77
00:06:08,130 --> 00:06:14,820
So we know that every every, every node has a data and it has a next pointer.
78
00:06:15,300 --> 00:06:21,780
And one thing that we know about lists is that the last element in the list should point.
79
00:06:21,810 --> 00:06:24,300
These next field should point to null.
80
00:06:24,420 --> 00:06:33,360
That's what specifies that the list ends in this given node, because in the standard ways, that's
81
00:06:33,360 --> 00:06:35,500
how we find the length of the list.
82
00:06:35,520 --> 00:06:38,030
OK, and that's how we find the end of the list.
83
00:06:38,370 --> 00:06:47,970
So simply, as long as we can say that temp next, as long as the next temp does not equal to null.
84
00:06:48,060 --> 00:06:57,600
OK, so if a given node that temp points to if it has a next field and this next field points to no
85
00:06:57,660 --> 00:07:03,780
meaning, we don't have any further nodes, meaning we are at the end of the list.
86
00:07:04,110 --> 00:07:08,100
And that's why we should like finish up with this condition.
87
00:07:08,100 --> 00:07:14,550
The result of the condition should be false and we should be out of the loop in once we are out of this
88
00:07:14,550 --> 00:07:14,960
loop.
89
00:07:14,970 --> 00:07:16,950
We have been iterated.
90
00:07:17,220 --> 00:07:25,200
We have iterated over all the elements of this linked list, count all of them, and we simply will
91
00:07:25,200 --> 00:07:27,720
return the number that we have found.
92
00:07:27,740 --> 00:07:29,220
So return count.
93
00:07:29,220 --> 00:07:36,170
So int count counties of Taipei and the type the signature, everything seems to be OK.
94
00:07:36,840 --> 00:07:38,070
So there you go, guys.
95
00:07:38,340 --> 00:07:43,500
This is our function to count the number of nodes in a list.
96
00:07:43,500 --> 00:07:49,170
So count number of nodes in a list.
97
00:07:49,720 --> 00:07:50,340
Awesome.
9544
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.