Hello guys :)
I hope all are alright ;)
I'm using HashTable to make a program that reads a string from a file..
when I run it, it gives me the message of error (Don't Send) << I guess all of you know this message..
this is my code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Node
{
public:
int data;
int count;
Node *link;
Node()
{
data = count = 0;
link = 0;
}
Node( int x )
{
data = x;
count = 0;
link = 0;
}
}; // end class Node
class HashTable
{
private:
Node *HT, *tmp;
int tSize;
public:
HashTable( int x )
{
HT = new Node[x];
tSize = x;
tmp = 0;
} // end constructor for HashTable
void insert( int x )
{
int loc;
loc = x % tSize;
if( HT[loc].count == 0 )
{
HT[loc].data = x;
HT[loc].count++;
}
else
if( x > HT[loc].data )
{
if(HT[loc].count == 1)
{
HT[loc].link = new Node(x);
HT[loc].count++;
}
else if(HT[loc].link->data > x)
{
tmp = new Node(x);
tmp->link = HT[loc].link;
HT[loc].link = tmp;
HT[loc].count++;
}
else
{
Node * tmp2;
for(tmp = HT[loc].link, tmp2=tmp->link; tmp2; tmp2 = tmp, tmp = tmp->link)
if(tmp2->data > x)
break;
Node *tmp3 = new Node(x);
tmp3->link = tmp2;
tmp->link = tmp3;
HT[loc].count++;
}
}
else
if( x < HT[loc].data )
{
if(HT[loc].count == 1)
{
HT[loc].link = new Node(HT[loc].data);
HT[loc].data = x;
HT[loc].count++;
}
else
{
tmp = new Node(HT[loc].data);
HT[loc].data = x;
tmp->link = HT[loc].link;
HT[loc].link = tmp;
HT[loc].count++;
}
}
} // end function insert
bool search( int x, int &y )
{
y = x %tSize;
if( HT[y]. data == x )
{
cout << "\nNumber found at location " << y << endl;
return true;
}
else{
for( tmp = HT[y].link; tmp; tmp = tmp ->link)
{
if(tmp->data == x)
{
cout << "\nNumber found at location " << y << endl;
return true;
}
} // end for loop for searching an element entered by the user
}
cout << "\nNumber was not found\n";
return false;
} // end function search
void print()
{
cout << "\nLocation Entry(s)\n";
cout << "========= ========\n";
for( int i = 0; i < tSize; i++ )
{
cout << "HT [" << i << "] " << HT[i].data;
for( tmp = HT[i].link; tmp; tmp = tmp ->link)
{
cout << " -> " << tmp -> data;
}
cout << endl;
} // end for loop
int z = 0;
int u;
for( int j = 0; j < tSize; j++ )
{
if( HT[j].count > 0 )
{
z++;
}
}
u = (z/tSize) * 100;
cout << "\nThe utilization is " << u;
cout << endl;
cout << "The number of elements in the HashTable is ";
int w = 0;
for( int k = 0; k < tSize; k++ )
{
w+=HT[k].count;
}
cout << w;
cout << endl;
} // end print
};
int main()
{
HashTable hi(4);
string n;
int sum = 0;
// constructor opens the file
ifstream fin( "dictionary.txt" );
// if statement to check if the file opens
if( !fin )
{
cerr << "File cannot be opened..\n";
exit(1);
} // end if statement
while( fin >> n )
{
for( int i = 0; i < int( n.length() ); i++ )
{
sum += n[ i ];
hi.insert( sum );
}
}
cout << endl;
return 0;
}
What I'm doing is using (While loop) to read the string from a file, then convert it to integer then call the function (insert) that'll insert the string in the hashtable
but I don't know where's the problem of getting that message of error!!
I hope you'll help me finding it.. :)
thanks in advance :)