Hello and welcome back, everyone.
In this lecture we're going to implement no peak and other essential node functions.
So let's create a new file called Nodes C and we're just going to include compile H, we're going to
include assert H and we're going to include our vector.
Okay.
So what we're going to do now is we're going to say struct vector, no vector equals no struct vector
no, vector root equals no.
And we're just going to go void node cep vector struct vector vec struc vector root vec and we're just
going to go no vector equals vec, no vector root equals root vec.
So essentially our functions such as node PARP, no peak and stuff like that, we're not going to pass
vectors into them.
Okay.
They'll just rely on these vectors here, which we set with node set vector.
So someone that wants to use vector functionality, they'll call node set vector to set the the vector
and the root vector.
Right.
And then they can just do things like no peak, no push, and it will use the vectors they set when
they call this function.
So that's the design idea.
So here we're going to say void, no push.
We're making a push function now and we're just going to go vector push, no vector ampersand node.
So what is this?
No push function do what a push is or inserts are node to the end of the vector.
So that's the idea with that so I can think of a push is like an insert to the back of a queue type
of thing, right.
So the back of a vector.
So now we're going to say struct node, node, peak or null and that's going to return vector back pointer
or null.
And we're just going to put in the node vector there.
So that will essentially grab from the back of the vector ack.
And if there's nothing there on the, on the end of the vector, it'll return null.
So if we just go into this function back pointer or null means that we should access it as a pointer
before returning the value.
So if we have pointers stored on the vector, that's that, that's the type of function we call to resolve
the actual pointer that's been stored because otherwise it'll just return a pointer to the element in
the actual vector array.
Right.
Whereas we want the actual pointer, the pointer value.
Yeah.
So that's why we call vector back pointer on null because it will bring us the actual struc node rather
than the struct node double pointer.
If we just did vector back, it will bring us back one of those and would have to go into it once.
Right.
But if we call this, it'll actually go in there and it will do that for us automatically.
So we only get a struc node pointer, not a struck node double pointer.
Makes sense.
Nice.
So back to node C.
And we're going to continue writing these functions.
So we're going to go straight note no peak return.
And I'll tell you what I'm going to do.
Rather than use the actual vector functions now I'm going to do this manually just so I can explain
a bit more what I just described.
So we're going to go asterisk, struct node, asterisk, asterisk, vector back, no vector.
So that's basically what happens when we do a vector, a vector back pointer.
So we do vector back, which gives us a double pointer, and then we go into once to give us the struc
node.
So we're now going to implement node pop.
So we're just going to go struct node, no pop.
So pop basically removes from the back of the vector, it pops pops the element off.
So we're going to say struct node last node equals vector back pointer, no vector.
And if you are bit confused about these pointers, right, like why are we using back pointer instead
of vector back.
Just take a little while to study this source code because it will make sense to you.
Simply put, vector back will not give us the actual pointer.
It'll only give us a position in the vector data array.
It'll only give us a pointer to that.
So by doing back pointer, it will actually go in there and pull back the pointer value.
So that's important to mention.
So then we're just going to instruct node last node root equals vector empty, no vector.
Question mark.
Now we're doing a ternary here, guys.
Vector back pointer, no vector root.
So this basically says is the node vector null.
And if the node vectors null, then we'll just set this root to null.
Otherwise, we'll get the last element on the node vector root.
That's what that says.
And then we're just going to say vector pop.
No vector.
And we're also going to say if last node is equal to last node root, then vector pop.
No vector roots.
And then we're just going to return the last node.
So essentially, we don't want to leave duplicates on the the root vector and the normal vector.
So you can think of the root vector is like the tree vector, right?
So yeah, the root vector is basically the tree vector I discussed earlier.
So we get the last node that's on the node vector.
If the node vector is not empty, then we also get the last node on the root OC and then we pop off
the the last node here from the node vector of course.
And then we just check.
We just check.
Okay, it's the last node equal to the last node root and if it is, then we pop from the root vector
as well.
This ensures we don't leave any duplicate nodes on the tree vector by doing this final check here.
So these are the most generic node functions we need to worry about.
We will add to this file as we go along.
Let's just add this file to the Makefile now.
So just buy here and a copy and paste paths that I know we're going to put.
Node Dodo Node dot C No.
Oh, no.
Dot zero.
And.
Just after that, we're going to go build Noto.
Perfect.
And now we need a terminal to test this.
So we're just going to go make clean, make, and we can see how that compiles.
Okay.
If you run it, we are stuck in an infinite loop.
Don't worry, it's because impossible.
See, we're not actually doing anything with paths next yet.
Okay.
So they are the most important node functions we'll be using.