‫So now we're going to build our hash table constructor.
‫So I'm going to bring up an address space here, except I'm going to make a change to the address space.
‫Instead of having eight items, I'm going to remove the item on the bottom and we're only going to have
‫seven.
‫I want to do this to emphasize that your address space, whatever size that you make it, you should
‫make it a prime number.
‫And the reason for that is if you have a prime number of addresses, your key value pairs will be distributed
‫more randomly.
‫In other words, you'll have fewer collisions.
‫So we'll start our hash table class off like this, and we're going to have a couple of member variables.
‫And I like to explicitly make these private, even though they will be private by default and our first
‫variable will call size, we'll set that to be equal to seven and that's going to be the size of the
‫array that we're using for our address space because we are assigning the value here.
‫We're going to make it a constant.
‫And because it is a constant, a common naming convention is to make the variable name.
‫All caps and static means that we're only going to store one of these numbers sevens in memory.
‫So if we create two hash tables, let's say we call them my hash table one and my hash table two.
‫Both of those instances of hash table will use this one, number seven, and now we'll create that address
‫space.
‫I'm going to call it data map.
‫And the size is going to be this size here.
‫And the data map is going to hold pointers to nodes like this.
‫So I called this video constructor, but technically, this hash table class does not have a constructor.
‫All we're doing is using these two lines to create this data map, address space.
‫Remember, technically, a constructor would be a separate function with the name of the class.
‫But we don't need that here because this is all we're going to do when we create a new hash table.
‫So the other thing we need to be able to create is nodes.
‫So these nodes are going to be a little bit different than what we saw in the linked list section.
‫These nodes are going to have key and value.
‫And next in the linked list section, the nodes just had value index, but they didn't have key.
‫So in our node class, there are going to be three member variables, key value and next.
‫And in the constructor we will pass it a key and a value.
‫This key and value are going to be these variables here.
‫And we'll set this key and this value equal to those.
‫And this key and this value will be the member variables for the class.
‫And then the last thing is we'll set next to be equal to null pointer.
‫So we've written the code in the hash table class to create the address space and then with the node
‫class, the ability to create nodes.
‫So now let's flip over to VS code and take a look at this.
‫So there is our node class there and I'm going to scroll up and this is the code that we just wrote
‫for the hash table class.
‫And just like I have done previously, I've created a function to be able to print out whatever the
‫data structure is that we're building.
‫I'm going to call this one print table.
‫I won't walk through the detail of this code, but you can access this code and the resources tab for
‫this video and then I'll scroll up again.
‫So with this line here, we create a new hash table, and then with this line here, we'll print that
‫table out and I'll run this and you can see our addresses here zero through six.
‫We haven't added any key value pairs yet, so these are all empty.
‫But it does look like we have a working hash table constructor.