.
In LinearHashTable, t is an ArrayStack of T's.
For examples of how hash tables work, suppose the hash table length is 8 and you are hashing some 3 digit numbers with the hash function being to take the middle digit. Thus hash(123) is 2.
Question 1:
Draw a picture of the the ChainedHashTable that results from these operations:
ChainedHashTable<int> H(000); // null is 000.
H.add(123);
H.add(321);
H.add(135);
H.remove(321);
H.add(929);
Solution (I'll show the state after each action, even though only the last picture was required):
H.add(123);
0 1 2 3 4 5 6 7
---------------------------------
| | | . | | | | | |
----------|----------------------
V
-----
|123|
-----
| |
| |
H.add(321);
0 1 2 3 4 5 6 7
---------------------------------
| | | . | | | | | |
----------|----------------------
V
-----
|123|
-----
|321|
-----
| |
H.add(135);
0 1 2 3 4 5 6 7
---------------------------------
| | | . | . | | | | |
----------|---|------------------
| \
V \
----- -----
|123| |135|
----- -----
|321| | |
-----
| |
H.remove(321);
0 1 2 3 4 5 6 7
---------------------------------
| | | . | . | | | | |
----------|---|------------------
| \
V \
----- -----
|123| |135|
----- -----
| | | |
-----
| |
H.add(929);
0 1 2 3 4 5 6 7
---------------------------------
| | | . | . | | | | |
----------|---|------------------
| \
V \
----- -----
|123| |135|
----- -----
|929| | |
-----
| |
Question 2: Same thing for a LinearHashTable
LinearHashTable<int> H(000,999); // null is 000, del is 999.
H.add(123);
H.add(321);
H.add(135);
H.remove(321);
H.add(929);
Solution (I'll show the state after each action,
even though only the last picture was required):
0 1 2 3 4 5 6 7
-----------------------------------------
|null|null|null|null|null|null|null|null|
-----------------------------------------
H.add(123); // hashed to 2, found 2 unoccupied, so used it.
0 1 2 3 4 5 6 7
-----------------------------------------
|null|null|123 |null|null|null|null|null|
-----------------------------------------
H.add(321); // found 2 occupied so went to 3.
0 1 2 3 4 5 6 7
-----------------------------------------
|null|null|123 |321 |null|null|null|null|
-----------------------------------------
H.add(135); // found 3 occupied so went to 4.
0 1 2 3 4 5 6 7
-----------------------------------------
|null|null|123 |321 |135 |null|null|null|
-----------------------------------------
H.remove(321); // wrong val at 2 so went to 3 and found it.
0 1 2 3 4 5 6 7
-----------------------------------------
|null|null|123 |del |135 |null|null|null|
-----------------------------------------
// comment: an H.find(135) would now work right
// because del is "owl", occupied when looking.
H.add(929); // 2 occupied, but 3 ok (del is "awe", available when entering).
0 1 2 3 4 5 6 7
----------------------------------------
|null|null|123 |929 |135 |null|null|null|
-----------------------------------------
// comment: 939 would have gone to the same spot.