Answered step by step
Verified Expert Solution
Question
1 Approved Answer
We want to keep a hash table that stores the students in our section. Each student has two info field: student id and name. Student
We want to keep a hash table that stores the students in our section. Each student has two info
field: student id and name. Student id is going to be used as the key.
Implement a hash table class that provides the following public methods.
insert: this takes a student object idname pair and store the object in the hash table
using the id as the key. This function should not allow any duplicate keys ie no two
students with the same id in the table.
retrieve: this takes a key id and retrieves the value name associated with the key if it
is found.
Hence, only insert and retrieve operations can be used with objects of this hash table class. The
default constructor and destructor have been provided for your convenience.
Use Open Addressing with linear probing resolution method. Use the hash function hkey
key TableSize. Here the key is student ID
To implement you can use the following hashTable.h as a starter. To test your solution write
your own driver code in main.cpp Do not submit your main.cppFor your convenience, a
sample main.cpp is enclosed in the Appendix.
starter hashTable.h file
struct Student
int id; this is the key
string name;
;
class hashTable
public:
hashTableint capacity
size;
TableSizecapacity;
if TableSize
TableSize ;
list new StudentTableSize;
for int i; i TableSize;i
listiid SentinelID; initially all of the entries empty
bool insertStudent &st
returns true if the st is inserted, returns false if the st is
not inserted eg an object with the same key already exists or
the table is full
the hash function is stid TableSize;
write your code here
bool retrieveint id string &name
the function returns true and the name if the id appears in the
table Otherwise, the function returns false
write your code here
~hashTable
delete list;
private:
int TableSize;
int size;
int SentinelID; to understand whether the entry is empty
Student list;
Appendix: Sample main.cpp
#include
#include "hashTable.h
using namespace std;
The driver code for the class hashTable
int main
hashTable myHT; small table of size is enough to test
Student st;
stid ;
stid Ali;
myHTinsertst;
stid ;
stid Aesha;
myHTinsertst;
stid ;
stid Hasan;
if myHTinsertst to test the duplicate insertion
cout "Student with id stid and with name
stname is inserted into the table" endl;
else
cout "Unable to insert Student:" stid endl;
int queryid ;
string name;
if myHTretrievequeryid name
cout "Student queryid has the name name endl;
else
cout "Student queryid is not in the table" endl;
int queryid ;
string name;
if myHTretrievequeryid name
cout "Student queryid has the name name endl;
else
cout "Student queryid is not in the table" endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started