Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, what is going on, guys?
And welcome back to another video where we are going to practice our skills using the linked lists.
So in this exercise, what we are required to do, what we are requested to do is to write a function
that creates a linked list.
OK, so the function should create create a linked list.
So basically saying the function should create this new list.
OK, these are the steps.
Create a new list, receive receive numbers as the input from the user and every received received number
should be added to the and to the end of the list.
That's basically the flow of how the function is going to work in this top condition.
The stopping condition, the stopping condition in this case for the list will be when the number is
minus one.
So would be when number equal is, let's say is minus one.
And finally, what the function should do is return the new created list.
So that's pretty much everything we have to do in this exercise.
So once again, I've went over the steps quickly, one by one.
Let's briefly discuss them once again.
So first of all, we need a function that will create a linked list.
OK, then one, the function.
We're rolling twice.
OK, so creating a linked list received no from numbers from the user input.
OK, and at every time you receive a number, create a new node and add this number to the end of the
list, at least note to the end of the list and do so.
Repeat this operation until a stopping condition of minus one when the number receives from the user
is minus one.
So until of this condition happens, keep on receiving numbers from the user and creating new nodes
and adding them to the end of the list.
Once the stopping condition is met, then in this case, return the address of the first node.
Return the new list.
OK, that's all that we have to do.
OK, so now let's talk about the solution.
So first of all, one more time.
Let's understand what we are being asked.
We have a function.
This function does not receive anything as an argument, OK, anything because we don't see in the question
anything similar to the phrase the function should receive and yada, yada, yada.
Right.
So we'll assume that we'll just have empty parentheses for this function.
OK, in the function creates a new list.
So this part we already know how to make.
OK, because we've seen in one of her previous examples how to create a new list.
I think we've seen it right.
If not, that's a problem.
We will see it right now.
So no worries, guys.
Then what we will have to do inside of the body of this function is simply to run some loop, OK, to
get inputs from the user and add them to the end of the list.
Any for everything has gone smoothly.
Then the function should return the new created list.
Then you aren't the first node in this list.
OK, so one of the questions that students like to ask me are here is why didn't we use an array to
solve these problem?
And actually, it's a very good question that I've been asked a lot.
How come that we've always used the raise for these types of questions?
Why should we bother to use linked lists?
Now, in the end, the answer for this question is very simple, because we do not know in advance what
is the size of the inputs that the user is going to give us.
That means we don't have any idea how many numbers we are going to receive from the user during these
process.
It may be 10 or it may be ten thousand.
So how can we create an array of just some fixed size if we don't know how many elements we are going
to get?
You got the idea, guys, and we know that a linked list is an easy way and easy structure that we can
use to expand.
We just have to add a new node.
And there you go.
You got your.
So some new element added to your data structure, you have expanded your linked list, but that is
not the case for a race, OK, although you can do something, something very tricky by using these
real function for a dynamic array.
But still, these operations are much less efficient.
So that's the reason why we will use linked lists for these question instead of our well known arrays.
So I hope that's clear to you guys.
It's very important not only to get some hands on, but also to understand what is going on behind the
scenes and how we can use it for our advantage and also to prove that our solution will be good enough
and also to be able to answer the question, why didn't you use something else?
OK, fantastic.
Let's start coding.
Right.
That's time to some coding.
And the first thing we are going to need for this question is defining a new type of structure.
Our.
OK, so let's define our node.
And we know that it can be that it can be the same node as we use it in.
Yeah, I think I think I added this video in previous examples.
So but if you're not not familiar with that.
So let's use typedef struct node.
OK, so we creating a new structure called node using these typedef which we already also discussed.
OK, and now we are going to fill in the fields.
OK, so the first field is going to be let's say into data and the second field.
OK, because we are talking about nodes in this linked list.
We know they're from one node.
We should be able to access the next node using some pointers.
So these pointer is going to be of struct node, right.
Because we are going to point to the next node in the next node is of type struct node.
So next is the name of the field which will contain and hold a pointer to the next node in the linked
list.
And also we are going to specify here, no the so that whenever we would like to create a new node,
we will know that we can use node and it will be struct node.
Awesome guys.
So now let's write the main function, not the main function, but the function that we are interested
in.
And we know that this function should return a linked list.
So the question is, should it return just node or should it return node star?
So take a second and think about it.
And once you are done thinking about it and think for yourself is why node is not enough because node
for some reasons it may be good.
OK, I don't think I will cover up in this video, but what we want to do is the return of the address.
OK, the address of the first element of this array, I'm not talking about all the details behind the
scenes because you we would have to talk about copying a value and by value and the copying or what
happens behind the scenes.
But what I want you to understand is that it's much better here to just return of the address of the
first node and then the main function would when we are going to receive this address, we are simply
going to get this address.
When we know that from this address on, we have our first node and we know how to access the next note
and so on.
So node star and let's say create a list of numbers, OK, this will be our function and this function
will not receive anything because why should it receive something?
It will create a list and return it.
OK, so now what we have to do is to create the how do so node start had and we created a pointer and
these pointer name is had.
OK, and what it will has to do is simply point always to keep the head of the linked list, because
we in some situations, if we move this had, we will not be able to know what is the head of the linked
list.
So this hand will simply always point, always keep the hand of the linked list.
OK, so we always know from what point the list starts.
OK, so now let's use also additional variable.
Additional points are current node.
OK, so Curnoe is simply a variable that holds an address where resides some node.
And I will explain you guys.
Exactly.
We will be using it and it's simply basically we will use different iterations and we will use like
knowing where where the current a new element should be added to.
So this current node will simply point to the last node we've been working with.
And once we want to add a new node, we'll know, OK?
So I don't have to iterate from the head to the last node that we have.
But I know that the current node holds of the last element for this function, and I should add this
new element from there on just to save some time and efficiency.
All right.
So now what we are going also to add is a an integer number in NUM, and this variable is simply where
we are going to store each time the number received from the user, the actual data.
So let's run some commands or print half, please.
OK, please answer a number or minus one to you.
Finish to finish the finish.
To finish.
To finish.
OK, and once you do so, let's read this number using this kind of function.
So Percentage D. and put it inside num.
OK, and if num ok if the first number was equal to what was equal to minus one.
So basically we shouldn't even iterate any further than simply return null.
OK.
OK.
So very good guys.
And now let's proceed.
What we also have to add here is the hard OK, the first pointer equals two.
OK, so what we do here is that we assume that num at this step, OK, we assume that the first number
was some something legit number, OK, some some legit data, legit data, not minus one.
OK, so if that's the case we need to create a node for that.
And we know that the first node will be for the hand.
So we know that we will allocate using some Marlock function and using the size of node.
OK, so we allocating a new node, inputting the result, the address from the Molik function to had
so had already points to a new created node.
And we also will say that current node equals to had.
So basically at this initial step it's outside the loop current node in hand will point to the first
to the first created node.
All right.
So now let's use current node data.
OK, current current node data equals two num.
So simply, we know that previously we just allocated it, we allocated the memory, but it was not
initialized in.
The field of data will be initialized with the number num received from the user.
OK, so that's pretty good.
And now what we want to do is simply taking what do we want to take.
We simply want to take additional value.
OK, additional value.
So let's just copy these two lines.
Please enter a number will allow.
OK, no problem.
That's what we will do.
And now we know that we at least have one node so we can iterate using while loop as long as num does
not equals to minus one, as long as our condition, stubborn condition has not been met, as long as
we haven't reached our stopping condition condition.
So as long as these while loop happens to be true, then we are going to run its body.
So its body says the following thing.
So current node, current node is basically the last note that we are pointing to the field of next
OK, which should point to the next node, which is currently just on initialized value.
Right.
We do not point anywhere from here.
We should allocate a new OK because we received additional number here.
We should allocate a new new node.
So that's how we allocate.
And, you know, let's copy that.
OK, so we allocated a new node.
And what we also want to do is to like to move forward the current node.
So current node now points to the previous two one one before the last nodes that was just allocated
here.
So we allocated a new node.
We know that the next.
Field points to there, and we want current node to point to the last one, so current node dot next.
OK, there you go.
So current node at this point points to the last element, and that's awesome.
What also we want to do is that we know that we created this new element is we know that we need to
specify the current node data.
OK, so the data should be equal to them, right?
We created this new node.
OK, well, first of all, we received some number.
We made sure that this number does not equal to that.
Not does not equal to minus one.
We created the new node, added it to through the end of the linked list.
OK, then we said let's move also current node to point to these new allocated on node.
And also we will say that current node right now is the last is the new one.
It's pointing, it's pointing to this one.
Let's say that its data will be equal to now.
Right.
We fill it up pretty simple.
And now we know that one iteration is over.
Let's read the values for the next iteration.
So print F, please enter.
Enter a number or minus one to finish.
OK, guys.
So that's what we do when we read the value using this kind of function.
So percentage num num num num.
OK, and now we go again and again and again and again.
We run over this loop adding nodes to the end of it.
Keeping up with this simple analogy.
My suggestion to you guys is take a piece of paper and just draw at least one, two cycles of how it's
being edited.
So create these first points or create this second pointer that you do not point anywhere.
Then read one value from the user, run these logic behind the scene and kind of draw a new node created.
Say that hand is now pointing through there and also current node pointing to their update.
The data read the additional number, OK, field step by step to see the whole picture of what is going
on behind the scenes and then run the logic of these while loop one after another, iteration after
iteration and see how it looks like even, you know, like visually it will help you a lot.
It will give you some practice.
It will help you on your task at work, on your tasks, at work or even on your exams.
It will reduce pressure and it will like don't jump right away to code.
And don't say you understand everything.
Try to like to draw a little bit and to proceed from there.
OK, I know that a lot of a lot of you will not do it, but I still take my chances.
So lastly, guys, we know that we have the current node and at some some point now is equal to minus
one.
And we leave these function for good and we leave this loop for good.
And last thing that we know that the last created the last created node has some data, some data,
and it also has some field of next, which is on initialized.
And we want to indicate that the last node in the list and here and there is no other place we can move
from this point on in, not to like make some memory access violation.
So what we are going to do is to specify the current node point.
Next point next.
OK, that's the last node on the field.
Next will be equal to now and this will simply let us know that it's the last element.
And finally, what we will do is that we will return the hand because it points through the head of
the list.
And we know that this list can be navigated only in one direction and this navigation can only start
from the head.
So current node, thank you for helping us out.
Now we need to return the head of the list, which is the address of the first node in this list.
The address of the first node in this list are some guys who I think this was a really tough exercise,
but I think it cover it up so much.
The creation of a new linked list and returning a new linked list to whatever and whenever.
We would like to use that probably in our future examples, we will also use it.
So I think that's freaking awesome once again.
If something is not clear, feel free to repeat this video, to like to view it one more time, to write
your notes down, and if still you have any questions, feel free to ask them.
In the frequently asked questions, maybe somebody asked a similar question before.
Take a look there.
And this will help you to learn more and also share your knowledge and your questions with other students.
So, as always, guys, thank you so much for watching.
That was an important video and I will see you in next videos.
Have a great day and great practice.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.