Question: how can I implement this in c++ Homework using hashtable and function. const int STRTBL_NUM_BUCKETS = 1000; // a node in a linked list struct
how can I implement this in c++ Homework using hashtable and function. const int STRTBL_NUM_BUCKETS = 1000; // a node in a linked list struct StringTableEntry { std::string data; StringTableEntry* next = NULL; }; typedef StringTableEntry* StringTableRef; class StringTable { public: StringTable(); ~StringTable(); StringTableRef insert(string item); StringTableRef search(string searchName); string search(StringTableRef ref); void print(); void destruct(); private: StringTableRef bucket[STRTBL_NUM_BUCKETS]; int hash(string item); int hashVal; int numCollisions = 0; int numEntries = 0; }; #include #include #include "StringTable.h" int main(int argc, char** argv) { { // extra block to test the destructor StringTable t; string filename, aline, cmd; char ccmd; StringTableRef p; // open the input file if (argc < 2) return 0; filename = argv[1]; ifstream f; f.open(filename); if (f.fail()) return 0; // read test data and insert into string table, one line per item while (!f.eof() && !f.fail()) { getline(f, aline); t.insert(aline); } f.close(); t.print(); // how do I print the items. ccmd = '?'; while (ccmd != 'X' && ccmd != 'E') { cout << "Insert, Search, Print or Exit (I,S,P,X): "; getline(cin, cmd); ccmd = toupper(cmd[0]); switch (ccmd) { case 'I': cout << "Enter string: "; getline(cin, aline); t.insert(aline); break; case 'S': cout << "Enter string: "; getline(cin, aline); p = t.search(aline); // searching line? if (p) { cout << "Found search 1: " << p->data << endl; aline = t.search(p); cout << "Found search 2: " << aline << endl; } else cout << "Not found "; break; case 'P': t.print(); break; // How do I print case 'X': cout << "Testing destruct... "; t.destruct(); t.print(); break; default: cout << "Invalid input. Enter I,S,P or X "; } } } system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
