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;...
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
Are my codes correct? #include <iostream> #include <string> #include <cassert> #include <cctype> #include <cstring> #include <stdio.h>...
Are my codes correct? #include <iostream> #include <string> #include <cassert> #include <cctype> #include <cstring> #include <stdio.h> #include <ctype.h> #include<fstream> int locateMinimum( const std::string array[ ], int n ){    if(n <= 0){    return -1;    }    else{    int minInd = 0;    for(int i = 0; i < n; ++i){    if(array[i] < array[minInd]){    minInd = i;    }    }    return minInd;    } } int countPunctuation( const std::string array[], int n) { int...
convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include...
convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include <stdlib.h> unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT