Question

In: Computer Science

/* * C++ Program to Implement Hash Tables with Quadratic Probing */ #include <iostream> #include <cstdlib>...

/*
* C++ Program to Implement Hash Tables with Quadratic Probing
*/

#include <iostream>
#include <cstdlib>
#define MIN_TABLE_SIZE 10
using namespace std;

//-----------------------------------------------------------------------
// Node Type Declaration
//-----------------------------------------------------------------------
enum EntryType
{
Legi, Emp, Del
};

//-----------------------------------------------------------------------
// Node Declaration
//-----------------------------------------------------------------------
struct HashTableEntry
{
int e;
enum EntryType info;
};

//-----------------------------------------------------------------------
// Table Declaration
//-----------------------------------------------------------------------
struct HashTable
{
int size;
HashTableEntry *t;
};

//-----------------------------------------------------------------------
// Function: isPrime Function
// Return whether n is prime or not
//-----------------------------------------------------------------------
bool isPrime (int n) //Complete the function stubs needed to implement the operations
{
if( n == 2 || n == 3 )
return true;
  
if( n == 1 || n % 2 == 0 )
return false;
  
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
return false;
  
return true;
}

//-----------------------------------------------------------------------
// Function: nextPrime Function
// Finding next prime size of the table
//-----------------------------------------------------------------------
int nextPrime(int n)
{
if (n % 2 == 0)
++n;
while (!IsPrime(n)) n += 2;
return n;
}

//-----------------------------------------------------------------------
// Function: Hash Function
//-----------------------------------------------------------------------
int HashFunc(int key, int size) //Complete the function stubs needed to implement the operations
{

}

//-----------------------------------------------------------------------
// Function: initiateTable Function
// Initialize Table
//-----------------------------------------------------------------------
HashTable *initiateTable(int size)
{
HashTable *ht;
if (size < MIN_TABLE_SIZE)
{
cout<<"Table Size is Too Small"<<endl;
return NULL;
}
ht= new HashTable;
  
if (ht == NULL)
{
cout<<"Out of Space"<<endl;
return NULL;
}
ht->size = nextPrime(size);
ht->t = new HashTableEntry [ht->size];
  
if (ht->t == NULL)
{
cout<<"Table Size is Too Small"<<endl;
return NULL;
}
  
for (int i = 0; i < ht->size; i++)
{
ht->t[i].info = Emp;
ht->t[i].e = 0;
}
return ht;
}

//-----------------------------------------------------------------------
// Function: Search Element at a key
//-----------------------------------------------------------------------
int SearchKey(int key, HashTable *ht) //Complete the function stubs needed to implement the operations
{

}

//-----------------------------------------------------------------------
// Function: Insert Element at a key
//-----------------------------------------------------------------------
void Insert(int key, HashTable *ht) //Complete the function stubs needed to implement the operations
{

}

//-----------------------------------------------------------------------
// Function: Rehash
//-----------------------------------------------------------------------
HashTable *Rehash(HashTable *ht) //Complete the function stubs needed to implement the operations
{

}

//-----------------------------------------------------------------------
// Function: Display Hash Table
//-----------------------------------------------------------------------
void display(HashTable *ht)
{
for (int i = 0; i < ht->size; i++)
{
int value = ht->t[i].e;
if (!value)
cout<<"Position: "<<i + 1<<" Element: Null"<<endl;
else
cout<<"Position: "<<i + 1<<" Element: "<<value<<endl;
}
}

Solutions

Expert Solution

Sharing the code for Implementing Hash Tables with Quadratic Probing:

#include <iostream>
#include <cstdlib>
#define MIN_TABLE_SIZE 10
using namespace std;
/*
* Node Type Declaration
*/
enum EntryType {Legitimate, Empty, Deleted};
/*
* Node Declaration
*/
struct Node
{
int element;
enum EntryType info;
};
/*
* Table Declaration
*/
struct HashTable
{
int size;
Node *table;
};
/*
* Returns whether n is prime or not
*/
bool isPrime (int x)
{
if (x == 2 || x == 3)
return true;
if (x == 1 || x % 2 == 0)
return false;
for (int i = 3; i * i <= x; i += 2)
if (x % i == 0)
return false;
return true;
}
/*
* Finding next prime size of the table
*/
int nextPrime(int n)
{
if (n <= 0)
n == 3;
if (n % 2 == 0)
n++;
for (; !isPrime( n ); n += 2);
return n;
}
/*
* Function To Generate Hash
*/
int HashFunc(int key, int size)
{
return key % size;
}
/*
* Function to Initialize Table
*/
HashTable *initializeTable(int size)
{
HashTable *htable;
if (size < MIN_TABLE_SIZE)
{
cout<<"Table Size Too Small"<<endl;
return NULL;
}
htable = new HashTable;
if (htable == NULL)
{
cout<<"Out of Space"<<endl;
return NULL;
}
htable->size = nextPrime(size);
htable->table = new Node [htable->size];
if (htable->table == NULL)
{
cout<<"Table Size Too Small"<<endl;
return NULL;
}
for (int i = 0; i < htable->size; i++)
{
htable->table[i].info = Empty;
htable->table[i].element = NULL;
}
return htable;
}
/*
* Function to Find Element at a key
*/
int Find(int key, HashTable *htable)
{
int pos = HashFunc(key, htable->size);
int collisions = 0;
while (htable->table[pos].info != Empty &&
htable->table[pos].element != key)
{
pos = pos + 2 * ++collisions -1;
if (pos >= htable->size)
pos = pos - htable->size;
}
return pos;
}
/*
* Function to Insert Element into a key
*/
void Insert(int key, HashTable *htable)
{
int pos = Find(key, htable);
if (htable->table[pos].info != Legitimate)
{
htable->table[pos].info = Legitimate;
htable->table[pos].element = key;
}
}
/*
* Function to Rehash the Table
*/
HashTable *Rehash(HashTable *htable)
{
int size = htable->size;
Node *table = htable->table;
htable = initializeTable(2 * size);
for (int i = 0; i < size; i++)
{
if (table[i].info == Legitimate)
Insert(table[i].element, htable);
}
free(table);
return htable;
}
/*
* Function to Retrieve Hash Table
*/
void Retrieve(HashTable *htable)
{
for (int i = 0; i < htable->size; i++)
{
int value = htable->table[i].element;
if (!value)
cout<<"Position: "<<i + 1<<" Element: Null"<<endl;
else
cout<<"Position: "<<i + 1<<" Element: "<<value<<endl;
}

}
/*
* Main Contains Menu
*/
int main()
{
int value, size, pos, i = 1;
int choice;
HashTable *htable;
while(1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Quadratic Probing"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"1.Initialize size of the table"<<endl;
cout<<"2.Insert element into the table"<<endl;
cout<<"3.Display Hash Table"<<endl;
cout<<"4.Rehash Table"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<" size of the Hash Table: ";
cin>>size;
htable = initializeTable(size);
cout<<"Size of Hash Table: "<<nextPrime(size);
break;
case 2:
if (i > htable->size)
{
cout<<"Table is Full, Rehash the table"<<endl;
continue;
}
   cout<<"Enter element to be inserted: ";
   cin>>value;
Insert(value, htable);
i++;
break;
case 3:
Retrieve(htable);
break;
case 4:
htable = Rehash(htable);
break;
case 5:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}


Related Solutions

Can someone please write me a header file in c++ using the quadratic probing hash method...
Can someone please write me a header file in c++ using the quadratic probing hash method that will work with this program? #include "hash.h" #include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <list> using namespace std; const size_t KEYS_COUNT = 1000; // Number of keys to generate for testing string random_key(); class HashCheck { private: size_t count; Hash hash; public: HashCheck(Hash& h) { count = 0; hash = h; } void operator() (const string key) { if (hash[key] !=...
Please review the hash technique. I give you this phrase : If quadratic probing is used,...
Please review the hash technique. I give you this phrase : If quadratic probing is used, and the table size is prime, then a new element can always be inserted if the table is at least half empty. If you disagree with this phrase, please give or provide a counter example to prove that it is in fact an incorrect statement. However, if you think that is it right, please show your reasoning behind it and prove it. Please type...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2 files (table2.h, short story text file) are all fully coded and commented for convenience. *****I have given references that I've completed previously for the table2.cpp file, I just need help applying them. Will provide good rating, thanks****** -------------------------------------------------------------------------------------------------------------------------- table2.h (given file, do not edit): // FILE: table2.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is...
fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib>...
fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib> #include <ctime> void insertion_sort(int array[], int size, int start); void heap_sort(int B[], int n); void build_max_heap(int B[], int n); void max_heapify(int B[], int i, int n); void quick_sort(int B[], int p, int r); int partition(int B[], int p, int r); int main() {    int m = 10, Nf = 20000, Ns = 1000, delta = 1000, A[m][Nf];    for (int i = 0;...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string>...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string> #include <vector> #include <iomanip> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int cash_type, block_size, cash_size,number_of_blocks=0;; int compulsry_misses=0, capcity_misses=0, conflict_misses=0; #define DRAM_SIZE (64*1024*1024) unsigned int m_w = 0xABABAB55; /* must not be zero, nor 0x464fffff */ unsigned int m_z = 0x05080902; /* must not be zero, nor 0x9068ffff */ unsigned int rand_() { m_z = 36969 * (m_z & 65535) + (m_z >>...
Convert this C++ to js/html and run in browser. ///////////////////////////////////////////////// #include <cstdlib> #include <ctime> #include <sstream>...
Convert this C++ to js/html and run in browser. ///////////////////////////////////////////////// #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is...
C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For...
C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" //Converting hexadecimal to binary int main() {    char binarynum[65], hexa[17];    //Using long int because it has greater capacity    long int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT