I can't seem to wrap my mind around what is going on here. My teacher gave us an implementation of a copy constructor for a separately chained hash table.
Basically we traverse down each bucket index, and scan for each node in a linked list, copying each value from a source to a target along the way.
In the code, we go through a for loop until we hit the amount of buckets in our map. From there, I don't really know what is going on?
1 2
|
Node** prevNode = &hashTable[i];
*prevNode = nullptr;
|
This is declared before a while loop. What are we storing into prevNode in line 1? Is it the address of a hash table at a specific index into a pointer into a pointer into a previous node? What exactly is this doing?
Also what is *prevNode referring to? We end up changing *prevNode to equal a copied node later on.
After this, in a while loop, we initialize a new pointer called copy, which we construct a new node of, and transfer a source nodes values into. Like this:
1 2 3 4
|
Node* copy = new Node(); // create a new Node to store data to
copy->key = sourceNode->key; // copy username
copy->value = sourceNode->value; // copy password
copy->next = nullptr; // set next to nullptr
|
This I understand, but after this we have the lines:
1 2 3
|
*prevNode = copy;
prevNode = ©->next;
sourceNode = sourceNode->next;
|
I understand traversing sourceNode in the last line. The thing I'm having trouble with is understanding what "prevNode = ©->next" does.
Thanks for any tips you can give me.