﻿1
00:00:00,450 --> 00:00:03,030
‫So now let's create our get function.

2
00:00:03,390 --> 00:00:09,900
‫I'm going to bring up an address space with a few key value pairs and I'll bring in this to represent

3
00:00:09,900 --> 00:00:11,340
‫our hash function.

4
00:00:11,490 --> 00:00:14,640
‫And let's say we're going to get lumber.

5
00:00:14,970 --> 00:00:21,180
‫The first thing we'll do is run lumber through the hash function and we'll get the address of six.

6
00:00:21,420 --> 00:00:26,370
‫So we know that if we do have any lumber, this is where it will be.

7
00:00:26,580 --> 00:00:32,250
‫Then once you go to that index, we need to iterate through this length list looking for it, and then

8
00:00:32,250 --> 00:00:38,340
‫once we find it, we'll return the value that is associated with that key.

9
00:00:38,880 --> 00:00:41,760
‫But let's say we're going to look for bolts.

10
00:00:42,240 --> 00:00:47,070
‫We'll run that through the hash function and we get an address of four.

11
00:00:47,340 --> 00:00:53,580
‫And when we go to that address, we don't have any key value pairs, which means that we don't have

12
00:00:53,580 --> 00:00:54,540
‫any bolts.

13
00:00:54,810 --> 00:01:00,690
‫So whenever we look for a key that is not in the hash table will return zero.

14
00:01:01,360 --> 00:01:06,520
‫So let's move our hash function down to the corner and we'll start writing our get function.

15
00:01:06,850 --> 00:01:13,810
‫So we'll pass this a key and we'll return an integer, which is the value that's associated with that

16
00:01:13,810 --> 00:01:14,410
‫key.

17
00:01:14,860 --> 00:01:21,250
‫So the first thing we'll do is get the index, we'll pass the key to the hash function and run that.

18
00:01:21,250 --> 00:01:26,020
‫And let's say we're looking for nails that will give us an index of six.

19
00:01:26,470 --> 00:01:31,870
‫And if this hash table does contain nails, that is where we'll find that.

20
00:01:32,140 --> 00:01:35,830
‫So to be able to iterate through this length list, we're going to need a variable.

21
00:01:35,830 --> 00:01:44,020
‫We'll call that temp, we'll create that like this temp is equal to data map at that index, which points

22
00:01:44,020 --> 00:01:47,350
‫it to the first node at the index of six.

23
00:01:47,740 --> 00:01:54,310
‫And then we'll have a while loop that says while temp is not equal to null pointer, which means that

24
00:01:54,310 --> 00:02:02,500
‫temp is pointing to a node, we'll say if temp key and temp is pointing to the key value pair where

25
00:02:02,500 --> 00:02:04,540
‫the key is nails.

26
00:02:04,930 --> 00:02:13,270
‫If it is equal to the key that we're looking for, then we'll return temp value, which is 100.

27
00:02:13,690 --> 00:02:18,010
‫But now let's say that the key that we're looking for is lumber.

28
00:02:18,040 --> 00:02:20,170
‫We'll need a way to iterate through this.

29
00:02:20,170 --> 00:02:21,640
‫We'll do that with this line.

30
00:02:21,640 --> 00:02:27,730
‫Temp equals temp next and the while loop will run and then run again.

31
00:02:28,030 --> 00:02:34,120
‫And then because temp is pointing at lumber, now we have found the key that we're looking for.

32
00:02:34,120 --> 00:02:37,240
‫So temp key is equal to the key we're looking for.

33
00:02:37,450 --> 00:02:40,360
‫So we'll return temp value.

34
00:02:40,720 --> 00:02:45,070
‫So now let's look at a couple of scenarios where the key will not be in the hash table.

35
00:02:45,070 --> 00:02:50,710
‫We'll move temp back over and when that happens, we're going to return zero.

36
00:02:51,130 --> 00:02:57,730
‫So the first scenario is when we're looking for a key that maps to an index where we already have key

37
00:02:57,730 --> 00:03:04,720
‫value pairs, we will go through this while loop so long as temp is not equal to null pointer.

38
00:03:04,900 --> 00:03:12,760
‫So if the key is not in the length list, temp will move through the length list like this until temp

39
00:03:12,760 --> 00:03:14,500
‫is equal to null pointer.

40
00:03:14,500 --> 00:03:19,480
‫And that breaks us out of the while loop and we return zero.

41
00:03:19,870 --> 00:03:25,810
‫The other possibility is that we're looking for a key at an index that does not have a length list.

42
00:03:26,080 --> 00:03:33,790
‫The way this code will work and that situation is temp is going to be set equal to null pointer because

43
00:03:33,790 --> 00:03:38,350
‫remember this array is just holding pointers to nodes.

44
00:03:38,530 --> 00:03:45,160
‫So if that pointer is not pointing to a node, then by definition it is equal to null pointer and because

45
00:03:45,160 --> 00:03:51,610
‫now temp is equal to null pointer, when we move down to this line, we're going to skip the while loop

46
00:03:51,610 --> 00:03:54,580
‫and come down here and return zero.

47
00:03:55,030 --> 00:03:58,240
‫So this code works for both situations.

48
00:03:58,450 --> 00:04:04,900
‫It works when we already have nodes at that address and it also works when we don't have nodes at that

49
00:04:04,900 --> 00:04:05,650
‫address.

50
00:04:06,250 --> 00:04:10,270
‫So now let's flip over to VS code and take a look at this.

51
00:04:10,960 --> 00:04:15,310
‫So there is our get member function and I'll scroll up.

52
00:04:15,950 --> 00:04:22,550
‫And in our main function we'll use this line to create our hash table and these lines create our key

53
00:04:22,550 --> 00:04:23,840
‫value pairs.

54
00:04:24,170 --> 00:04:27,830
‫And then with this line, we'll print that out and I'll run this.

55
00:04:28,690 --> 00:04:33,370
‫And you can see that our hash table contains nails, tile and lumber.

56
00:04:33,910 --> 00:04:38,500
‫So I'm going to come up here and replace this print table line.

57
00:04:39,370 --> 00:04:45,430
‫With these two lines of code and we're going to look for lumber, which is in the hash table, and then

58
00:04:45,430 --> 00:04:48,820
‫we're going to look for bolts, which is not in the hash table.

59
00:04:49,150 --> 00:04:51,310
‫So now I'll run this.

60
00:04:52,220 --> 00:04:55,280
‫And you can see that the quantity for lumber is 80.

61
00:04:55,400 --> 00:05:01,520
‫And then because bolts is not a key value pair that's in our hash table, it's showing that we have

62
00:05:01,520 --> 00:05:02,780
‫zero bolts.

63
00:05:03,660 --> 00:05:06,060
‫And that is our function.

64
00:05:06,450 --> 00:05:07,500
‫Forget.

