struct ZippedBookNode{ string title; string body; hoffnode* huff_root; ZippedBoo
ID: 3917870 • Letter: S
Question
struct ZippedBookNode{
string title;
string body;
hoffnode* huff_root;
ZippedBookNode *next;
ZippedBookNode()
{
}
ZippedBookNode(std::string name, string e, HuffNode* hr, ZippedBookNode* n)
{
title=name;
c_excerpt=e;
huff_root=hr;
next=n;
}
};
using namespace std;
template
HashTable::HashTable() {
// for(int i = 0; i< ARRAY_LEN; i++){
// lookupTable[i] = new ZippedBookNode("empty",NULL,NULL,NULL);
// }
tableSize = ARRAY_LEN;
// lookupTable[ARRAY_LEN];
// lookupTable= new ZippedBookNode*[tableSize];
for(int j = 0; j < tableSize; j++){
lookupTable[j] = NULL;
}
}
template
HashTable::~HashTable() {
stack wordItemStack;
for(int i = 0; i
ZippedBookNode* c;
for(ZippedBookNode*c=lookupTable[i]; c; c=c->next) wordItemStack.push(c);
while(!wordItemStack.empty()){
c=wordItemStack.top();
delete c;
wordItemStack.pop();
}
}
}
template
int HashTable::getTableSize() {
if(ARRAY_LEN <= 0){
return -1;
}
else{
tableSize = ARRAY_LEN;
return tableSize;
}
}
template
void HashTable::insertNode(ZippedBookNode* node) {
//int index=hashSum(node->title,ARRAY_LEN)
int sum = hashSum(node->title);
//ZippedBookNode *newNode=new ZippedBookNode("empty",nullptr,nullptr,node);
if(!lookupTable[sum]){
lookupTable[sum]=node;
}
else{
ZippedBookNode* tmp=lookupTable[sum];
while(tmp->next){
tmp=tmp->next;
}
tmp->next=node;
}
}
template
void HashTable::printTitles() {
ZippedBookNode *current = new ZippedBookNode;
for (int i = 0; i < ARRAY_LEN; i++)
{
if (lookupTable[i] != NULL){
current = lookupTable[i];
cout <title<
while(current -> next != NULL)
{
current = current -> next;
cout<title<
}
}
}
}
** I am having some error while adding node, can you help me out with this hashtable. ***DONT CHANGE ANY FUNCTION*** I just want insertNode and printTitiles working or if there is any other error. For hoffnode, you don't have to worry just treat it as an external node.**
eg:
************************ suppose a text file of those stuff**********************
#titl1
stuff1
#title2
stuff2
#title3
stuff 3
************************ suppose a text file of those stuff**********************
basic code------>
ZippedBookNode *new;
new->title=title;
new->body=body;
new->huff_root=NULL;
new->next=NULL;
insertnode(new);
printTitle();
output:::
title1
title2
title3
....................................................................
Thank you
Explanation / Answer
Your code is incomplete. I hope you have necessary code for HashTable class. I have resolved most of the errors. You will find it useful now. Complete your code with the below resolved code. Additional headers are included in the code
/*****************************/
#include <string>
#include <iostream>
#include <stack>
#define ARRAY_LEN 10
using namespace std;
struct ZippedBookNode{
string title;
string body;
hoffnode* huff_root;
ZippedBookNode *next;
ZippedBookNode()
{
}
ZippedBookNode(std::string name, string e, HuffNode* hr, ZippedBookNode* n)
{
title=name;
c_excerpt=e;
huff_root=hr;
next=n;
}
};
class HashTable{
int tableSize;
ZippedBookNode* lookupTable[10];
};
template
HashTable::HashTable() {
// for(int i = 0; i< ARRAY_LEN; i++){
// lookupTable[i] = new ZippedBookNode("empty",NULL,NULL,NULL);
// }
tableSize = ARRAY_LEN;
// lookupTable[ARRAY_LEN];
// lookupTable= new ZippedBookNode*[tableSize];
for(int j = 0; j < tableSize; j++){
lookupTable[j] = NULL;
}
}
template
HashTable::~HashTable() {
stack<ZippedBookNode*> wordItemStack;
for(int i = 0; i<10;i++){
ZippedBookNode* c;
for(ZippedBookNode*c=lookupTable[i]; c; c=c->next) wordItemStack.push(c);
while(!wordItemStack.empty()){
c=wordItemStack.top();
delete c;
wordItemStack.pop();
}
}
}
template
int HashTable::getTableSize() {
if(ARRAY_LEN <= 0){
return -1;
}
else{
tableSize = ARRAY_LEN;
return tableSize;
}
}
template
void HashTable::insertNode(ZippedBookNode* node) {
//int index=hashSum(node->title,ARRAY_LEN)
int sum = hashSum(node->title);
//ZippedBookNode *newNode=new ZippedBookNode("empty",nullptr,nullptr,node);
if(!lookupTable[sum]){
lookupTable[sum]=node;
}
else{
ZippedBookNode* tmp=lookupTable[sum];
while(tmp->next){
tmp=tmp->next;
}
tmp->next=node;
}
}
template
void HashTable::printTitles() {
ZippedBookNode *current = new ZippedBookNode;
for (int i = 0; i < ARRAY_LEN; i++)
{
if (lookupTable[i] != NULL){
current = lookupTable[i];
cout <<"title"<<endl;
while(current -> next != NULL)
{
current = current -> next;
cout<<"title"<<endl;
}
}
}
}
/*****************************/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.