Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:02,310
All right, what is going on, guys?
2
00:00:02,340 --> 00:00:10,490
And welcome back to another video where we are going to practice our skills using the linked lists.
3
00:00:10,500 --> 00:00:17,880
So in this exercise, what we are required to do, what we are requested to do is to write a function
4
00:00:17,880 --> 00:00:20,410
that creates a linked list.
5
00:00:20,470 --> 00:00:26,880
OK, so the function should create create a linked list.
6
00:00:27,740 --> 00:00:31,230
So basically saying the function should create this new list.
7
00:00:31,360 --> 00:00:33,060
OK, these are the steps.
8
00:00:33,060 --> 00:00:46,980
Create a new list, receive receive numbers as the input from the user and every received received number
9
00:00:47,190 --> 00:00:53,010
should be added to the and to the end of the list.
10
00:00:53,220 --> 00:00:59,790
That's basically the flow of how the function is going to work in this top condition.
11
00:00:59,790 --> 00:01:08,220
The stopping condition, the stopping condition in this case for the list will be when the number is
12
00:01:08,220 --> 00:01:09,210
minus one.
13
00:01:09,390 --> 00:01:16,200
So would be when number equal is, let's say is minus one.
14
00:01:16,440 --> 00:01:25,010
And finally, what the function should do is return the new created list.
15
00:01:25,020 --> 00:01:29,790
So that's pretty much everything we have to do in this exercise.
16
00:01:29,910 --> 00:01:35,450
So once again, I've went over the steps quickly, one by one.
17
00:01:35,460 --> 00:01:38,260
Let's briefly discuss them once again.
18
00:01:38,430 --> 00:01:41,880
So first of all, we need a function that will create a linked list.
19
00:01:41,970 --> 00:01:44,580
OK, then one, the function.
20
00:01:45,420 --> 00:01:46,680
We're rolling twice.
21
00:01:46,710 --> 00:01:53,660
OK, so creating a linked list received no from numbers from the user input.
22
00:01:53,720 --> 00:02:00,000
OK, and at every time you receive a number, create a new node and add this number to the end of the
23
00:02:00,000 --> 00:02:04,020
list, at least note to the end of the list and do so.
24
00:02:04,020 --> 00:02:10,770
Repeat this operation until a stopping condition of minus one when the number receives from the user
25
00:02:10,770 --> 00:02:11,490
is minus one.
26
00:02:11,860 --> 00:02:19,620
So until of this condition happens, keep on receiving numbers from the user and creating new nodes
27
00:02:19,620 --> 00:02:21,930
and adding them to the end of the list.
28
00:02:22,140 --> 00:02:28,320
Once the stopping condition is met, then in this case, return the address of the first node.
29
00:02:28,410 --> 00:02:30,660
Return the new list.
30
00:02:30,780 --> 00:02:34,140
OK, that's all that we have to do.
31
00:02:34,950 --> 00:02:37,820
OK, so now let's talk about the solution.
32
00:02:37,830 --> 00:02:40,580
So first of all, one more time.
33
00:02:40,590 --> 00:02:43,900
Let's understand what we are being asked.
34
00:02:43,920 --> 00:02:45,060
We have a function.
35
00:02:45,330 --> 00:02:52,440
This function does not receive anything as an argument, OK, anything because we don't see in the question
36
00:02:52,440 --> 00:02:57,250
anything similar to the phrase the function should receive and yada, yada, yada.
37
00:02:57,270 --> 00:02:57,650
Right.
38
00:02:58,080 --> 00:03:02,860
So we'll assume that we'll just have empty parentheses for this function.
39
00:03:02,880 --> 00:03:06,950
OK, in the function creates a new list.
40
00:03:06,990 --> 00:03:10,420
So this part we already know how to make.
41
00:03:10,440 --> 00:03:17,210
OK, because we've seen in one of her previous examples how to create a new list.
42
00:03:17,220 --> 00:03:18,900
I think we've seen it right.
43
00:03:19,050 --> 00:03:20,790
If not, that's a problem.
44
00:03:21,510 --> 00:03:22,850
We will see it right now.
45
00:03:22,920 --> 00:03:24,300
So no worries, guys.
46
00:03:25,230 --> 00:03:33,720
Then what we will have to do inside of the body of this function is simply to run some loop, OK, to
47
00:03:33,720 --> 00:03:37,410
get inputs from the user and add them to the end of the list.
48
00:03:37,650 --> 00:03:39,860
Any for everything has gone smoothly.
49
00:03:40,080 --> 00:03:42,900
Then the function should return the new created list.
50
00:03:42,900 --> 00:03:46,740
Then you aren't the first node in this list.
51
00:03:46,770 --> 00:03:56,040
OK, so one of the questions that students like to ask me are here is why didn't we use an array to
52
00:03:56,040 --> 00:03:57,900
solve these problem?
53
00:03:58,200 --> 00:04:02,040
And actually, it's a very good question that I've been asked a lot.
54
00:04:02,460 --> 00:04:07,050
How come that we've always used the raise for these types of questions?
55
00:04:07,380 --> 00:04:10,320
Why should we bother to use linked lists?
56
00:04:10,320 --> 00:04:20,040
Now, in the end, the answer for this question is very simple, because we do not know in advance what
57
00:04:20,040 --> 00:04:25,380
is the size of the inputs that the user is going to give us.
58
00:04:25,850 --> 00:04:32,220
That means we don't have any idea how many numbers we are going to receive from the user during these
59
00:04:32,220 --> 00:04:32,940
process.
60
00:04:33,180 --> 00:04:35,930
It may be 10 or it may be ten thousand.
61
00:04:36,180 --> 00:04:44,460
So how can we create an array of just some fixed size if we don't know how many elements we are going
62
00:04:44,460 --> 00:04:45,140
to get?
63
00:04:45,450 --> 00:04:53,640
You got the idea, guys, and we know that a linked list is an easy way and easy structure that we can
64
00:04:53,640 --> 00:04:54,990
use to expand.
65
00:04:54,990 --> 00:04:57,360
We just have to add a new node.
66
00:04:57,630 --> 00:04:58,830
And there you go.
67
00:04:58,830 --> 00:04:59,490
You got your.
68
00:04:59,550 --> 00:05:07,620
So some new element added to your data structure, you have expanded your linked list, but that is
69
00:05:07,650 --> 00:05:15,330
not the case for a race, OK, although you can do something, something very tricky by using these
70
00:05:15,330 --> 00:05:17,830
real function for a dynamic array.
71
00:05:18,330 --> 00:05:23,170
But still, these operations are much less efficient.
72
00:05:23,790 --> 00:05:33,400
So that's the reason why we will use linked lists for these question instead of our well known arrays.
73
00:05:33,450 --> 00:05:35,050
So I hope that's clear to you guys.
74
00:05:35,070 --> 00:05:41,610
It's very important not only to get some hands on, but also to understand what is going on behind the
75
00:05:41,610 --> 00:05:49,800
scenes and how we can use it for our advantage and also to prove that our solution will be good enough
76
00:05:50,040 --> 00:05:56,550
and also to be able to answer the question, why didn't you use something else?
77
00:05:56,610 --> 00:05:57,810
OK, fantastic.
78
00:05:57,810 --> 00:05:59,250
Let's start coding.
79
00:05:59,250 --> 00:05:59,640
Right.
80
00:05:59,790 --> 00:06:01,380
That's time to some coding.
81
00:06:01,830 --> 00:06:07,680
And the first thing we are going to need for this question is defining a new type of structure.
82
00:06:07,680 --> 00:06:08,310
Our.
83
00:06:08,970 --> 00:06:10,800
OK, so let's define our node.
84
00:06:10,980 --> 00:06:17,480
And we know that it can be that it can be the same node as we use it in.
85
00:06:17,820 --> 00:06:20,910
Yeah, I think I think I added this video in previous examples.
86
00:06:21,060 --> 00:06:23,940
So but if you're not not familiar with that.
87
00:06:23,940 --> 00:06:27,180
So let's use typedef struct node.
88
00:06:27,300 --> 00:06:34,980
OK, so we creating a new structure called node using these typedef which we already also discussed.
89
00:06:35,010 --> 00:06:38,430
OK, and now we are going to fill in the fields.
90
00:06:38,550 --> 00:06:43,860
OK, so the first field is going to be let's say into data and the second field.
91
00:06:43,860 --> 00:06:49,350
OK, because we are talking about nodes in this linked list.
92
00:06:49,350 --> 00:06:51,270
We know they're from one node.
93
00:06:51,300 --> 00:06:55,550
We should be able to access the next node using some pointers.
94
00:06:55,560 --> 00:07:00,060
So these pointer is going to be of struct node, right.
95
00:07:00,060 --> 00:07:06,680
Because we are going to point to the next node in the next node is of type struct node.
96
00:07:06,690 --> 00:07:15,900
So next is the name of the field which will contain and hold a pointer to the next node in the linked
97
00:07:15,900 --> 00:07:16,250
list.
98
00:07:16,500 --> 00:07:21,570
And also we are going to specify here, no the so that whenever we would like to create a new node,
99
00:07:21,810 --> 00:07:26,460
we will know that we can use node and it will be struct node.
100
00:07:26,550 --> 00:07:27,610
Awesome guys.
101
00:07:27,630 --> 00:07:33,660
So now let's write the main function, not the main function, but the function that we are interested
102
00:07:33,660 --> 00:07:33,870
in.
103
00:07:34,200 --> 00:07:37,880
And we know that this function should return a linked list.
104
00:07:38,100 --> 00:07:43,890
So the question is, should it return just node or should it return node star?
105
00:07:43,920 --> 00:07:46,130
So take a second and think about it.
106
00:07:46,140 --> 00:07:53,880
And once you are done thinking about it and think for yourself is why node is not enough because node
107
00:07:54,450 --> 00:07:56,590
for some reasons it may be good.
108
00:07:56,740 --> 00:08:03,900
OK, I don't think I will cover up in this video, but what we want to do is the return of the address.
109
00:08:04,260 --> 00:08:13,380
OK, the address of the first element of this array, I'm not talking about all the details behind the
110
00:08:13,380 --> 00:08:21,000
scenes because you we would have to talk about copying a value and by value and the copying or what
111
00:08:21,000 --> 00:08:21,930
happens behind the scenes.
112
00:08:22,170 --> 00:08:29,160
But what I want you to understand is that it's much better here to just return of the address of the
113
00:08:29,160 --> 00:08:34,860
first node and then the main function would when we are going to receive this address, we are simply
114
00:08:34,860 --> 00:08:36,240
going to get this address.
115
00:08:36,360 --> 00:08:42,480
When we know that from this address on, we have our first node and we know how to access the next note
116
00:08:42,480 --> 00:08:43,330
and so on.
117
00:08:43,410 --> 00:08:53,160
So node star and let's say create a list of numbers, OK, this will be our function and this function
118
00:08:53,160 --> 00:08:57,900
will not receive anything because why should it receive something?
119
00:08:58,560 --> 00:09:01,770
It will create a list and return it.
120
00:09:02,010 --> 00:09:10,050
OK, so now what we have to do is to create the how do so node start had and we created a pointer and
121
00:09:10,050 --> 00:09:12,310
these pointer name is had.
122
00:09:12,580 --> 00:09:18,930
OK, and what it will has to do is simply point always to keep the head of the linked list, because
123
00:09:19,290 --> 00:09:28,170
we in some situations, if we move this had, we will not be able to know what is the head of the linked
124
00:09:28,170 --> 00:09:28,540
list.
125
00:09:28,560 --> 00:09:37,080
So this hand will simply always point, always keep the hand of the linked list.
126
00:09:37,410 --> 00:09:42,330
OK, so we always know from what point the list starts.
127
00:09:42,360 --> 00:09:45,630
OK, so now let's use also additional variable.
128
00:09:45,630 --> 00:09:48,060
Additional points are current node.
129
00:09:48,060 --> 00:09:55,530
OK, so Curnoe is simply a variable that holds an address where resides some node.
130
00:09:55,630 --> 00:09:57,900
And I will explain you guys.
131
00:09:58,110 --> 00:09:58,770
Exactly.
132
00:09:59,480 --> 00:10:08,530
We will be using it and it's simply basically we will use different iterations and we will use like
133
00:10:09,730 --> 00:10:15,800
knowing where where the current a new element should be added to.
134
00:10:16,060 --> 00:10:20,740
So this current node will simply point to the last node we've been working with.
135
00:10:20,740 --> 00:10:24,350
And once we want to add a new node, we'll know, OK?
136
00:10:24,370 --> 00:10:29,410
So I don't have to iterate from the head to the last node that we have.
137
00:10:29,650 --> 00:10:35,920
But I know that the current node holds of the last element for this function, and I should add this
138
00:10:35,920 --> 00:10:40,570
new element from there on just to save some time and efficiency.
139
00:10:41,260 --> 00:10:41,890
All right.
140
00:10:41,890 --> 00:10:51,130
So now what we are going also to add is a an integer number in NUM, and this variable is simply where
141
00:10:51,130 --> 00:10:56,860
we are going to store each time the number received from the user, the actual data.
142
00:10:56,890 --> 00:11:02,020
So let's run some commands or print half, please.
143
00:11:02,050 --> 00:11:08,410
OK, please answer a number or minus one to you.
144
00:11:08,410 --> 00:11:10,360
Finish to finish the finish.
145
00:11:10,360 --> 00:11:10,810
To finish.
146
00:11:10,810 --> 00:11:11,920
To finish.
147
00:11:12,910 --> 00:11:17,830
OK, and once you do so, let's read this number using this kind of function.
148
00:11:17,840 --> 00:11:21,150
So Percentage D. and put it inside num.
149
00:11:21,680 --> 00:11:28,510
OK, and if num ok if the first number was equal to what was equal to minus one.
150
00:11:28,840 --> 00:11:35,740
So basically we shouldn't even iterate any further than simply return null.
151
00:11:36,010 --> 00:11:36,490
OK.
152
00:11:37,450 --> 00:11:38,050
OK.
153
00:11:38,080 --> 00:11:39,690
So very good guys.
154
00:11:39,700 --> 00:11:42,120
And now let's proceed.
155
00:11:42,130 --> 00:11:48,870
What we also have to add here is the hard OK, the first pointer equals two.
156
00:11:48,880 --> 00:11:58,600
OK, so what we do here is that we assume that num at this step, OK, we assume that the first number
157
00:11:59,740 --> 00:12:07,570
was some something legit number, OK, some some legit data, legit data, not minus one.
158
00:12:08,720 --> 00:12:12,970
OK, so if that's the case we need to create a node for that.
159
00:12:13,210 --> 00:12:16,360
And we know that the first node will be for the hand.
160
00:12:16,360 --> 00:12:25,280
So we know that we will allocate using some Marlock function and using the size of node.
161
00:12:25,300 --> 00:12:33,460
OK, so we allocating a new node, inputting the result, the address from the Molik function to had
162
00:12:33,460 --> 00:12:37,030
so had already points to a new created node.
163
00:12:37,180 --> 00:12:40,630
And we also will say that current node equals to had.
164
00:12:40,810 --> 00:12:48,160
So basically at this initial step it's outside the loop current node in hand will point to the first
165
00:12:49,000 --> 00:12:51,580
to the first created node.
166
00:12:51,610 --> 00:12:52,330
All right.
167
00:12:52,330 --> 00:12:56,320
So now let's use current node data.
168
00:12:56,380 --> 00:13:01,240
OK, current current node data equals two num.
169
00:13:01,240 --> 00:13:07,510
So simply, we know that previously we just allocated it, we allocated the memory, but it was not
170
00:13:07,510 --> 00:13:08,530
initialized in.
171
00:13:08,800 --> 00:13:14,110
The field of data will be initialized with the number num received from the user.
172
00:13:14,140 --> 00:13:16,370
OK, so that's pretty good.
173
00:13:16,390 --> 00:13:21,840
And now what we want to do is simply taking what do we want to take.
174
00:13:21,850 --> 00:13:25,240
We simply want to take additional value.
175
00:13:25,270 --> 00:13:26,820
OK, additional value.
176
00:13:26,830 --> 00:13:29,410
So let's just copy these two lines.
177
00:13:29,980 --> 00:13:31,750
Please enter a number will allow.
178
00:13:31,810 --> 00:13:32,920
OK, no problem.
179
00:13:33,440 --> 00:13:34,690
That's what we will do.
180
00:13:34,900 --> 00:13:45,580
And now we know that we at least have one node so we can iterate using while loop as long as num does
181
00:13:45,580 --> 00:13:53,560
not equals to minus one, as long as our condition, stubborn condition has not been met, as long as
182
00:13:53,560 --> 00:14:01,060
we haven't reached our stopping condition condition.
183
00:14:01,180 --> 00:14:07,330
So as long as these while loop happens to be true, then we are going to run its body.
184
00:14:07,330 --> 00:14:09,370
So its body says the following thing.
185
00:14:09,390 --> 00:14:17,220
So current node, current node is basically the last note that we are pointing to the field of next
186
00:14:17,250 --> 00:14:24,180
OK, which should point to the next node, which is currently just on initialized value.
187
00:14:24,190 --> 00:14:24,550
Right.
188
00:14:24,550 --> 00:14:26,910
We do not point anywhere from here.
189
00:14:27,970 --> 00:14:33,220
We should allocate a new OK because we received additional number here.
190
00:14:33,220 --> 00:14:36,400
We should allocate a new new node.
191
00:14:36,400 --> 00:14:37,720
So that's how we allocate.
192
00:14:37,720 --> 00:14:39,280
And, you know, let's copy that.
193
00:14:39,620 --> 00:14:41,860
OK, so we allocated a new node.
194
00:14:42,730 --> 00:14:47,260
And what we also want to do is to like to move forward the current node.
195
00:14:47,260 --> 00:14:55,120
So current node now points to the previous two one one before the last nodes that was just allocated
196
00:14:55,120 --> 00:14:55,480
here.
197
00:14:55,480 --> 00:14:58,240
So we allocated a new node.
198
00:14:58,240 --> 00:14:59,230
We know that the next.
199
00:14:59,300 --> 00:15:07,610
Field points to there, and we want current node to point to the last one, so current node dot next.
200
00:15:07,820 --> 00:15:09,590
OK, there you go.
201
00:15:09,740 --> 00:15:15,530
So current node at this point points to the last element, and that's awesome.
202
00:15:16,250 --> 00:15:23,960
What also we want to do is that we know that we created this new element is we know that we need to
203
00:15:23,960 --> 00:15:27,140
specify the current node data.
204
00:15:27,500 --> 00:15:30,710
OK, so the data should be equal to them, right?
205
00:15:30,770 --> 00:15:32,330
We created this new node.
206
00:15:32,450 --> 00:15:34,760
OK, well, first of all, we received some number.
207
00:15:34,880 --> 00:15:38,080
We made sure that this number does not equal to that.
208
00:15:38,090 --> 00:15:39,920
Not does not equal to minus one.
209
00:15:40,280 --> 00:15:44,720
We created the new node, added it to through the end of the linked list.
210
00:15:44,750 --> 00:15:51,560
OK, then we said let's move also current node to point to these new allocated on node.
211
00:15:51,840 --> 00:15:57,410
And also we will say that current node right now is the last is the new one.
212
00:15:57,500 --> 00:15:59,840
It's pointing, it's pointing to this one.
213
00:16:00,050 --> 00:16:03,120
Let's say that its data will be equal to now.
214
00:16:03,200 --> 00:16:03,460
Right.
215
00:16:03,470 --> 00:16:05,510
We fill it up pretty simple.
216
00:16:05,960 --> 00:16:09,080
And now we know that one iteration is over.
217
00:16:09,260 --> 00:16:12,600
Let's read the values for the next iteration.
218
00:16:12,620 --> 00:16:16,130
So print F, please enter.
219
00:16:16,500 --> 00:16:22,040
Enter a number or minus one to finish.
220
00:16:22,970 --> 00:16:23,560
OK, guys.
221
00:16:24,050 --> 00:16:28,730
So that's what we do when we read the value using this kind of function.
222
00:16:29,330 --> 00:16:32,510
So percentage num num num num.
223
00:16:32,630 --> 00:16:36,680
OK, and now we go again and again and again and again.
224
00:16:36,680 --> 00:16:40,360
We run over this loop adding nodes to the end of it.
225
00:16:40,610 --> 00:16:43,120
Keeping up with this simple analogy.
226
00:16:43,130 --> 00:16:50,930
My suggestion to you guys is take a piece of paper and just draw at least one, two cycles of how it's
227
00:16:50,930 --> 00:16:51,950
being edited.
228
00:16:52,130 --> 00:16:57,410
So create these first points or create this second pointer that you do not point anywhere.
229
00:16:57,650 --> 00:17:06,350
Then read one value from the user, run these logic behind the scene and kind of draw a new node created.
230
00:17:06,470 --> 00:17:11,630
Say that hand is now pointing through there and also current node pointing to their update.
231
00:17:11,630 --> 00:17:17,990
The data read the additional number, OK, field step by step to see the whole picture of what is going
232
00:17:17,990 --> 00:17:24,710
on behind the scenes and then run the logic of these while loop one after another, iteration after
233
00:17:24,710 --> 00:17:31,550
iteration and see how it looks like even, you know, like visually it will help you a lot.
234
00:17:31,550 --> 00:17:33,320
It will give you some practice.
235
00:17:33,320 --> 00:17:39,920
It will help you on your task at work, on your tasks, at work or even on your exams.
236
00:17:40,100 --> 00:17:44,630
It will reduce pressure and it will like don't jump right away to code.
237
00:17:44,630 --> 00:17:46,520
And don't say you understand everything.
238
00:17:46,580 --> 00:17:51,290
Try to like to draw a little bit and to proceed from there.
239
00:17:51,520 --> 00:17:56,960
OK, I know that a lot of a lot of you will not do it, but I still take my chances.
240
00:17:57,560 --> 00:18:06,170
So lastly, guys, we know that we have the current node and at some some point now is equal to minus
241
00:18:06,170 --> 00:18:06,470
one.
242
00:18:06,470 --> 00:18:11,660
And we leave these function for good and we leave this loop for good.
243
00:18:12,200 --> 00:18:22,460
And last thing that we know that the last created the last created node has some data, some data,
244
00:18:22,460 --> 00:18:26,740
and it also has some field of next, which is on initialized.
245
00:18:26,750 --> 00:18:36,500
And we want to indicate that the last node in the list and here and there is no other place we can move
246
00:18:36,500 --> 00:18:41,460
from this point on in, not to like make some memory access violation.
247
00:18:41,870 --> 00:18:45,350
So what we are going to do is to specify the current node point.
248
00:18:45,350 --> 00:18:46,880
Next point next.
249
00:18:46,910 --> 00:18:49,550
OK, that's the last node on the field.
250
00:18:49,550 --> 00:18:55,760
Next will be equal to now and this will simply let us know that it's the last element.
251
00:18:56,000 --> 00:19:03,080
And finally, what we will do is that we will return the hand because it points through the head of
252
00:19:03,080 --> 00:19:03,590
the list.
253
00:19:03,800 --> 00:19:11,780
And we know that this list can be navigated only in one direction and this navigation can only start
254
00:19:11,780 --> 00:19:12,740
from the head.
255
00:19:12,740 --> 00:19:15,500
So current node, thank you for helping us out.
256
00:19:15,740 --> 00:19:24,260
Now we need to return the head of the list, which is the address of the first node in this list.
257
00:19:24,260 --> 00:19:36,680
The address of the first node in this list are some guys who I think this was a really tough exercise,
258
00:19:36,680 --> 00:19:39,380
but I think it cover it up so much.
259
00:19:39,740 --> 00:19:48,650
The creation of a new linked list and returning a new linked list to whatever and whenever.
260
00:19:48,650 --> 00:19:53,510
We would like to use that probably in our future examples, we will also use it.
261
00:19:53,900 --> 00:19:57,950
So I think that's freaking awesome once again.
262
00:19:59,010 --> 00:20:07,800
If something is not clear, feel free to repeat this video, to like to view it one more time, to write
263
00:20:07,800 --> 00:20:12,330
your notes down, and if still you have any questions, feel free to ask them.
264
00:20:12,480 --> 00:20:18,260
In the frequently asked questions, maybe somebody asked a similar question before.
265
00:20:18,300 --> 00:20:19,190
Take a look there.
266
00:20:19,200 --> 00:20:26,670
And this will help you to learn more and also share your knowledge and your questions with other students.
267
00:20:27,190 --> 00:20:30,360
So, as always, guys, thank you so much for watching.
268
00:20:30,600 --> 00:20:35,420
That was an important video and I will see you in next videos.
269
00:20:35,430 --> 00:20:38,340
Have a great day and great practice.
25908
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.