Please answer in c++ Here is a sample of the stat data file New Hampshire Concor
ID: 3824127 • Letter: P
Question
Please answer in c++
Here is a sample of the stat data file
New Hampshire
Concord
9304 1788 9
Massachusetts
Boston
8257 1788 6
Vermont
Montpelier
9609 1791 14
Rhode Island
Providence
1214 1790 13
Connecticut
Hartford
5009 1788 5
Maine
Augusta
33215 1820 23
New York
Albany
49576 1788 11
Pennsylvania
Harrisburg
45333 1787 2
Delaware
Dover
2057 1787 1
Please give user option to search hash table, delete state info from hash table, and option to viewhash table.
Solve the following Problem a) Some of the attributes of a state in the United States are its name, capital, area, year of admission to the union, and the order of admission to the union. Design the class stateData to keep track of the information for a state. Your class must include appropriate functions to manipulate the state's data, such as the functions setstateInfo, getStatelnfo, and so on. Also, overload the relational operators to compare two states by their name. For easy input and output, overload the stream operators. b) Use Hashing to create a hash table to keep track of each state's information. Select and implement a hash function and a collision resolution method that can be used to implement this problem. Use the state's name (as described in class stateData in part a) as the key to determine the hash address. You may assume that a state's name is a string of no more than 15 characters.Explanation / Answer
Hash Table used to store data in an associative manner.
Hash table data stored in array format and each element has its own position.
If u know the node of the data it will be easy to access.
It uses an array as a storage medium
It uses hashing technique to find out where to insert data and wher to find out the data in hash table.
Basic operations of hash tables are
Insert –used to insert data into hash table
Search-used to search data
Search functionality
To search a data compute the hash code of the key passed and locate the element using that hash code as index in the array.
struct detailsOfData *search(int n) {
//find the hash value
int hashValue = hashCode(n);
//keeping search one by one until its empty
while(dataArray[hashValue] != NULL) {
if(dataArray [hashValue]->n == n)
return dataArray [hashValue];
//increment one value
++ hashValue;
//wrap around the table
hashValue %= SIZE;
}
return NULL;
}
Delete-used to delete data from hash table
Delete functionality
It is used to delete the data from hash table.
struct DataItem* delete(struct DataItem* item) {
int n = item->n;
//calculate hash value
int hashValue = hashCode(key);
//move in array until an empty
while(hashArray[hashValue] !=NULL) {
if(dataArray[hashValue]->n == n) {
struct DataItem* temp = dataArray[hashValue];
//put other data in deleted position
dataArray[hashValue] = newItem;
return sample;
}
++ hashValue;
hashValue %= SIZE;
}
return NULL;
}
Search-used to search a particular data in hash table
Each operations I explained below
Insert functionality
This operation is used to insert data.
Sample code
void add(int n,int item) {
struct detailsOfData *item = (struct detailsOfData *) malloc(sizeof(struct detailsOfData));
item-> item = item;
item->n = n;
//calculate hash value
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(dataArray[hashIndex] != NULL && hashArray[hashValue]->n != -1) {
//go to next cell
++ hashValue;
hashValue %= SIZE;
}
dataArray[hashValue] = item;
}
View functionality
This is used to view hash table data
void displayData(Hashtable *hashtable)
{
node n;
cout << "the elemnst in the data are displayed thorugh iteration <<;
hashtable->initIterator();
while(hashtable->hasNext())
{
hashtable->getNextKey(n.Key);
hashtable->get(&n);
display(&n);
}
}
//the above function will call below function to display data
void display(node *n)
{
cout << "the dats’s are"
<< n->data 1 <<" name" << n->value << "alue: "
<< n->data 1 <<" number" << n->value << " umber: "
}