1 00:00:01,600 --> 00:00:02,350 Hi, everyone. 2 00:00:02,380 --> 00:00:08,109 So in this video, what we will do, we will implement the remove function and get value function. 3 00:00:08,770 --> 00:00:11,820 So now first let's discuss the remove function. 4 00:00:12,400 --> 00:00:15,370 So you are given a key and you want to remove that key. 5 00:00:15,730 --> 00:00:16,730 So what is the procedure? 6 00:00:16,750 --> 00:00:18,070 So procedure is very simple. 7 00:00:18,640 --> 00:00:21,370 First of all, this key will pass through a hash function. 8 00:00:22,030 --> 00:00:28,660 So get index function, get a good index function and that will return me the bucket index and what 9 00:00:28,660 --> 00:00:29,000 we'll do. 10 00:00:29,020 --> 00:00:30,190 We will go to that index. 11 00:00:30,220 --> 00:00:32,380 So this is our bucket, Buckethead. 12 00:00:33,660 --> 00:00:38,640 Now we will reach this index, so this index, what we have, we have a long list. 13 00:00:45,900 --> 00:00:51,100 Now, suppose you want to delete ABC, the key given is ABC. 14 00:00:51,450 --> 00:00:52,790 So you want to delete ABC. 15 00:00:53,040 --> 00:00:55,540 So after reading this index, what will do? 16 00:00:55,620 --> 00:00:58,590 We will reach the pointer then, let's say. 17 00:01:00,840 --> 00:01:02,560 My ABC is present here. 18 00:01:03,030 --> 00:01:08,640 So what we have to do, we will iterate over this link list and we will compare the keys. 19 00:01:08,670 --> 00:01:12,980 So this ABC, we have found the key, which is to be deleted. 20 00:01:13,350 --> 00:01:17,330 So after finding out the key that we have to delete what we will do. 21 00:01:17,640 --> 00:01:19,890 So we have to delete this node. 22 00:01:20,070 --> 00:01:23,320 So for deleting this node, we know we have to take a previous pointer. 23 00:01:23,730 --> 00:01:26,640 So this is previous pointer and this is current pointer. 24 00:01:27,120 --> 00:01:28,800 And we will do something like this. 25 00:01:30,620 --> 00:01:37,130 So basically, we will do the next of previous is actually the next of current. 26 00:01:39,110 --> 00:01:40,940 So we have to do something like this. 27 00:01:42,720 --> 00:01:44,310 So this link will make. 28 00:01:45,950 --> 00:01:48,470 So now let's write the code for the more function. 29 00:01:50,770 --> 00:01:56,620 So this is my remote function, it is taking as input, so we will copy some gold. 30 00:01:57,010 --> 00:02:00,310 So first of all, we have to do we have to find the bucket index. 31 00:02:04,330 --> 00:02:07,060 Then Waterloo, so we need the head of the linked list. 32 00:02:10,220 --> 00:02:15,500 So after finding out the head of the linked list, we need one more pointer, previous pointer so that 33 00:02:15,500 --> 00:02:16,190 we can delete. 34 00:02:19,250 --> 00:02:23,900 So we need previous pointer and this previous point is initialing than simple. 35 00:02:25,110 --> 00:02:31,470 Then what we have to do, we have to iterate over the linked list, so now let us say teetotaling list. 36 00:02:36,010 --> 00:02:37,090 So, Heracles. 37 00:02:40,300 --> 00:02:44,320 Next offered, and we also have to update our previous point. 38 00:02:44,860 --> 00:02:50,160 So what we will do, we will first rate privacy equals head and then our head will move ahead. 39 00:02:50,290 --> 00:02:50,860 Simple. 40 00:02:52,400 --> 00:02:57,740 So inside this, when we are dealing with an analyst, what we have to do, so we have to check. 41 00:02:59,360 --> 00:03:00,740 So if headachy. 42 00:03:02,090 --> 00:03:04,430 Is it close to the key that we have to remove? 43 00:03:05,940 --> 00:03:12,960 Then what we will do so at this point, so this is my head instead of current time using the variable 44 00:03:12,960 --> 00:03:13,160 head. 45 00:03:13,570 --> 00:03:17,630 So if Hedgie, which is ABC, is it close to the give? 46 00:03:17,640 --> 00:03:19,170 And then we have to delete this. 47 00:03:19,180 --> 00:03:22,850 And so basically we have to write this statement, this line. 48 00:03:24,570 --> 00:03:27,150 So what will do so previous. 49 00:03:28,680 --> 00:03:30,480 Euronext is actually. 50 00:03:33,780 --> 00:03:34,520 Head next. 51 00:03:36,210 --> 00:03:40,250 Simple, but there is a problem with this and it is the problem. 52 00:03:41,930 --> 00:03:43,310 So problem is very simple. 53 00:03:45,030 --> 00:03:45,900 So let's see. 54 00:03:47,480 --> 00:03:49,040 So this is your budgetary. 55 00:03:53,260 --> 00:03:58,660 This is your budget index, BMI is your baggage index, and then you have the list. 56 00:04:02,900 --> 00:04:10,700 Now, suppose you want to delete the ABC and ABC is present at the head only, so this is the head and 57 00:04:10,700 --> 00:04:14,210 ABC's president at the head only then they have to do so. 58 00:04:14,210 --> 00:04:17,899 We have to delete this node and we will update. 59 00:04:18,970 --> 00:04:26,260 This one, so at this point, what is the coalition so devious is actually Linell previs is actually. 60 00:04:26,680 --> 00:04:32,170 So if previous horsnell what we have to do, so we have to update our Buchard index. 61 00:04:33,270 --> 00:04:42,060 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 62 00:04:42,060 --> 00:04:43,080 write like this. 63 00:04:43,090 --> 00:04:44,700 So we have a condition. 64 00:04:52,620 --> 00:05:00,120 So if previous is null, so that means I have to delete the hardpoint, but only the first node, so 65 00:05:00,120 --> 00:05:04,740 for the leading the first node, what do we have to update our baggage index? 66 00:05:07,210 --> 00:05:08,590 So what will happen next? 67 00:05:08,620 --> 00:05:10,270 So it will be. 68 00:05:11,890 --> 00:05:12,760 Next offered. 69 00:05:14,810 --> 00:05:15,350 Simple. 70 00:05:16,420 --> 00:05:17,950 And in the part. 71 00:05:19,630 --> 00:05:21,160 We can do something like this. 72 00:05:26,830 --> 00:05:28,150 So if previous Horsnell. 73 00:05:30,400 --> 00:05:37,660 If privacy, then you have to update your bucket index, in the other case, you can do like this simple. 74 00:05:38,730 --> 00:05:42,270 Now, what we have to do, so we have to remove so far removing. 75 00:05:45,150 --> 00:05:47,650 So what do we have here, so I have. 76 00:05:47,670 --> 00:05:49,230 So this is my previous pointed. 77 00:05:52,120 --> 00:05:57,460 And this is my head, basically the current pointer, and this is the next node, so initially this 78 00:05:57,460 --> 00:05:59,570 is a situation now of writing the code. 79 00:05:59,590 --> 00:06:00,540 This is the situation. 80 00:06:01,090 --> 00:06:03,880 So what I have to do, I have to delete this node. 81 00:06:04,060 --> 00:06:07,930 So I will write, delete my current node. 82 00:06:08,500 --> 00:06:12,860 But if you will write like this, if you will write delete, then what will happen? 83 00:06:13,180 --> 00:06:14,500 So this whole linked list. 84 00:06:16,990 --> 00:06:20,410 So this whole list will get destroyed because of this link. 85 00:06:21,370 --> 00:06:27,990 Because of this link and and we have written our district cursive, so our district cursive. 86 00:06:28,030 --> 00:06:32,920 So what will happen, if you will write simple like this, delete current, then this complete link 87 00:06:32,920 --> 00:06:34,840 list will get destroyed. 88 00:06:35,410 --> 00:06:39,010 So what we have to do, so we have to make we have to remove this pointer. 89 00:06:39,430 --> 00:06:44,830 So for removing this point, what we will do so we will first write current error. 90 00:06:44,830 --> 00:06:45,670 Next is null. 91 00:06:46,880 --> 00:06:48,710 So first, we will isolate this node. 92 00:06:49,220 --> 00:06:50,280 It will point towards. 93 00:06:51,920 --> 00:06:58,610 No, this note is isolated and then we can delete this old simple, so let's do this. 94 00:07:01,780 --> 00:07:03,310 So we have to do we have to. 95 00:07:06,360 --> 00:07:12,360 Believed had actually been to Canada directly because if they had, then the rest of the English, which 96 00:07:12,360 --> 00:07:17,130 is present in front of it, will get destroyed because our delete function, our district is because 97 00:07:17,130 --> 00:07:17,290 of. 98 00:07:18,150 --> 00:07:20,370 So first you have to isolate that node. 99 00:07:20,760 --> 00:07:25,070 And what is the way to isolating that node will make the next point only. 100 00:07:26,130 --> 00:07:29,760 Now, this is correct, but if we will see the return, the function is. 101 00:07:30,450 --> 00:07:33,140 So basically first we have to store the value before deleting. 102 00:07:33,780 --> 00:07:37,190 So before deleting what we will do, we will store our value. 103 00:07:37,620 --> 00:07:38,560 And what is the value? 104 00:07:38,580 --> 00:07:40,260 So value is held at a value. 105 00:07:43,750 --> 00:07:45,890 Simple, now, one more thing. 106 00:07:46,390 --> 00:07:53,320 OK, so we have to return also, so delete and then you will return your values or return value. 107 00:07:54,630 --> 00:07:57,660 So I'm starting I am returning the saved value. 108 00:07:58,260 --> 00:08:00,950 Now, one more thing, since you are deleting a.. 109 00:08:01,230 --> 00:08:05,100 What they have to do, you have to do minus minus simple. 110 00:08:06,770 --> 00:08:10,040 Now everything seems fine, but what if. 111 00:08:11,080 --> 00:08:13,270 I reach the end of the loop. 112 00:08:14,580 --> 00:08:19,870 So what if I reach at one zero two, so that means the given guy is not present. 113 00:08:20,460 --> 00:08:25,780 So if the giving is not present in the list, but we have to write down something, we have to retain 114 00:08:25,820 --> 00:08:26,180 value. 115 00:08:26,460 --> 00:08:30,120 So let's return a default value and let's say I am returning zero. 116 00:08:30,690 --> 00:08:33,150 So zero is basically the default value. 117 00:08:33,150 --> 00:08:36,460 So zero signifies here that the giving is not present. 118 00:08:36,809 --> 00:08:42,970 So if the value is integer, I'm going to return zero and if B is a pointer, then zero signifies null. 119 00:08:43,000 --> 00:08:47,580 I'm returning to return zero means the giving is not present. 120 00:08:48,860 --> 00:08:50,060 So this is the computer called. 121 00:08:51,260 --> 00:08:57,560 So in this remote function, we have to handle some cases, for example, first we have to check if. 122 00:08:58,220 --> 00:09:03,980 That means you have to delete the first, not only you have to delete this first. 123 00:09:03,980 --> 00:09:04,640 And not only. 124 00:09:05,090 --> 00:09:07,190 So that's why you did something like this. 125 00:09:09,610 --> 00:09:16,000 OK, so that's why you have the right bucket buckets of bucket indexes next of it, so next of this 126 00:09:16,000 --> 00:09:16,420 is head. 127 00:09:17,870 --> 00:09:24,530 Again, so when you're deleting the head, so first you have to make the head next channel so that the 128 00:09:24,530 --> 00:09:28,440 rest of the list present in front of it do not get deleted. 129 00:09:28,910 --> 00:09:31,380 So if you like deleted, then what will happen? 130 00:09:31,760 --> 00:09:34,110 So our function is our district, basically. 131 00:09:34,250 --> 00:09:38,040 So the rest of the list presented in front of it will also get destroyed. 132 00:09:38,270 --> 00:09:42,470 So that's why I had that on external so that we can isolate that node. 133 00:09:42,680 --> 00:09:46,750 But before deleting that note, I have to store the value because I have to return the value. 134 00:09:46,940 --> 00:09:48,040 So I'm returning the value. 135 00:09:48,050 --> 00:09:53,750 And we also have to do count minus minus and if we are not able to find the key. 136 00:09:54,230 --> 00:09:58,130 So if the key is not present in this linked list, then I am returning zero. 137 00:09:59,000 --> 00:10:00,380 So this is the remove function. 138 00:10:00,740 --> 00:10:05,180 Now let's also write the search function, basically the get value function. 139 00:10:05,690 --> 00:10:06,770 So there get value. 140 00:10:06,800 --> 00:10:09,570 OK, so this is the great value function. 141 00:10:09,590 --> 00:10:11,240 So again, we will copy the same code. 142 00:10:14,330 --> 00:10:15,980 So we just have to find a value. 143 00:10:17,210 --> 00:10:19,190 So for finding the value. 144 00:10:22,670 --> 00:10:27,970 Again, so given a key ABC, you have to find the value for ABC. 145 00:10:28,610 --> 00:10:35,420 Again, we have this key will pass through the hash function, basically get back index function. 146 00:10:35,420 --> 00:10:37,760 It will it will return the bucket index. 147 00:10:38,180 --> 00:10:40,940 I will go to that index in the area in my bucket. 148 00:10:40,990 --> 00:10:41,300 Very. 149 00:10:42,470 --> 00:10:50,600 And in this market index, what do we have, I have a long list so far finding out the value for the 150 00:10:50,600 --> 00:10:51,040 given key. 151 00:10:51,080 --> 00:10:54,340 What I will do, I will iterate over this linked list. 152 00:10:54,350 --> 00:10:59,090 So suppose ABC's present here and I will just hold the value the title. 153 00:10:59,300 --> 00:11:00,830 So I will compare ABC. 154 00:11:01,400 --> 00:11:08,540 But this ABC, if you're able to find out the key you will return to and if you read the complete link 155 00:11:08,600 --> 00:11:11,470 list, but you are not able to find out the given key. 156 00:11:11,480 --> 00:11:15,240 So that means the given key is not present and then you can return a default value. 157 00:11:15,950 --> 00:11:16,520 Simple. 158 00:11:20,010 --> 00:11:21,930 So let us also find out they had. 159 00:11:27,420 --> 00:11:30,990 Now, what you have to do, we have to it the linked list to find out the key. 160 00:11:32,900 --> 00:11:34,640 So while it is not a questionable. 161 00:11:37,960 --> 00:11:39,490 So how ridiculous next of Ed. 162 00:11:44,260 --> 00:11:46,960 Simple and you have to write if condition here. 163 00:11:51,160 --> 00:11:52,180 So if hedgie. 164 00:11:54,310 --> 00:11:56,550 Is it goes to the given guy, so that means. 165 00:11:57,630 --> 00:12:04,570 You find out you found the Gunji and now what we have to do, so basically you have to return the value. 166 00:12:04,920 --> 00:12:06,420 So we just have to return the value. 167 00:12:06,450 --> 00:12:07,580 We do not have to do anything. 168 00:12:08,220 --> 00:12:09,630 So you can directly return. 169 00:12:11,640 --> 00:12:11,970 Head of. 170 00:12:15,490 --> 00:12:21,840 Simple, now, if you will reach if you will come out of the loop, it will come out this way loop. 171 00:12:22,090 --> 00:12:27,810 So that means if you will come out this way loop, that means the given key is not present in my map. 172 00:12:28,060 --> 00:12:32,800 So if the given key is not present, so I can return a default value and let's say zero is the default 173 00:12:32,800 --> 00:12:33,120 value. 174 00:12:33,490 --> 00:12:35,890 So return zero means the given keys not present. 175 00:12:37,470 --> 00:12:42,990 So this is the computer get value function, so get value function, this very simple, this function 176 00:12:43,320 --> 00:12:50,270 is a little bit tougher, then get value function to this function is a little bit less function, a 177 00:12:50,340 --> 00:12:53,100 little bit more complex, then get value function. 178 00:12:54,100 --> 00:12:58,990 Now, in the next video, we will discuss their time, complexity of our hash map I will see in the 179 00:12:58,990 --> 00:13:00,050 next one by.