OK, so in today's video, I'm going to go over the delete function and that was in the coding exercise
is a challenge for you all.
This was might have been a little bit difficult.
If you think a lot about the insert function, you're kind of able to infer how you might do this delete
function because it's a little bit similar, but sometimes it's hard to think about and figure it out
without getting, you know, a decent amount of errors if you got through it.
Definitely.
Congratulations to you.
Otherwise, don't worry about it.
I'm going to go ahead and go over it with some visuals and of course, the code as well right now.
So let's go ahead and get started.
I went in, went ahead and made a new project from sources.
So just starting off from our insert function that was here and I'm going to go ahead and just add the
delete function in between that one and print here.
First, I'm going to go over to my header file and just put the delete function here.
I'm actually just going to call it Del.
And we're going to have the same thing.
That can be a data value pass to it.
So go back to the TPP.
OK, so let's go ahead and copy this template so you can put that there.
It's going to be void list T and it's going to be.
OK.
So similar to our insert function and a pen, I'm going to start off with a pointer right away, but
it's actually just going to be to start at the head.
And it's kind of going to be like similar to occur where we were using that as like an iterator, kind
of.
So I'm going to just call this del node.
So it's the node that we want to delete from a set of equal to the head just right off the bat.
So we start with that.
And first off, I'm going to do the situation in which the list is empty, which we don't really have
to do anything.
So we're just going to say like that if the list is empty, we're just going to do your check to make
sure that we have size is equal to zero.
And we're just going to honestly return and not do anything else, if that's the case.
So actually, I don't really need these, then.
And there's a second case where if the size is one,
then all we really need to do is check to see if the data value and this is the type of delete we're
doing.
If if that that wasn't clear yet, we are getting past this data value and we're trying to find that
data value and delete the node that is associated with that data value, the node that has that data
value.
So I'm going to have to go ahead and check here to see if, in fact, this singular note that we have
in the list has the same data we're looking for.
So I'm going to say it's just going to be the head somewhere safe since downloads equal to the head.
I'm going to see if downloads data is equal to the data value that is passed.
If that is the case, then we're just going to say that head and tail on are going to be null pointer.
We're going to decrease the size by one.
So I'm going to say this one for size and then we're going to delete the download, which is pointing
to head, which is going to be the only thing that's in our list.
OK, so the next thing is or else which is going to be kind of similar, like I said, to insert just
a little bit different.
I'm going to say I already have the download pointer, so I'm not going to declare some new pointer
to traverse that linked list.
I'm going to go ahead and just start with that wow loop.
And I'm going to say, well, download not equal to, you know, pointer.
And then we're going to have to look at the different cases of deleting.
We know that we only want to delete if we find a situation where download data is equal to the data
value that's being passed.
So let's go ahead and just start that because we know we want that.
So I'm going to say, if downloads data is equal to you data well and then we're going to have to put
some conditions inside of here.
So I have some more drawings similar to when we did insert.
So we have a few conditions here.
These two men will look at first.
So we have the situation in which we're at the tail.
And so if we're at the tail, we're going to need to have tails, previous, become the new tail once
we delete this.
And then we're also going to want to set that knows that new tail, we're going to want to say this
next point or to know because now it's supposed to be what tail was pointing to, which was all so pretty
simple.
And then for head, it's the opposite kind of thing.
So if we're deleting the head and we want to have heads next, it's previous.
Should be pointing to no.
And this next is, are you already going to be pointing to the next node?
So I actually shouldn't really have this here.
I should probably go ahead and delete that.
Same here.
I left this note here, so that's actually not going to be.
True, necessarily, unless there is just that one.
Item, in fact, that wouldn't be true, this would just be nil anyways, so yeah, I mean, it could
just have two items like this, but.
Yeah, that's not that's that's not necessarily pointing to, no, so I'm going to go ahead and have
that not be there.
But yeah, this is pretty easy.
We're going to update this previous to Noel.
And so these two things are pretty similar to just this one's opposite.
So let's go ahead and do those two first before I talk about the next case.
So that is going to be one situation is whether downloads, notes previous is equal to, you know,
pointer.
And that is the case in which no, in which the node that we're on is going to be the head.
So I'll go ahead and say that.
I say if the node is guilty and and then we will go ahead and add in our head equals head arrow next.
So we're making had become the thing to it's right.
And then we're going to say that its previous is going to be no point now.
So just going back to the drawing to make that clear, we are deleting the head here.
So we updated head to head Arrow next.
So now this is head.
And then we said that its previous is now equal to no pointer since it's that.
OK, so that should be good for that first case, the next case will be if the node is the tail.
And for that, I will be saying LCF down node arrow next is equal to null pointer.
And in that case, we are going to.
Have the new tale become tales previous and that new tales next needs to get set to know?
So let's go ahead and do that.
I'm going to say tail equals tail arrow three and then I'm going to say that tails next.
This is a new tale now, and I'm saying it's next is going to be able to, you know, pointers set to
no point here.
OK, so now we come up on the final case, which is if the node is somewhere in the middle.
I'm just calling that the middle, but anywhere in between.
So that's going to be an else.
And let's take a look at what that's going to look like.
So if I scroll up here, if we delete something in between, we pretty much need to change both of these
pointers.
So I'm referring to this is still next and download for you.
This is the note that we want to delete.
So what did you notice on our little iterator style pointer?
So we're going to have to set this proved that Dell knows produce of downloads next is going to get
downloads next three is going to get set to download preve and downloads.
Preuves next is going to be set to downloads next and then we'll delete the note.
So let's go ahead and implement that.
So I'm going to say downloads next preve.
And so that's this one right here.
Downloads next preve, and we're going to set it equal to this.
Downloads preve.
So let's go ahead and do that, I'm going to say that setting or setting it to download Creek and then
the opposite.
I'm going to say downloads free next.
It's going to be set to download next.
So let's just go back just to make this really clear.
So downloads.
Previous Next.
If getting set to download next.
So this was going to be in the parentheses.
And then this is going to be definitely a previous next.
And then Frances, and this will you.
That's what's inserted in there.
So we already have that.
So let's put download next in here.
OK.
And then so I didn't add the changes to the size and the actual deletion in each one of these because
we know that if this is the case.
That the data matches we are going to delete no matter what.
So I'm just going to put one instance of this, so it's going to do these things based on these kind
of sub conditions in here.
And then regardless which one of these gets executed with whichever one we jump into the scope of,
we're going to still down here do the same change of the size.
So size minus equals one and then the deletion of download.
So I'm going to say delete, download.
Because we know that like if this is the case where the data is equal, we're going to for sure want
to delete it because we found it.
And at that point, we can just return actually.
So I'm going to say return, and then we need to put our thing that drives the while loop to traverse
the list doesn't try the while loop, it drives our traversal.
So I'm going to say download equals download arrow next.
Just like our curve equals Kerr Arrow next to us up here.
OK, so that should be good, and we should now write some code that will actually test that this works
just like we always do.
So I'm going to head back to Maine.
And if everything else?
It's good.
Hopefully, we will be able to see some decent results.
So in Maine here, I'm just going to add a couple of things.
I'm going to delete a couple of values first that I know are in my list just singularly and then print
it out so I can verify that.
So we just have one through eight in here out of order, because previously we're making that order
link list with the insert function.
And so since this won't do, I'm just going to say, let's see out that we are deleting the value five
and I did not spell delete deleting the value five.
And that's been in line.
And so I'm going to say Link to list what's Dell and then I'm just going to put five in here and then,
let's say, also put the lean evaluate until it from the end.
The line, and then I'll say, let's go ahead and delete like the first value will be won so we can
delete that.
So I'll put let's put a we'll delete these two and then I'll print this out.
So let's do a linked list print.
And then after this, let's go ahead and just say we need value one which.
And then we will do
delete of the value one.
And then after that, yeah, let's go ahead and print that.
After that, we'll go ahead and delete the rest, so I'm actually just going to make a for loop.
There's not going to be this many items in there, but I'm just going to say
I'm just going to say actually that it's just going to go from zero to seven.
So I'll do like zero.
That value is not going to be in there, but I'll go ahead and just write this for a loop.
If we don't find something to delete, then it just basically doesn't do anything and I delete function.
So.
That should be fine.
So I have.
Plus plus A. equals zero I less than eight plus plus.
And then let's just say the list to Levi, and then we'll print it after this to verify that it just
says forwards and backwards.
What does it say with nothing in our link list being printed, such as blank lines there?
So I'm going to say only first print and we shouldn't be seeing anything in there.
OK, so if everything is all good, we should see that it removes the value five and eight versus a
burner phone link list in order one through eight.
Then it should delete five and eight.
It should be the last value and five would be somewhere in the middle there.
So we should notice that when we print it here.
Then you should delete the first value and we can verify that it did that.
By printing right here, and then we'll just delete the rest and verify that everything is working.
So that's how we're going to test this.
Let's go ahead and run.
OK, so we have a problem.
This.
No, oh, yeah, it's like I forgot and l hear my bad.
OK, let's go ahead and do this again.
All right.
OK, so we see and fortunately, it did not delete.
The eight.
Leading up is that because.
Deleting the value eight, I never actually deleted that.
So.
That would be a problem.
So let's go ahead and.
Do that because it just went up to seven in our loop and it didn't delete it, so it looks like stuff
like worked pretty well, but let's go ahead and actually have this.
So it's doing what we want it to do.
OK, so that we're seeing an empty list, so let's go ahead and just verify everything so forwards and
backwards, we have one through eight.
Still leaves the value five deletes evaluate.
We have one two three four No.
Five six seven No.
Eight.
And then we delete the value one, so it starts at two two three four five six seven eight.
And then we delete the rest.
And we have a blank right here.
So it looks like it works pretty well.
So hopefully that explained it enough for you and go back to these drawings here, and yeah, I will
try and include this as some material that you can refer back to.
Hopefully this was explanatory enough for these three different operations that we had in our else.
OK.
So congratulations, if you were able to figure this out in the coding exercise, if not, don't worry.
This is kind of a complicated operation, so hopefully you were able to figure it out now after this
video than that, I will see you in the next lecture.