Question
I need to implement a hash table of 29 buckets to store string data, below are the given specs. III. Data structure: Must have all
I need to implement a hash table of 29 buckets to store string data, below are the given specs.
III. Data structure: Must have all the object classes as given below.
********************************
- list Node class
- data (string)
- count (int) // initialize to zero
- next (listNode *)
methods:
- printNode (node) // use the format:
(this nodes data, this nodes count, next nodes data )
// see example below.
- A hashTable class
- bucketSize (int) // set to 29
- hashTableAry [bucketSize] (listNode *)
// An array of list node pointer, listNode *, size of bucketSize,
method:
- createHashAry (...) // The method dynamically allocate the hashTableAry,
//where each entry point to a dummy node: (dummy, 0, null)
// On your own! You should know how to do this.
- storeDataIntoHashTable () // reads a data from inFile, gets the index from hash function, and stores
// the data into the hash table
- (int) Doit () // Given an English word, the method returns the index of hashTableAry
// where the data will be insert into the linked list pointed by hashTableAry [index].
- listInsert () // Reuse the code in your previous projects
- findSpot () // Reuse code in your previous projects
- printHashTable ()
// The method calls printList method to print the entire 29 linked list of the hash table.
- printList ()
// The method calls printNode method to print each node in the entire linked list, pointed by the //hashTableAry[index] to a given outFile, using the following format:
hashTable[index] (this node data, this nodes count, next nodes data) (this node data, this nodes count, next nodes data) . . . . . NULL
For example: if index is 5
hashTable [5] ( dummy, 0, Adam) (Adam, 1, Ben) (Ben, 2, Brian) (Brian, 2, Chuck)........................ NULL
******************************************
IV. Main( )
******************************************
Step 1: inFile open input file using argv[1]
outFile1, outFile2 open outfiles using argv[2] and argv[3]
Step 2: createHashAry (hashTableAry, bucketSize) // on your own
Step 3: storeDataIntoHashTable (inFile, outFile2)
Step 4: printHashTable (outFile1)
Step 5: Close all files
******************************************
V. storeDataIntoHashTable (inFile, outFile2)
******************************************
Step 1: data get one string from inFile
Step 2: newNode get a new listNode (data,1, null)
Step 3: index Doit (data)
Step 4: listHead hashTableAry [index] // listHead is a local listNode* type
Step 5: listInsert (listHead, newNode)
Step 6: printList (index, outFile2) // debug prints
Step 7: repeat step 1 step 6 until the end of inFile
******************************************
VI. listInsert (listHead, newNode)
******************************************
Step 1: Spot findSpot (listHead, newNode)
Step 2: if Spot != null
newNodes next Spots next
Spots next newNode
******************************************
VII. (listNode *) findSpot (listHead, newNode)
******************************************
Step 1: Spot listHead
Step 2: if Spots next != null *and* Spots nexts data < newNodes data // string comparison!!
Spot Spots next
Step 3: repeat Step 2 until condition failed
Step 4: if Spots nexts data == newNodes data
Spots nexts count ++
Spot null
Step 5: return Spot
******************************************
VIII. printHashTable (outFile)
******************************************
Step 1: index 0
Step 2: printList(index, outFile)
Step 3: index ++
Step 4: repeat step 2 to step 3 while index < bucketSize
******************************************
VIII. printList (index, outFile)
******************************************
Step 0: output to outFile : hashTable [ index]
Step 1: printSpot hashTableAry[index]
Step 2: printNode (printSpot)
printSpot printSpots next
Steo 3: repeat step 2 while printNode != null
#include#include using namespace std; const int bucketSize = 29; // Set table size struct listNode{ public: string data; int count = 0; listNode* next; listNode(string d, int c, listNode* n) { data = d; count = c; next = n; } listNode* printNode(listNode* node){ //Ex: this node's data, this node's count, next node's data -> return node; } }; class hashTable{ //An array of list node pointer,size 29 listNode* hashTableAry[bucketSize]; public: hashTable(listNode* listHead){ listHead = hashTableAry[bucketSize]; } //dynamically allocate the hashTableAry void createHashAry(listNode* hashTableAry, int bucketSize){ //where each entry points to a dummy node: ("dummy", 0, null) //for(int i = 0; i < bucketSize; i++){ //listNode* dummy = hashTableAry[i]; //dummy = new listNode("dummy", 0, nullptr); //} return; } //reads a data from inFile, gets the index from hash function //stores the data into the hash table void storeDataIntoHashTable(ifstream& inFile, ofstream& outFile2){ string data; listNode* listHead; listNode* newNode; while(!inFile.eof()){ //get a string from inFile inFile >> data; //get a new listNode(data,1,null) newNode = new listNode(data,1, nullptr); //step 3 int index = Doit(data); listHead = hashTableAry[index]; listInsert(listHead, newNode); printList(index,outFile2); } return; } //given an English word, the method returns // the 'index' of hashTableAry int Doit(string data){ //where the data will be inserted into // LL pointed by hashTableAry[index] int index = 0; for(int i = 0; i < data.length(); i++){ index = index * 32 + data[i]; } return index; } //finished void listInsert(listNode* listHead, listNode* newNode){ listNode* spot; spot = findSpot(listHead,newNode); if(spot != NULL){ newNode -> next = spot -> next; spot ->next = newNode; } } listNode * findSpot(listNode* listHead, listNode* newNode){ listNode* spot = listHead; while(spot ->next != nullptr && spot->next->data < newNode->data){ spot = spot->next; if (spot->next->data == newNode->data) { spot = spot->next; //?? spot = nullptr; } } return spot; } //calls printList method to print entire 29 LL of hashtable void printHashTable(ofstream& outFile1){ int index = 0; while(index < bucketSize) { printList(index, outFile1); index++; } } //method calls printNode method to print each node //in the entire LL, pointed by the //hashTableAry[index] to a given outFile //hashTable[5] ->(dummy,0,Adam)->(Adam,1,Ben)->(Ben,2,Brian)->...->NULL void printList(int index, ofstream& outFile2){ outFile2 << "hashTable[index]->" << endl; listNode *printSpot = hashTableAry[index]; //printSpot = printSpot->next; } }; int main(int argc, char* argv[]) { ifstream inFile; //text file contains a list of english words(strings) inFile.open(argv[1]); //open input file using argv[1] ofstream outFile1, outFile2; outFile1.open(argv[2]); //open outfile using argv[2] outFile2.open(argv[3]); //open outfile using argv[3] //step 2 listNode* hashTableAry; int bucketSize; hashTable bucket(hashTableAry); bucket.createHashAry(hashTableAry, bucketSize); //step 3 bucket.storeDataIntoHashTable(inFile,outFile2); //step 4 bucket.printHashTable(outFile1); //step 5 close all files inFile.close(); outFile1.close(); outFile2.close(); }
this is my code so far. I'm not sure how to implement the createHashAry() method, prof says to dynamically allocate the hashTableAry where each entry points to a dummy node: ("dummy", 0, null).
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