Hi, everyone.
So in this video, what we will do, we will implement the remove function and get value function.
So now first let's discuss the remove function.
So you are given a key and you want to remove that key.
So what is the procedure?
So procedure is very simple.
First of all, this key will pass through a hash function.
So get index function, get a good index function and that will return me the bucket index and what
we'll do.
We will go to that index.
So this is our bucket, Buckethead.
Now we will reach this index, so this index, what we have, we have a long list.
Now, suppose you want to delete ABC, the key given is ABC.
So you want to delete ABC.
So after reading this index, what will do?
We will reach the pointer then, let's say.
My ABC is present here.
So what we have to do, we will iterate over this link list and we will compare the keys.
So this ABC, we have found the key, which is to be deleted.
So after finding out the key that we have to delete what we will do.
So we have to delete this node.
So for deleting this node, we know we have to take a previous pointer.
So this is previous pointer and this is current pointer.
And we will do something like this.
So basically, we will do the next of previous is actually the next of current.
So we have to do something like this.
So this link will make.
So now let's write the code for the more function.
So this is my remote function, it is taking as input, so we will copy some gold.
So first of all, we have to do we have to find the bucket index.
Then Waterloo, so we need the head of the linked list.
So after finding out the head of the linked list, we need one more pointer, previous pointer so that
we can delete.
So we need previous pointer and this previous point is initialing than simple.
Then what we have to do, we have to iterate over the linked list, so now let us say teetotaling list.
So, Heracles.
Next offered, and we also have to update our previous point.
So what we will do, we will first rate privacy equals head and then our head will move ahead.
Simple.
So inside this, when we are dealing with an analyst, what we have to do, so we have to check.
So if headachy.
Is it close to the key that we have to remove?
Then what we will do so at this point, so this is my head instead of current time using the variable
head.
So if Hedgie, which is ABC, is it close to the give?
And then we have to delete this.
And so basically we have to write this statement, this line.
So what will do so previous.
Euronext is actually.
Head next.
Simple, but there is a problem with this and it is the problem.
So problem is very simple.
So let's see.
So this is your budgetary.
This is your budget index, BMI is your baggage index, and then you have the list.
Now, suppose you want to delete the ABC and ABC is present at the head only, so this is the head and
ABC's president at the head only then they have to do so.
We have to delete this node and we will update.
This one, so at this point, what is the coalition so devious is actually Linell previs is actually.
So if previous horsnell what we have to do, so we have to update our Buchard index.
So we will we will link this node and this will be our new link, so let's do this so we can we cannot
write like this.
So we have a condition.
So if previous is null, so that means I have to delete the hardpoint, but only the first node, so
for the leading the first node, what do we have to update our baggage index?
So what will happen next?
So it will be.
Next offered.
Simple.
And in the part.
We can do something like this.
So if previous Horsnell.
If privacy, then you have to update your bucket index, in the other case, you can do like this simple.
Now, what we have to do, so we have to remove so far removing.
So what do we have here, so I have.
So this is my previous pointed.
And this is my head, basically the current pointer, and this is the next node, so initially this
is a situation now of writing the code.
This is the situation.
So what I have to do, I have to delete this node.
So I will write, delete my current node.
But if you will write like this, if you will write delete, then what will happen?
So this whole linked list.
So this whole list will get destroyed because of this link.
Because of this link and and we have written our district cursive, so our district cursive.
So what will happen, if you will write simple like this, delete current, then this complete link
list will get destroyed.
So what we have to do, so we have to make we have to remove this pointer.
So for removing this point, what we will do so we will first write current error.
Next is null.
So first, we will isolate this node.
It will point towards.
No, this note is isolated and then we can delete this old simple, so let's do this.
So we have to do we have to.
Believed had actually been to Canada directly because if they had, then the rest of the English, which
is present in front of it, will get destroyed because our delete function, our district is because
of.
So first you have to isolate that node.
And what is the way to isolating that node will make the next point only.
Now, this is correct, but if we will see the return, the function is.
So basically first we have to store the value before deleting.
So before deleting what we will do, we will store our value.
And what is the value?
So value is held at a value.
Simple, now, one more thing.
OK, so we have to return also, so delete and then you will return your values or return value.
So I'm starting I am returning the saved value.
Now, one more thing, since you are deleting a..
What they have to do, you have to do minus minus simple.
Now everything seems fine, but what if.
I reach the end of the loop.
So what if I reach at one zero two, so that means the given guy is not present.
So if the giving is not present in the list, but we have to write down something, we have to retain
value.
So let's return a default value and let's say I am returning zero.
So zero is basically the default value.
So zero signifies here that the giving is not present.
So if the value is integer, I'm going to return zero and if B is a pointer, then zero signifies null.
I'm returning to return zero means the giving is not present.
So this is the computer called.
So in this remote function, we have to handle some cases, for example, first we have to check if.
That means you have to delete the first, not only you have to delete this first.
And not only.
So that's why you did something like this.
OK, so that's why you have the right bucket buckets of bucket indexes next of it, so next of this
is head.
Again, so when you're deleting the head, so first you have to make the head next channel so that the
rest of the list present in front of it do not get deleted.
So if you like deleted, then what will happen?
So our function is our district, basically.
So the rest of the list presented in front of it will also get destroyed.
So that's why I had that on external so that we can isolate that node.
But before deleting that note, I have to store the value because I have to return the value.
So I'm returning the value.
And we also have to do count minus minus and if we are not able to find the key.
So if the key is not present in this linked list, then I am returning zero.
So this is the remove function.
Now let's also write the search function, basically the get value function.
So there get value.
OK, so this is the great value function.
So again, we will copy the same code.
So we just have to find a value.
So for finding the value.
Again, so given a key ABC, you have to find the value for ABC.
Again, we have this key will pass through the hash function, basically get back index function.
It will it will return the bucket index.
I will go to that index in the area in my bucket.
Very.
And in this market index, what do we have, I have a long list so far finding out the value for the
given key.
What I will do, I will iterate over this linked list.
So suppose ABC's present here and I will just hold the value the title.
So I will compare ABC.
But this ABC, if you're able to find out the key you will return to and if you read the complete link
list, but you are not able to find out the given key.
So that means the given key is not present and then you can return a default value.
Simple.
So let us also find out they had.
Now, what you have to do, we have to it the linked list to find out the key.
So while it is not a questionable.
So how ridiculous next of Ed.
Simple and you have to write if condition here.
So if hedgie.
Is it goes to the given guy, so that means.
You find out you found the Gunji and now what we have to do, so basically you have to return the value.
So we just have to return the value.
We do not have to do anything.
So you can directly return.
Head of.
Simple, now, if you will reach if you will come out of the loop, it will come out this way loop.
So that means if you will come out this way loop, that means the given key is not present in my map.
So if the given key is not present, so I can return a default value and let's say zero is the default
value.
So return zero means the given keys not present.
So this is the computer get value function, so get value function, this very simple, this function
is a little bit tougher, then get value function to this function is a little bit less function, a
little bit more complex, then get value function.
Now, in the next video, we will discuss their time, complexity of our hash map I will see in the
next one by.