Type up and get the Hash table on pages 19 - 22 to work. Show your sample test run at the bottom of your code using a multiline comment
I typed everything but the code is not working please help me fix the mistakes.
These are pages 19-22.
The code I have:
#include <iostream>
#inlcude <iomanip>
#include <stack>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
//////////////////////////////HASH TABLE///////////////////////////////////////////////
//hash.cpp
//demonstrate hash table with linear probing
///////////////////////////////////////////////////////////////////////////////////////
class DataItem
{ public:
int data;
DataItem(int newData)
{data = newData;
}
};
class HashTable
{
private:
vector<DataItem*>hashArray;
int arraySize;
DataItem* pNonItem;
public:
HashTable(int size)
{
arraySize = size;
hashArray.resize(arraySize);
for(int j=0; j<arraySize;j++)
hashArray[j] = NULL;
pNonItem = new DataItem(-1);
}
void displayTable();
cout <<"Table:";
for( int j=0; j<arraySize;j++);
{
if(hashArray[j] != NULL)
cout <<hashArray[j]->data << " ";
else
cout << "**";
}
cout <<endl;
}
int hashFunc (int key)
{
return key % arraySize;
}
void insert(DataItem*pItem)
{
int key = pItem->data;
int hashVal = hashFunc(key);
while(hashArray[hashVal] !=NULL &&
hashArray[hashVal]->data != -1)
{++hashVal;
hashVal %= arraySize;
}
hashArray[hashVal] = pItem;
}
DataItem* remove(int key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] !=NULL)
{ if(hashArray[hashVal]->data == key)
{DataItem* pTemp = hashArray[hashVal];
hashArray[hasVal] = pNonItem;
return pTemp;
}
++hashVal;
hashVal %= arraySize;
}
return Null;
}
DataItem* find(int key)
{int hashVal = hashFunc(key);
while(hashArray[hashVal !=Null )
{
if(hashArray[hashVal]->data == key)
return hashArray[hashVal];
++hashVal;
hashVal %= }
return NULL; }
int main(){
DataItem* pDataItem;
int key ,size ,n, keysPerCell;
time_t aTime;
char choice ='b';
cout << " Enter size of hash table:";
cin >> size;
cout << "Enter initial number of item:";
cin >> n;
keysPerCell = 10;
HashTable theHashTable(size);
srand( static_cast<unsigned>(time(&aTime)));
for (int j=0;j<n; j++)
{
aKey = rand() % (keysPerCe;;*size);
pDataItem = new DataItem(key);
theHashTable.insert(pDataItem);
}
while (choice != 'x')
{
cout<< "Enter first letter of "<<"show, inserts delete, or find:";
char choice;
cin >> choice;
switch(choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
cout << "Enter key value to insert:";
cin >> aKey;
pDataItem = new DataItem(aKey);
theHashTable.insert(pDataItem);
break ;
case 'd':
cout << "Enter key value to delete: ";
cin >> aKey;
theHashTable.remove(aKey);
break;
case'f':
cout << "Enter key value to find:";
cin >> aKey;
pDataItem = theHashTable.find(aKey);
if(pDataItem != NULL)
cout <<"found" <<aKey <<endl;
else
cout << "Could not find" << aKey << end;
break;
default:
cout << "invalid entry/n";
}
}
}
--------------------------------I used Visual Studio 2013, C++ language, Console App----------------
--------------TEST CASES-----------
Test Case 1:
If the values are enter correctly output showing
perfectly
---------------------------------
Test Case 2:
If entered initial number of item is greater than hash table size,
It is not showing anything.
It is because in the insert method it is looping.
---------------------------------
Test Case 3:
If entered delete number is not in the hash table, it is not
showing "not found" message.
---------------------------------
Test Case 4:
If inserted more than hash table capacity, It is not showing any
error message and no other options. It is because in the insert
method it is looping.
---------------------------------
Test Case 5:
Enter first letter of show,inserts delete or find is passed single
character: then show correct error output "Invalid Entry". But when
i pass two letter character like "as" it is showing unwanted error
messages.
---------------------------------
Test Case 6:
If hash table size is passed as letter or string then unexpected
error is showing
------------------------------------------------------------------------------------------------------------------------------------
------------------Working Old Code-------------
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stack>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <string>
using namespace std;
//////////////////////////////HASH TABLE///////////////////////////////////////////////
//hash.cpp
//demonstrate hash table with linear probing
///////////////////////////////////////////////////////////////////////////////////////
class DataItem
{
public:
int data;
DataItem(int newData)
{
data = newData;
}
};
class HashTable
{
private:
vector<DataItem*>hashArray;
int arraySize;
DataItem* pNonItem;
public:
HashTable(int size)
{
arraySize = size;
hashArray.resize(arraySize);
for (int j = 0; j <
arraySize; j++)
hashArray[j]
= NULL;
pNonItem = new
DataItem(-1);
}
void displayTable()
{
cout <<
"Table:";
for (int j=0;
j<arraySize; j++)
{
if
(hashArray[j] != NULL)
cout
<< hashArray[j]->data << " ";
else
cout
<< "**";
}
cout << endl;
}
int hashFunc(int key)
{
return key %
arraySize;
}
void insert(DataItem*pItem)
{
int key =
pItem->data;
int hashVal =
hashFunc(key);
while (hashArray[hashVal]
!= NULL &&
hashArray[hashVal]->data
!= -1)
{
++hashVal;
hashVal
%= arraySize;
}
hashArray[hashVal] =
pItem;
}
DataItem* remove(int key)
{
int hashVal =
hashFunc(key);
while (hashArray[hashVal]
!= NULL)
{
if
(hashArray[hashVal]->data == key)
{
DataItem*
pTemp = hashArray[hashVal];
hashArray[hashVal]
= pNonItem;
return
pTemp;
}
++hashVal;
hashVal
%= arraySize;
}
//cout << "Entered
number is not found." << endl;
return NULL;
}
DataItem* find(int key)
{
int hashVal =
hashFunc(key);
while (hashArray[hashVal]
!= NULL)
{
if
(hashArray[hashVal]->data == key)
return
hashArray[hashVal];
++hashVal;
hashVal
%= arraySize;
}
return NULL;
}
};
int main(){
DataItem* pDataItem;
int aKey, keysPerCell;
string stringSize, stringN;
int intSize;
int intN;
time_t aTime;
char choice = 'b';
cout << "Enter size of hash table: ";
cin >> stringSize;
istringstream iss1(stringSize);
iss1 >> intSize;
if (iss1.fail()) {
cout << "Please
enter numeric values" << endl;
system("pause");
return 0;
}
cout << "Enter initial number of item less
than or equal to hash table size " << intSize << ":
";
cin >> stringN;
istringstream iss2(stringN);
iss2 >> intN;
if (iss2.fail()) {
cout << "Please
enter numeric values" << endl;
system("pause");
return 0;
}
keysPerCell = 10;
if (intN > intSize)
{
cout << "Please
enter number less than or equal to hash table size." <<
endl;
system("pause");
return 0;
}
HashTable theHashTable(intSize);
srand(static_cast<unsigned>(time(&aTime)));
for (int j = 0; j<intN; j++)
{
aKey = rand() %
(keysPerCell*intSize);
pDataItem = new
DataItem(aKey);
theHashTable.insert(pDataItem);
}
while (choice != 'x')
{
char choice = NULL;
cout << "Enter
first letter of " << "show, inserts delete, or find: ";
cin >>
choice;
int length =
sizeof(choice);
if ((choice >= 65
&& choice <= 90) || (choice >= 97 && choice
<= 122))
cout
<< " Alphabet ";
switch (choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
cout
<< "Enter key value to insert: ";
cin
>> aKey;
pDataItem
= new DataItem(aKey);
theHashTable.insert(pDataItem);
break;
case 'd':
cout
<< "Enter key value to delete: ";
cin
>> aKey;
theHashTable.remove(aKey);
break;
case'f':
cout
<< "Enter key value to find: ";
cin
>> aKey;
pDataItem
= theHashTable.find(aKey);
if
(pDataItem != NULL)
cout
<< "Found " << aKey << endl;
else
cout
<< "Could not find " << aKey << endl;
break;
default:
cout
<< "Invalid entry\n";
break;
}
}
}
------------------Working New Code, Solved test cases problem-------------
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stack>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <string>
using namespace std;
//////////////////////////////HASH TABLE///////////////////////////////////////////////
//hash.cpp
//demonstrate hash table with linear probing
///////////////////////////////////////////////////////////////////////////////////////
class DataItem
{
public:
int data;
DataItem(int newData)
{
data = newData;
}
};
class HashTable
{
private:
vector<DataItem*>hashArray;
int arraySize;
DataItem* pNonItem;
public:
HashTable(int size)
{
arraySize = size;
hashArray.resize(arraySize);
for (int j = 0; j < arraySize;
j++)
hashArray[j] =
NULL;
pNonItem = new DataItem(-1);
}
void displayTable()
{
cout << "Table:";
for (int j=0; j<arraySize;
j++)
{
if (hashArray[j]
!= NULL)
cout << hashArray[j]->data << "
";
else
cout << "**";
}
cout << endl;
}
int hashFunc(int key)
{
return key % arraySize;
}
DataItem* insert(DataItem*pItem)
{
int count = 0;
int key = pItem->data;
int hashVal = hashFunc(key);
while (hashArray[hashVal] != NULL
&&
hashArray[hashVal]->data != -1)
{
++hashVal;
hashVal %=
arraySize;
if (++count ==
arraySize)
{
return NULL;
}
}
hashArray[hashVal] = pItem;
DataItem* pTemp =
hashArray[hashVal];
return pTemp;
}
DataItem* remove(int key)
{
int count = 0;
int hashVal = hashFunc(key);
while (hashArray[hashVal] !=
NULL)
{
if
(hashArray[hashVal]->data == key)
{
DataItem* pTemp = hashArray[hashVal];
hashArray[hashVal] = pNonItem;
return pTemp;
}
++hashVal;
hashVal %=
arraySize;
if (++count ==
arraySize)
{
return NULL;
}
}
return NULL;
}
DataItem* find(int key)
{
int hashVal = hashFunc(key);
while (hashArray[hashVal] !=
NULL)
{
if
(hashArray[hashVal]->data == key)
return hashArray[hashVal];
++hashVal;
hashVal %=
arraySize;
}
return NULL;
}
};
int main(){
DataItem* pDataItem;
int aKey, keysPerCell;
string stringSize, stringN;
int intSize;
int intN;
time_t aTime;
char choice = 'b';
cout << "Enter size of hash table: ";
cin >> stringSize;
istringstream iss1(stringSize);
iss1 >> intSize;
if (iss1.fail()) {
cout << "Invalid input!!!
Please enter numeric values" << endl;
system("pause");
return 0;
}
cout << "Enter initial number of item less than
or equal to hash table size " << intSize << ": ";
cin >> stringN;
istringstream iss2(stringN);
iss2 >> intN;
if (iss2.fail()) {
cout << "Invalid input!!!
Please enter numeric values" << endl;
system("pause");
return 0;
}
keysPerCell = 10;
if (intN > intSize)
{
cout << "Please enter number
less than or equal to hash table size." << endl;
system("pause");
return 0;
}
HashTable theHashTable(intSize);
srand(static_cast<unsigned>(time(&aTime)));
for (int j = 0; j<intN; j++)
{
aKey = rand() %
(keysPerCell*intSize);
pDataItem = new
DataItem(aKey);
theHashTable.insert(pDataItem);
}
while (choice != 'x')
{
string stringChoice = "";
char choice = NULL;
cout << "Enter first letter
of " << "show, inserts delete, or find: ";
cin >> stringChoice;
int length =
stringChoice.length();
choice = stringChoice[0];
if (length > 1 ||
choice >= 48 && choice <= 57)
{
cout <<
"Invalid input!!! Please enter one letter." << endl;
continue;
}
switch (choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
cout <<
"Enter key value to insert: ";
cin >>
aKey;
pDataItem = new
DataItem(aKey);
pDataItem =
theHashTable.insert(pDataItem);
if (pDataItem !=
NULL)
cout << aKey << " inserted
successfully " << endl;
else
cout << "Hash table is full!" <<
endl;
break;
case 'd':
cout <<
"Enter key value to delete: ";
cin >>
aKey;
pDataItem =
theHashTable.remove(aKey);
if (pDataItem !=
NULL)
cout << aKey << " deleted
successfully " << endl;
else
cout << "Entered key is not found!"
<< endl;
break;
case'f':
cout <<
"Enter key value to find: ";
cin >>
aKey;
pDataItem =
theHashTable.find(aKey);
if (pDataItem !=
NULL)
cout << "Found " << aKey <<
endl;
else
cout << "Could not find " << aKey
<< endl;
break;
default:
cout <<
"Invalid entry\n";
break;
}
}
}
------------New code Screenshot---------------
Test Case 2: SOLVED
If entered initial number of item is greater than hash table size,
It is not showing anything.
It is because in the insert method it is looping.
---------------------------------
Test Case 3:
If entered delete number is not in the hash table, it is not
showing "not found" message.
---------------------------------
Test Case 4:
If inserted more than hash table capacity, It is not showing any
error message and no other options. It is because in the insert
method it is looping.
---------------------------------
Test Case 5:
Enter first letter of show,inserts delete or find is passed single
character: then show correct error output "Invalid Entry". But when
i pass two letter character like "as" it is showing unwanted error
messages.
---------------------------------
Test Case 6:
If hash table size is passed as letter or string then unexpected
error is showing
Type up and get the Hash table on pages 19 - 22 to work. Show your...
Identify the letters and anything associated with it. It is a hash map so please feel free to point out the other important parts beside the arrows. I apologize for the order of the pictures class HashMap private: HashEntry table ; public: HashMap() table-new HashEntry * [TABLE_SIZE]; for (int 1-0; 1< TABLE SIZE: i++) table [i] = NULL; Hash Function int HashFunc(int key) return key % TABLE-SIZE; Insert Element at a key void Insert(int key, int value) int hashHashFunc(key); while...
C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can be used to implement a Set ADT. Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set....
Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...
Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...
Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...
Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...
This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues. In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there is a need for a destructor. To...
In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...
Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner; public class HashtableChaining<K, V> { // Hashtable bucket private ArrayList<HashNode<K, V>> bucket; // Current capacity of the array list private int numBuckets; // current size of the array list private int size; public HashtableChaining(int buckets){ bucket = new ArrayList<>(); numBuckets = buckets; ...
How would you get this lab to work with the numbers 37 14 68 47, the book we are using is Data structures using C++ by D.S. Malik. Please I need help? Just keeps repeating the same question. Code: #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType...