C++: (PLEASE Help)
I need to:
Implement main.cpp file, which creates a HashTable object with parameter 10007 for the constructor. The constructor of HashTable uses this prime number to build an internal SLL object array. Each element of the array is an SLL object. Of course, you can choose a different array size, but make sure the size is a prime number.
Here is my code so far:
#include #include #include "hashTable.h" #include #include #include
using namespace std;
int main(int argc, char * argv[]){ clock_t start, end; double duration;
//start = clock(); char firstLetter; string SSN, firstName, lastName, name; fstream input(argv[1]); int count = 0; int numInserted = 0; int numDeleted = 0; int numRetrieved = 0; int numTable = 0;
//Do I need these two? Ive tried with and without them both/combination of neither //and gotten the same issue //Node* node = new Node; //SLL* SLL = new SLL;
HashTable* T = new HashTable(10007);
while(!input.eof()){ input >> firstLetter >> SSN >> firstName >> lastName; name = firstName + " " + lastName; switch(firstLetter){ case 'd':{ if(T.erase(SSN) == true){ numDeleted++; numTable--; } } case 'i':{ if(T.insert(SSN, name) == true){ numInserted++; numTable++; } } case 'r':{ if(T.find(SSN) == true){ numRetrieved++; } } } input.close(); }
//duration = ( end - start ) / (double) CLOCKS_PER_SEC; cout << "The Numer of Valid Insertions: " << numInserted << endl; cout << "The Number of Valid Deletions: " << numDeleted << endl; cout << "The Number of Valid Retrievals: " << numRetrieved << endl; cout << "The Number of Items in Array: " << numTable << endl; cout<<"elapsed time: "<< duration <<' '; //end = clock(); }
I'm getting errors:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is hashtable.h file:
#include #include "SLL.h" #include #include using namespace std;
template class HashTable { int tableSize; // table size SLL* table; public: // default constructor, which uses default table size 3 HashTable() { tableSize = 3; table = new SLL[tableSize]; } // constructor, which use size as the table size HashTable(int size) { tableSize = size; table = new SLL[tableSize]; } // search item in the table // if found, return true; otherwise, return false bool find(V item){ long hashVal = 0; long compressVal = 0; for(int i=0; i<=item.length()-1; i++) { int power = item.length()-1-i; hashVal = hashVal + ((int)(item.at(i)))*(pow(31, power)); } compressVal = abs((hashVal)%(10007)); SLL* temp = &table[compressVal]; if(temp->search(item) != NULL){ return true; } else{ return false; } } // insert (item1, item2) to the table // use item1 as the key // if inserted, return true // otherwise, return false bool insert(V item1, V item2) { long hashVal=0; long compressVal=0; for(int i=0; i<=item1.length() - 1; i++){ int power = item1.length() - 1 - i; hashVal = hashVal + ((int)(item1.at(i))) * (pow(31, power)); } compressVal = abs((hashVal)%(10007)); SLL* temp1 = &table[compressVal]; if(temp1->search(item1) != NULL){ return false; } SLL* temp2 = &table[compressVal]; temp2->insert(item1, item2); SLL* temp3 = &table[compressVal]; if(temp3->search(item1) != NULL){ return true; } else{ return false; } } // delete the pair whose key value is item // if deleted, return true // otherwise, return false bool erase(V item) { long hashVal=0; long compressVal=0; for(int i=0; i<= item.length() - 1; i++) { int power = item.length() -1 - i; hashVal = hashVal + ((int)(item.at(i)))*(pow(31, power)); } compressVal = abs((hashVal)%(10007)); SLL* temp1 = &table[compressVal]; if(temp1->remove(item)) { return true; } else { return false; } } // return the total number of nodes in the hash table int getSize() { int nodeCount = 0; for(int i=0; i<=tableSize-1; i++) { SLL* temp = &table[i]; nodeCount = nodeCount + temp->getSize(); } return nodeCount; } };
~~~~~~~~~~~~~~~~~~This is SLL.h
#include #include "node.h" using namespace std;
template class SLL { Node * headPtr; int size; public: // default constructor SLL(){ headPtr = NULL; size = 0; } // destructor ~SLL(){ Node *next; while(headPtr != nullptr) { next = headPtr->next; delete headPtr; headPtr = next; } } Node* getHeadPtr(){ return headPtr; } // insert (item1, item2) to the list void insert(U item1, U item2){ Node *n = new Node(); n->SSN = item1; n->name = item2; n->next = nullptr; if(headPtr == nullptr) headPtr = n; else{ Node last = headPtr; while(last->next != nullptr){ last = last->next; } last->next = n; } size++; } // if find the item1 value, return the pointer to the node // otherwise, return nullptr Node* search(U item1){ Node *curr = headPtr; while(curr != nullptr) { if(curr->item1 == curr->SSN) return curr; curr = curr->next; } return nullptr;//not found } // remove the node with key value: item1 bool remove(U item1){ Node *prev = nullptr, *curr = headPtr; while(curr != nullptr) { if(curr->SSN == item1) break; prev = curr; curr = curr->next; } if(curr == nullptr) //not found return false; if(curr == headPtr) //remove head node? headPtr = curr->next; else prev->next = curr->next; delete curr; size--; return true; } int getSize(){ return size; } // display the SSN values of each node in the linked list void display(){ Node* temp; temp = headPtr; while (temp!= nullptr) { cout << temp->SSN << endl; temp = temp->next; } } };
PLEASE HELP