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
What is going on, guys, so in this video, what we are going to do is to write a function that counts
the number of nodes in a given linked list.
These functions should basically just receive, receive, receive a list, OK?
It should receive at least then you should count how many nodes are there in that list.
And finally, it should return these count.
Pretty simple, right?
Nothing complicated.
Just receive a list.
OK, get a list.
Count how many nodes there are in this particular list and return these count to whoever called these
function in.
The first thing to note here is that as opposed the two static arrays, for example, we cannot know
in advance what is the size of a linked list.
We simply usually have a pointer to the head of the list and that's it.
We don't have the exact number of nodes that it currently has because we said that we will mostly use
lists as our preferred data structure for tasks or where there are frequent changes in when we have
frequent changes, we cannot know the size of the of the linked list.
And also one question that I've been asked a lot is actually goes like this.
Why do we have to write a function?
Can't we just use size?
So that's one of the questions I've been asked.
So why do we have to write a dedicated function for that, something like that and dedicated function
for that?
Can't we just can't we just use size of OK, the size of operator and will tell us the exact size of
the linked list.
So actually we know that was linked list.
We work with pointers.
We have our head pointer pointing to the head of the list.
For example, we know we have a list with a couple of nodes and we want to find out how many nodes are
there in that list.
We simply can just use the size of because what should we put in the parentheses of this, of the size
of should we put just the hand if we do so, something like that.
So size of size of had we had a pointer, we will simply get the size of a single pointer had and obviously
not the size of the whole list, not even the size of the node itself.
It will be just the size of the pointer.
So I think that's a good idea.
Why size of won't work here.
The size of operator.
But if you still think something can be done with that, please try it out and let me know.
That's an important part of the starting process.
OK, so let's move on and write our a simple function.
OK, so there you go.
So first of all, what we created here is the template structure for the node in our linked list in
what we will do is just to write a function.
OK, so we know that the function returns.
What it returns, it returns.
The count right account account is simply an integer.
So the signature is going to be of an integer type and then we will specify the function names or for
example, count nodes in list.
OK, and what should the function receive?
The function should receive some some address that in this address we have the first node in this list.
So what we can do here is simply to use node star, OK, because we receive the address of the first
element and we have here node star had OK, for example, we're receiving the address of the first node
in the list and now what we want to do is to write the function body.
So for that we will create int count, OK, we will set it to be zero and this will be the count of
the nodes in this in the list.
So Count will represent the count of the nodes and also just for simplicity.
But it's not mandatory.
Let's create some additional pointer, call it let's call it temp, OK.
And we will set temp to point to head.
OK, so we'll steal.
Although it doesn't really matter in this function, we will still keep the head of the list pointing
to the head of the list and temp will simply iterate, kind of iterate and run over each in any of the
elements in this linked list.
So now what do we.
Have to do is to write a simple loop that will iterate over all the elements, and as long as we haven't
reached the last element, we will use count plus plus.
OK, so while in here will be some condition, as long as this condition is true, then count plus plus
and we will use temp equal to town equals to temp next.
OK, so every time we will take the 10 pointer and point to the next element in this linked list.
OK, so the main question that should be asked here is basically what should be the condition in these
while loop?
So if we want to say what will be the condition in just our words, we will say that the condition would
be as long as we haven't reached the end of the list.
OK, so as long as this is OK, we will run the body of this loop.
So while and how we will treat this part like haven't reached the end of the list.
So we know that every every, every node has a data and it has a next pointer.
And one thing that we know about lists is that the last element in the list should point.
These next field should point to null.
That's what specifies that the list ends in this given node, because in the standard ways, that's
how we find the length of the list.
OK, and that's how we find the end of the list.
So simply, as long as we can say that temp next, as long as the next temp does not equal to null.
OK, so if a given node that temp points to if it has a next field and this next field points to no
meaning, we don't have any further nodes, meaning we are at the end of the list.
And that's why we should like finish up with this condition.
The result of the condition should be false and we should be out of the loop in once we are out of this
loop.
We have been iterated.
We have iterated over all the elements of this linked list, count all of them, and we simply will
return the number that we have found.
So return count.
So int count counties of Taipei and the type the signature, everything seems to be OK.
So there you go, guys.
This is our function to count the number of nodes in a list.
So count number of nodes in a list.
Awesome.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.