﻿1
00:00:00,420 --> 00:00:02,970
‫So now we'll create our set function.

2
00:00:03,390 --> 00:00:08,910
‫I'll bring up an address space like this, and this will represent our hash function.

3
00:00:09,150 --> 00:00:13,410
‫And the way this will work is we'll pass, set a key and a value.

4
00:00:13,590 --> 00:00:18,480
‫We'll run that key through the hash function and get an address.

5
00:00:18,720 --> 00:00:26,160
‫But the set function will also create a node with our key value pair, and then it will take that node

6
00:00:26,160 --> 00:00:29,160
‫and put it at the appropriate index.

7
00:00:29,460 --> 00:00:35,460
‫So let's remove this node for now and we'll move our hash function down to the corner and start our

8
00:00:35,460 --> 00:00:37,290
‫code out like this.

9
00:00:37,620 --> 00:00:41,340
‫And we're just going to pass this a key and a value.

10
00:00:41,490 --> 00:00:45,140
‫And the first thing we'll do in this function is find the index.

11
00:00:45,150 --> 00:00:49,980
‫And in order to do that, we're going to run the hash function on the key.

12
00:00:50,280 --> 00:00:57,240
‫We run this through the hash and get an address, and this is the address where the node is going to

13
00:00:57,240 --> 00:00:57,870
‫go.

14
00:00:58,350 --> 00:01:04,620
‫Then we'll create that node with that key and that value like this.

15
00:01:05,010 --> 00:01:10,050
‫Then we have to determine whether or not there are already nodes at the index of six.

16
00:01:10,050 --> 00:01:18,390
‫So we'll say if data map at the index that we're going to store this if it is equal to null pointer.

17
00:01:18,750 --> 00:01:23,310
‫This is the situation where we do not have nodes at that address.

18
00:01:23,580 --> 00:01:30,570
‫If this is the case, we'll set data map index to be equal to new node like this.

19
00:01:31,140 --> 00:01:34,770
‫Otherwise there are nodes there and we'll say else.

20
00:01:35,190 --> 00:01:40,950
‫And to build out this alt statement, I'm going to shrink down this node and I want to add a new node

21
00:01:40,950 --> 00:01:44,520
‫in with a linked list that already has a couple of items here.

22
00:01:44,520 --> 00:01:51,000
‫So it's lumber that we're going to add to this linked list that already has these two items in it.

23
00:01:51,300 --> 00:01:56,490
‫So in order to do this, we're going to have to iterate through the length list to get to the end to

24
00:01:56,490 --> 00:01:59,250
‫be able to add that new node to the end.

25
00:01:59,520 --> 00:02:03,510
‫And in order to iterate through the length list, we'll need a variable.

26
00:02:03,510 --> 00:02:08,280
‫We'll call this variable temp and we'll represent it like this.

27
00:02:08,730 --> 00:02:13,290
‫And we're going to set it to be equal to data map index.

28
00:02:13,620 --> 00:02:21,810
‫So the pointer at Data Map Index is this pointer here is the one that's actually being stored in the

29
00:02:21,810 --> 00:02:22,530
‫array.

30
00:02:22,830 --> 00:02:27,210
‫It is the equivalent in the linked list section of head.

31
00:02:27,540 --> 00:02:33,420
‫So we're pointing temp at the same node that that arrow is pointing to like this.

32
00:02:33,810 --> 00:02:40,710
‫So to iterate through the length list, we'll create this wild loop and we'll say while temp next is

33
00:02:40,710 --> 00:02:46,620
‫not equal to null pointer and temp next is pointing to a node.

34
00:02:46,620 --> 00:02:49,080
‫So it is not equal to null pointer.

35
00:02:49,260 --> 00:02:55,620
‫If this is the case, we will set temp to be equal to temp next, which moves that pointer to the next

36
00:02:55,620 --> 00:02:56,280
‫node.

37
00:02:56,580 --> 00:03:03,090
‫Now when we run the while loop again, temp next is equal to null pointer.

38
00:03:03,390 --> 00:03:09,300
‫So that's going to break us out of the while loop and then we'll set temp next to be equal to new node

39
00:03:09,810 --> 00:03:10,920
‫like this.

40
00:03:11,340 --> 00:03:14,280
‫And that adds that into the length list.

41
00:03:14,700 --> 00:03:20,160
‫So that is all of the code to set a key value pair and our hash table.

42
00:03:20,520 --> 00:03:24,450
‫So now let's flip over to vs code and take a look at this.

43
00:03:25,760 --> 00:03:30,380
‫So there is our set member function there and I'm going to scroll up.

44
00:03:31,510 --> 00:03:38,380
‫And in our main function, this creates the hash table and this sets the three key value pairs that

45
00:03:38,380 --> 00:03:39,730
‫we just saw.

46
00:03:39,760 --> 00:03:45,520
‫And then I've added a couple more here, and then with this line, we'll print that out and I'll run

47
00:03:45,520 --> 00:03:46,150
‫this.

48
00:03:46,900 --> 00:03:54,820
‫And you can see that all of our key value pairs have been added to our hash table and that is our function

49
00:03:55,210 --> 00:03:56,410
‫for set.

