Question

In: Computer Science

//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include...

//BEGIN--TABLE.H--

#ifndef TABLE_H
#define TABLE_H

#include        // Provide string
#include // Provide hash
#include        // Provide list
using namespace std;

class Table
{
public:
   // MEMBER CONSTANT
   static const unsigned int TABLE_SIZE = 13;

   Table() { total_records = 0; }
   virtual ~Table() { }

   unsigned int get_total() { return total_records; }

   virtual void insert(string key) =0;
   virtual void print() =0;
  
protected:
   unsigned int total_records;

   // HELPER FUNCTION
   unsigned int hashcode(string key) const
   {
       hash thishash;
       return thishash(key) % TABLE_SIZE;
   }
};

class ChainTable : public Table
{
public:
   ChainTable() {}

   virtual void insert(string key);
   virtual void print();
  
private:
   list datatable[TABLE_SIZE];
};

class QuadTable : public Table
{
public:
   QuadTable() {}
  
   virtual void insert(string key);
   virtual void print();
   bool full() { return total_records == TABLE_SIZE; }
  
private:
   string datatable[TABLE_SIZE];
};

#endif

//END TABLE.H--

//BEGIN_TABLE1.CPP---

#include
#include "table1.h"

void ChainTable::insert(string key)
{
   //-----------------------------------------------
   // TO-DO: insert key into the chained hash table
   // ------
   // 1. use hashcode function to calculate bucket index i

   //   2. check if key is already in the list at datatable[i];
   //   - if yes, do nothing
   //   - if no, insert key into the list, increment total_records
  
  
  
  
  
}

// print table contents visually
void ChainTable::print()
{
   cout << "ChainTable content: \n";
   if (total_records==0)
   {
       cout << "\t Empty!\n";
       return;
   }

   for (unsigned int i=0; i    {  
       if (datatable[i].empty())
           continue;

       cout << "\t Entry " << i << ": ";
       for (list::iterator it = datatable[i].begin(); it != datatable[i].end(); it++)
       {
           cout << (*it) << " -> ";
       }
       cout << "NULL\n";
   }
}

//////////////////////////////////////////////////
void QuadTable::insert(string key)
{
   //--------------------------------------------------------------
   // TO-DO: insert key into the hash table using quadratic probing
   // ------
   // 1. if hash table is full, do nothing and return
   //   2. use hashcode function to calculate array index i
   //   3. check if datatable[i] is empty
   //   - if yes, insert key at datatable[i]
   //   - if no, use quadratic probing with probe function c(i)=i^2,
   //       until an empty location is found, insert key at that location
   //   4. increment total_records
  
  
  
}

// print table contents visually
void QuadTable::print()
{
   cout << "QuadTable content: \n";
   if (total_records==0)
   {
       cout << "\t Empty!\n";
       return;
   }

   for (unsigned int i=0; i    {  
       if (datatable[i].empty())
           continue;

       cout << "\t Entry " << i << ": ";
       cout << datatable[i] << endl;
   }
}

//End Table1.cpp--

Solutions

Expert Solution

Chain table insert function .. append these lines in insert function.

void ChainTable::insert(string key)
{
   //-----------------------------------------------
   // TO-DO: insert key into the chained hash table
   // ------
   // 1. use hashcode function to calculate bucket index i
   unsigned int index=hashcode(key)
   //   2. check if key is already in the list at datatable[i];
   //   - if yes, do nothing
   if(datatable[index])
        return ;
   //   - if no, insert key into the list, increment total_records
   else
   {
       datatable[index]=key;
       total_records++;
   }
}

Quad table insert function. Plugin these lines ..

void QuadTable::insert(string key)
{
   //--------------------------------------------------------------
   // TO-DO: insert key into the hash table using quadratic probing
   // ------
   // 1. if hash table is full, do nothing and return
   if(get_total==TABLE_SIZE)
    return;
   //   2. use hashcode function to calculate array index i
   unsigned int index=hashcode(key)
   //   3. check if datatable[i] is empty
   if(datatable[index].empty())
   //   - if yes, insert key at datatable[i]
       datatable[index]=key;
   //   - if no, use quadratic probing with probe function c(i)=i^2,
   //       until an empty location is found, insert key at that location
   for(int i=0;i<TABLE_SIZE;i++)
   {
       int t=(index+i*i)%TABLE_SIZE;
       if(!datatable[t].empty())
       {
           datatable[t]=key;
           break;
       }
   }
   //   4. increment total_records
    table_records++;
    return;  
}

Related Solutions

I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef>...
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef> #include <iostream> struct node { int value; node* next; node(int value, node* next = nullptr) { this->value = value; this->next = next; } }; class mystack { private: node* stack_top; size_t stack_size; public: mystack(); mystack(const mystack& x); ~mystack(); mystack& operator=(const mystack& x); size_t size() const; bool empty() const; void clear(); const int& top() const; void push(int value); void pop(); void clone(const mystack& x); };...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int main() {    const char* filename = "samples.coe"; const int N = 1024; FILE* file = fopen(filename, "w"); if (file == NULL) { perror("fopen"); } fprintf(file, "; These are 1024 sample values in range -1 to 1,\n"); fprintf(file, "; Sine Wave 0\n"); fprintf(file, "memory_initialization_radix = 10;\n"); fprintf(file, "memory_initialization_vector\n"); double values[N]; double delta = M_PI / (N - 1); for (int i = 0; i...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /*     This code closely mimics the Python hash table we created in class this     week. Each "person" is defined by their name, zipcode, and their pet's name.     Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int); //---------------------------------------------------------------------------- /* define a "person" --------------------*/ class person{     public:     // variables that define a person     string name;     int zipcode;...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public:...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public: /** * MyVector * * This is the default constructor that sets size equal * to 0 and capacity to 10. * * Parameters: none * * Output: * return: none * reference parameters: none * stream: none */ MyVector(); /** * ~MyVector * * This is the destructor that deletes memory * * Parameters: none * * Output: * return: none * reference...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start of the marco guard // Try not to change this file #include <iostream> #include <cmath> using std::cout; using std::endl; using std::istream; using std::ostream; class Point { private:    double x, y, z; public:    // Constructors    Point();    Point(double inX, double inY, double inZ = 0);    Point(const Point& inPt);    // Get Functions    double getX() const;    double getY() const;   ...
define aray and string?
define aray and string?
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
Define “republic” in your own words; include 3 references and provide an example
Define “republic” in your own words; include 3 references and provide an example
Modify the SimpleVector by doubling the arraysize limit. --------SimpleVector.h------- #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H // SimpleVector class...
Modify the SimpleVector by doubling the arraysize limit. --------SimpleVector.h------- #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H // SimpleVector class template #include #include // Needed for bad_alloc exception #include // Needed for the exit function using namespace std; template class SimpleVector { private: T *aptr; // To point to the allocated array int arraySize; // Number of elements in the array void memError(); // Handles memory allocation errors void subError(); // Handles subscripts out of range public: // Default constructor SimpleVector() { aptr =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT