Question
can anybody debug this program for me please #include #include #include #define MIN_TABLE_SIZE 10 using namespace std; struct hashNode{ string stateName; hashNode *next; }; /*
can anybody debug this program for me please
#include
#include
#include
#define MIN_TABLE_SIZE 10
using namespace std;
struct hashNode{
string stateName;
hashNode *next;
};
/*
* Node Type Declaration
*/
enum EntryType {Legitimate, Empty, Deleted};
/*
* Node Declaration
*/
struct HashNode
{
int element;
enum EntryType info;
};
/*
* Table Declaration
*/
struct HashTable
{
int size1;
HashNode *table;
};
/*
* Returns whether n is prime or not
*/
bool isPrime (int n)
{
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;
}
/*
* 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 size1)
{
return key % size1;
}
/*
* Function to Initialize Table
*/
HashTable *initializeTable(int size1)
{
HashTable *htable;
if (size1 < MIN_TABLE_SIZE)
{
cout<<"Table Size Too Small"< return NULL; } htable = new HashTable; if (htable == NULL) { cout<<"Out of Space"< return NULL; } htable->size1 = nextPrime(size1); htable->table = new HashNode [htable->size1]; if (htable->table == NULL) { cout<<"Table Size Too Small"< return NULL; } for (int i = 0; i < htable->size1; 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->size1); int collisions = 0; while (htable->table[pos].info != Empty && htable->table[pos].element != key) { pos = pos + 2 * ++collisions -1; if (pos >= htable->size1) pos = pos - htable->size1; } 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 size1 = htable->size1; HashNode *table = htable->table; htable = initializeTable(2 * size1); for (int i = 0; i < size1; 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->size1; i++) { int value = htable->table[i].element; if (!value) cout<<"Position: "< else cout<<"Position: "< } } /* * Main Contains Menu */ int main() { hashNode *array[15]; for (int j=0; j<15; j++) { array[j]->stateName =" "; array[j]->next=NULL; } int size=0; string cName; ifstream myfile; myfile.open("a3.txt"); myfile>>cName; while(!myfile.eof()) { size=sizeof(cName); if (array[size]->stateName != " ") { hashNode *temp=new hashNode(); temp->stateName=cName; temp->next=NULL; if(array[size]->next==NULL) { array[size]->next=temp; } else { hashNode *temp2; temp2=array[size]; while(temp2->next!=NULL) { temp2=temp2->next; } temp2->next=temp; } } else{ array[size]->stateName=cName; array[size]->next=NULL; } myfile>>cName; } for(int i=0; i<15;i++) { cout< while(array[i]->next!=NULL) { cout< } cout< } int value, size1, pos, i = 1; int choice; HashTable *htable; while(1) { cout<<" ----------------------"< cout<<"Operations on Quadratic Probing"< cout<<" ----------------------"< cout<<"1.Initialize size of the table"< cout<<"2.Insert element into the table"< cout<<"3.Display Hash Table"< cout<<"4.Rehash The Table"< cout<<"5.Exit"< cout<<"Enter your choice: "; cin>>choice; switch(choice) { case 1: cout<<"Enter size of the Hash Table: "; cin>>size1; htable = initializeTable(size1); cout<<"Size of Hash Table: "< break; case 2: if (i > htable->size1) { cout<<"Table is Full, Rehash the table"< 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<<" Enter correct option "; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started