Question
Complete the following partially completed C++ code which reads in a hash table size entered by user (reresented by an integer), and creates a hash
Complete the following partially completed C++ code which reads in a hash table size entered by user (reresented by an integer), and creates a hash table of the size (collision resolving by chaining).
-Each slot of your hash table should be a link list of nodes where each node represents one Student. Initially all linked lists should be empty. Then by reading each students information line by line, their information needs to be stored in the hash table using a hash function. You will need to design your own hash function so that it reduces the number of collisions, i.e.,the length of each linked list should not be too long.
-Each student data will be in one line and will contain the following information which are separated by commas:
name, gender, inClass_or_onLine, major, campus, status, address, city
-After the line InsertionEnd, a user will enter a number of commands. Each command will be in one of the three forms: hashDisplay, hashSearch, or hashDelete. hashDisplay command:
Assignment4.cpp
#include#include #include #include "Hash.h" using namespace std; int main() { string name, gender, inClass_or_onLine, major, campus, status, address, city; //a variable represents the number of hash table slots int count; //a variable represents the number of commands inside the input file int numOfCommand; //a variable represents the actual command name inside the input file string command; //declare any other necessary variables here //---- //get the first line which is a number and store it inside count cin >> count; //create a hash table with the 'count' number of slots //---- do { //get one line of student data //---- //Tokenize it to get each information out, then //insert the new student inside the hash table //---- //---- } while(//as long the line we read in is not "InsertionEnd"); cin >> numOfCommand; for(int i= 0;i LinkedList.h
#include#include #include using namespace std; struct Student { string key; string name, gender, inClass_or_onLine, major, campus; string status, address, city; struct Student *next = NULL; }; class LinkedList { private: struct Student *head; int size; public: LinkedList(); ~LinkedList(); bool insert(string key, string name, string gender, string inClass_or_onLine, string major, string campus, string status, string address, string city); bool deleteStudent(string key); bool search(string key); void displayList(); int getSize(); }; //Constructor LinkedList::LinkedList() { head = NULL; size = 0; } //Destructor LinkedList::~LinkedList() { //Add your own codes here //---- //---- } //Return number of students inside the Linked list int LinkedList::getSize() { return size; } //Insert the parameter student at the head of the linked list. //return true if it is inserted successfully and false otherwise bool LinkedList::insert(string key, string name, string gender, string inClass_or_onLine, string major, string campus, string status, string address, string city) { //Add your own codes here //---- //---- } //Delete the student with the given key. //Return true if it is deleted successfully and false otherwise bool LinkedList::deleteStudent(string key) { //Add your own codes here //---- //---- if(//the student's key matches the parameter key) { cout name major campus Hash.h
#include#include #include #include "LinkedList.h" using namespace std; class Hash { private: LinkedList **table; int m; public: Hash(int size); ~Hash(); bool hashInsert(string name, string gender, string inClass_or_onLine, string major, string campus, string status, string address, string city); bool hashDelete(string name, string gender, string major, string address); bool hashSearch(string name, string gender, string major, string address); void hashDisplay(); int h(string key); //Add any other necessary function declarations here //which you think are useful //---- //---- }; //constructor - create an array of LinkedList, m is the number of slots Hash::Hash(int size) { table = new LinkedList*[size]; m = size; } //Destructor - release the memory Hash::~Hash() { delete[] table; table = NULL; } //hashInsert inserts a student with the relevant info. into the hash table. //it returns true if the data is inserted successfully and false otherwise bool Hash::hashInsert(string name, string gender, string inClass_or_onLine, string major, string campus, string status, string address, string city) { //Add your own codes here //---- //---- } //hashDelete deletes a student with the relevant key from the hash table. //it returns true if it is deleted successfully and false otherwise //Note: key is the combination of name, gender, major and address bool Hash::hashDelete(string name, string gender, string major, string address) { //A bool variable used to check whether the student is deleted from //the hash table or not. bool deleted = false; //Add your own codes here //---- //---- if(!deleted) { cout
Example: Input1.txt
6 John Smith,M,InClass,Computer Science,Tempe,Freshman,123 Whatever St.,Chandler Mary Johnson,F,InClass,Business,Tempe,Junior,213 Whatever St.,Chandler Carole Almenda,F,InClass,Computer Science,Polytech,Sophomore,111 Mickey Mouse Ave.,Tempe Natalie Parkson,F,InClass,Psychology,Tempe,Freshman,123 Main Ave.,Tempe Adriean Santoros,F,InClass,Psychology,Tempe,Freshman,22 Linda Dr.,Gilbert Tammy Barnes,M,Online,Business,Tempe,Senior,22 Linda Dr.,Seattle Jason Brown,M,InClass,Computer Science,Polytech,Freshman,123 University Dr.,Tempe Roger Clark,M,InClass,Business,Tempe,Junior,213 Whatever St.,Chandler InsertionEnd 8 hashDisplay hashDelete,Natalie Parkson,F,Psychology,123 Main Ave. hashSearch,John Smith,M,Computer Science,123 Whatever St. hashSearch,Lily Guy,F,Computer Science,123 Main Ave. hashDelete,Jason Brown,M,Computer Science,123 University Dr. hashSearch,Roger Clark,M,Business,213 Whatever St. hashDelete,Roger Clark,M,Business,213 Whatever St. hashDisplay
Example: Output1.txt
index: 0, linked list size: e The list is empty index: 1, linked list size: 4 Roger Clark Name Gender: inClassOrOnLine: InClass Major Campus: Address: City: Business Tempe 213 Whatever St. Chandler Name Gender: inClassOrOnLine: InClass Major Campus: Address: City: Jason Brown Computer Science Polytech 123 University Dr. Tempe Mary Johnson Name Gender: inClassOrOnLine: InClass Major: Campus: Address: City: Business Tempe 213 Whatever St. Chandler John Smith Name Gender: inClassOrOnLine: InClass Major: Campus: Address: City: Computer Science Tempe 123 Whatever St. Chandler index: 2, linked list size: 2 Name Gender: inClassOrOnLine: Online Major: Campus: Address: Tammy Barnes Business Tempe 22 Linda Dr Seattle index: 0, linked list size: e The list is empty index: 1, linked list size: 4 Roger Clark Name Gender: inClassOrOnLine: InClass Major Campus: Address: City: Business Tempe 213 Whatever St. Chandler Name Gender: inClassOrOnLine: InClass Major Campus: Address: City: Jason Brown Computer Science Polytech 123 University Dr. Tempe Mary Johnson Name Gender: inClassOrOnLine: InClass Major: Campus: Address: City: Business Tempe 213 Whatever St. Chandler John Smith Name Gender: inClassOrOnLine: InClass Major: Campus: Address: City: Computer Science Tempe 123 Whatever St. Chandler index: 2, linked list size: 2 Name Gender: inClassOrOnLine: Online Major: Campus: Address: Tammy Barnes Business Tempe 22 Linda Dr Seattle
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