C++:PLEASE HELP CODE: #include <iostream> #include \"SLL.h\" using namespace std
ID: 3711034 • Letter: C
Question
C++:PLEASE HELP
CODE:
#include <iostream>
#include "SLL.h"
using namespace std;
template <class V>
class HashTable {
int tableSize; // table size
SLL<V>* table;
public:
// default constructor, which uses default table size 3
HashTable(){
tableSize = 3;
table = new SLL<V>[tableSize];
}
// constructor, which use size as the table size
HashTable(int size){
// implement this method
}
// search item in the table
// if found, return true; otherwise, return false
bool find(V item){
// implement this method
}
// insert (item1, item2) to the table
// use item1 as the key
// if inserted, return true
// otherwise, return false
bool insert(V item1, V item2){
//implement this method
}
// delete the pair whose key value is item
// if deleted, return true
// otherwise, return false
bool erase(V item){
// implement this method
}
// return the total number of nodes in the hash table
int getSize(){
// implement this method
}
};
Implement HashTable class, which is class template. The default constructor uses 3 as the table size. The constructor with parameter uses the parameter value as the table size. Do not hard-code array size in your constructors. Let the user decide what array size will be.
SLL.h
#include <iostream>
#include <iostream>
#include "node.h"
using namespace std;
template <class U>
class SLL {
Node<U> * headPtr;
int size;
public:
// default constructor
SLL(){
headPtr = nullptr;
size = 0;
}
// destructor
~SLL(){
Node<U> *next;
while(headPtr != nullptr)
{
next = headPtr->next;
delete headPtr;
headPtr = next;
}
}
Node<U>* getHeadPtr(){
return headPtr;
}
// insert (item1, item2) to the list
void insert(U item1, U item2){
Node<U> *n = new Node<U>();
n->SSN = item1;
n->name = item2;
n->next = nullptr;
if(headPtr == nullptr)
headPtr = n;
else
{
Node<U> 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<U>* search(U item1){
Node<U> *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<U> *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<U>* temp;
temp = headPtr;
while (temp!= nullptr) {
cout << temp->SSN << endl;
temp = temp->next;
}
}
};
Explanation / Answer
hashtable.h
#include <iostream>
#include "SLL.h"
#include <string>
#include <cmath>
using namespace std;
template <class V>
class HashTable
{
int tableSize; // table size
SLL<V>* table;
public:
// default constructor, which uses default table size 3
HashTable()
{
tableSize = 3;
table = new SLL<V>[tableSize];
}
// constructor, which use size as the table size
HashTable(int size)
{
tableSize = size;
table = new SLL<V>[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<string>* 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<string>* temp1 = &table[compressVal];
if(temp1->search(item1) != NULL)
{
return false;
}
SLL<string>* temp2 = &table[compressVal];
temp2->insert(item1, item2);
SLL<string>* 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<string>* 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<string>* temp = &table[i];
nodeCount = nodeCount + temp->getSize();
}
return nodeCount;
}
};
SLL.h
#include <iostream>
#include "Node.h"
using namespace std;
template <class U>
class SLL {
Node<U> * headPtr;
int size;
public:
// default constructor
SLL()
{
headPtr = NULL;
size = 0;
}
// destructor
~SLL()
{
Node<U> * dest = headPtr;
while(dest != NULL)
{
Node<U> * temp = dest->next;
delete dest;
dest = temp;
}
}
Node<U>* getHeadPtr()
{
return headPtr;
}
// insert (item1, item2) to the list
void insert(U item1, U item2)
{
Node<U> * newPtr = new Node<U>;
newPtr->SSN = item1;
newPtr->name = item2;
newPtr->next = NULL;
if(headPtr == NULL)
{
headPtr = newPtr;
}
else
{
Node<U> * temp = headPtr;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newPtr;
}
}
// if find the item1 value, return the pointer to the node
// otherwise, return nullptr
Node<U>* search(U item1)
{
Node<U> * temp = headPtr;
if(headPtr == NULL)
{
return NULL;
}
while(temp != NULL)
{
if(temp->SSN == item1)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
// remove the node with key value: item1
bool remove(U item1)
{
Node<U> * temp = headPtr;
if(headPtr == NULL)
{
return false;
}
if(temp->SSN == item1)
{
headPtr = headPtr->next;
return true;
}
while(temp->next != NULL)
{
if(temp->next->SSN == item1)
{
temp->next = temp->next->next;
return true;
}
temp = temp->next;
}
return false;
}
int getSize()
{
return size;
}
// display the SSN values of each node in the linked list
void display()
{
Node<U>* temp;
temp = headPtr;
while (temp!= NULL)
{
cout << temp->SSN << endl;
temp = temp->next;
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.