Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me fix code in C + + All errors occur in main: #include #include #include #include template class Table { public: static const std::size

Help me fix code in C++ All errors occur in main: #include
#include
#include
#include
template
class Table {
public:
static const std::size_t CAPACITY =811;
Table() : used(0){}
void insert(const RecordType& entry);
void remove(int key);
bool isPresent(int key) const;
void find(int key, bool& found, RecordType& result, std::function hashingFunction = findIndex) const;
std::size_t size() const { return used; }
private:
static const int NEVERUSED =-1;
static const int PREVIOUSLYUSED =-2;
RecordType data[CAPACITY];
std::size_t used;
std::size_t hash(int key) const;
std::size_t nextIndex(std::size_t index) const;
void findIndex(int key, bool& found, std::size_t& i) const;
bool neverUsed(std::size_t index) const;
bool isVacant(std::size_t index) const;
static void quadraticProbe(int key, bool& found, std::size_t& i, const Table* table);
static void secondHash(int key, bool& found, std::size_t& i, const Table* table);
};
template
void Table::insert(const RecordType& entry){
bool alreadyPresent;
std::size_t index;
assert(entry.key >=0);
find(entry.key, alreadyPresent, index);
if (!alreadyPresent){
assert(size() CAPACITY);
index = hash(entry.key);
while (!isVacant(index)){
index = nextIndex(index);
}
++used;
}
data[index]= entry;
}
template
void Table::remove(int key){
bool found;
std::size_t index;
assert(key >=0);
find(key, found, index);
if (found){
data[index].key = PREVIOUSLYUSED;
--used;
}
}
template
void Table::find(int key, bool& found, RecordType& result, std::function hashingFunction) const {
std::size_t index;
hashingFunction(key, found, index);
if (found){
result = data[index];
}
}
template
std::size_t Table::hash(int key) const {
return key % CAPACITY;
}
template
std::size_t Table::nextIndex(std::size_t index) const {
return (index +1)% CAPACITY;
}
template
void Table::findIndex(int key, bool& found, std::size_t& i) const {
std::size_t count =0;
i = hash(key);
while ((count CAPACITY) && (!neverUsed(i)) && (data[i].key != key)){
++count;
i = nextIndex(i);
}
found =(data[i].key == key);
}
template
bool Table::neverUsed(std::size_t index) const {
return data[index].key == NEVERUSED;
}
template
bool Table::isVacant(std::size_t index) const {
return (data[index].key == NEVERUSED || data[index].key == PREVIOUSLYUSED);
}
template
void Table::quadraticProbe(int key, bool& found, std::size_t& i, const Table* table){
std::size_t count =0;
i = table->hash(key);
while ((count CAPACITY) && (!table->neverUsed(i)) && (table->data[i].key != key)){
++count;
i =(table->hash(key)+ count * count)% CAPACITY;
}
found =(table->data[i].key == key);
}
template
void Table::secondHash(int key, bool& found, std::size_t& i, const Table* table){
std::size_t count =0;
i = table->hash(key);
while ((count CAPACITY) && (!table->neverUsed(i)) && (table->data[i].key != key)){
++count;
i =(i +7-(key %7))% CAPACITY;
}
found =(table->data[i].key == key);
}
int main()
{
Table table;
bool found;
RecordType result;
int key =5;
table.find(key, found, result, std::bind(&Table::quadraticProbe, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, &table));
table.find(key, found, result, std::bind(&Table::secondHash, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, &table));
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions