Question

How do I do this? -> string print() const; Function to output the data, return the...

How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space

this is my code:

template <class elemType>
string hashT<elemType>::print() const
{
for (int i = 0; i < HTSize; i++){
       if (indexStatusList[i] == 1){
       cout <<HTable[i]<< " " << endl;
       }  
}   
}

**********Rest of the code if needed with what the functions do **************************************

  • hashT(int size = 101); The constructor. Create the arrays HTTable and indexStatusList, then size of both of them would be given by the parameter size; initialize each element of the array indexStatusList to 0; length = 0 (hash table is empty); HTSize = size; and the default array size is 101.

  • void insert(const elemType& rec); Function to insert an item in the hash table. The item to be inserted is specified by the parameter rec. Postcondition: If an empty position is found in the hash table, rec is inserted and the length is incremented by one; otherwise, an appropriate error message is displayed.

  • int search(const elemType& rec, bool& found) const; Function to determine whether the item specified by the parameter rec is in the hash table. The function returns the number of access before the method returns. Postcondition: If rec is found, found is set to true; otherwise, found is set to false.

  • void remove(const elemType& rec); Function to remove an item from the hash table. Postcondition: If rec is found in the table it is removed; otherwise, an appropriate error message is displayed.

  • string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space.

  • int hashFunction(elemType key); Hash function that returns the hashed value of the input.

  • ~hashT(); The destructor.

The following private methods are already offered to you in the template:

-- bool isItemAtEqual(int hashIndex, const elemType& rec) const; Function to determine whether the item specified by the parameter rec is the same as the item in the hash table at position hashIndex. Postcondition: Returns true if HTable[hashIndex] == rec; otherwise, returns false.

-- int hashFunction(const elemType& rec) const; This function returns the hash index for a given element rec.

-- int nextProbingPlace(int placenow, int probingtime) const; This function helps you find where the next place is to look into to find out where rec is.

rest of the code if needed.

#include<string>
#include<iostream>
#include <sstream>
using namespace std;
#ifndef HASHT_H
#define HASHT_H

template <class elemType>
class hashT
{
public:
   hashT(int size = 101);

   void insert(const elemType& rec);

   int search(const elemType& rec, bool& found) const;


   void remove(const elemType& rec);

   string print() const;

   ~hashT();
private:

   int nextProbingPlace(int place_now, int probing_time) const;

   int hashFunction(const elemType& key) const;

   bool isItemAtEqual(int hashIndex, const elemType& rec) const;

  

   private:
   elemType *HTable; //pointer to the hash table
   int *indexStatusList; //pointer to the array indicating the
   //status of a position in the hash table
   int length; //number of items in the hash table
   int HTSize; //maximum size of the hash table
};
#endif

template <class elemType>
int hashT<elemType>::hashFunction(const elemType& key) const
{


   ostringstream oss;
   oss<<key;
   string key_str = oss.str();
   int sum = 0;
   for (int j = 0; j < key_str.size(); j++)
       sum = sum + static_cast<int>(key_str.at(j));
   return (sum % HTSize);
}

template <class elemType>
int hashT<elemType>::nextProbingPlace(int place_now, int probing_time) const
{
   int result = (place_now+1)%HTSize;
   return result;
}

template <class elemType>
bool hashT<elemType>::isItemAtEqual(int hashIndex, const elemType& rec) const
{
   //cout<<"Inside"<<indexStatusList[hashIndex]<<endl;
   if(hashIndex<0 || hashIndex>=HTSize || indexStatusList[hashIndex]==0 )
       return false;

   return HTable[hashIndex] == rec;
}

template <class elemType>
hashT<elemType>::hashT(int size)
{
size = 101;
HTable = new elemType[size];
indexStatusList = new int[size];
HTSize = size;
length = 0;
for (int i = 0; i < size; i++) {
indexStatusList[i] = 0;
}
}

template <class elemType>
void hashT<elemType>::insert(const elemType& rec)
{
int inc = 1;
int pCount = 0;
int hashIndex = hashFunction ( rec );
while(indexStatusList[hashIndex] == 1 && HTable[hashIndex] != rec && pCount < HTSize / 2)
   {
       pCount++;
       hashIndex = (hashIndex + inc ) % HTSize;
       inc = inc + 2;
   }

   if(indexStatusList[hashIndex] != 1)
   {
       HTable[hashIndex] = rec;
       indexStatusList[hashIndex] = 1;
       length++;
   }
else if (HTable[hashIndex] == rec)
cout << "Error: No duplicates are allowed."<<endl; // cerr is used for error outputs
else
cout << "Error: The table is full."
<<"Unable to resole collision" << endl;
}

template <class elemType>
int hashT<elemType>::search(const elemType& rec, bool& found) const
{
int hashIndex = hashFunction ( rec ) ;
int pCount = 0;
int inc = 1;
  
   while(indexStatusList[hashIndex]!= 0 && HTable[hashIndex] != rec && pCount < HTSize / 2)
   {
       pCount++;
       hashIndex = (hashIndex + inc ) % HTSize;
       inc = inc + 2;
   }

   if (HTable[hashIndex] == rec && indexStatusList[hashIndex]== 1)
       found = true;
   else
       found = false;
   return pCount;
}

template <class elemType>
void hashT<elemType>::remove(const elemType& rec)
{
int hashIndex = hashFunction ( rec ) ;
int pCount = 0;
int inc = 1;
  
while(HTable[hashIndex] != rec && pCount < HTSize / 2)
   {
       if (indexStatusList[hashIndex] == 0)
           break;
       pCount++;
       hashIndex = (hashIndex + inc ) % HTSize;
       inc = inc + 2;
   }
   if (HTable[hashIndex] == rec)
   {
       HTable[hashIndex] = NULL;
       indexStatusList[hashIndex] = -1;
       length--;
   }
else
{
cout << "Item missing" << endl;
}

}

template <class elemType>
string hashT<elemType>::print() const
{
for (int i = 0; i < HTSize; i++){
       if (indexStatusList[i] == 1){
       cout <<HTable[i]<< " " << endl;
       }  
}   
}


template <class elemType>
hashT<elemType>::~hashT()
{
delete [] HTable;
delete [] indexStatusList;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

template <class elemType>
string hashT<elemType>::print() const
{
   ostringstream oss;
   for (int i = 0; i < HTSize; i++){
if (indexStatusList[i] == 1){
        oss<<HTable[i]<<" ";
}
   }
   return oss.str();
}

Add a comment
Know the answer?
Add Answer to:
How do I do this? -> string print() const; Function to output the data, return the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    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)...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT