Hello all,
I'm new to hash tables etc. and for some schoolwork I need to make one. The hash table and hashfunction itself aren't the problem. The "problem " I wish to solve lies in the display function of my code. I like to write a function so that it can display the strings in the following cout mode.
for example:
slot 0: john , jack , marie , glenn
slot 2: rose, melissa , mike
slot 5: jane, tarzan
slot 8: quincy , peter, marcel , audrey, raines
Below is the code I have so far ( I left the class out as you all can see )
It would be great if someone could help me out with a nice displayfunction;-).
greetings
Node* KeywordList::addToList(string value)
{
Node* n = new Node;
n->data = value;
addTailItem(n);
table[hashfunction(value)] = n;
return n;
}
void KeywordList::addTailItem(Node *n)
{
if (Head == NULL)
{
Head = n;
n->Prev = NULL;
}
else
{
Tail->Next = n;
n->Prev = Tail;
}
Tail = n;
n->Next = NULL;
}
void KeywordList::insertNode(Node *before, Node *after)
{
before->Next = after->Next;
before->Prev = after;
if( after->Next != NULL )
{
after->Next->Prev = before;
}
else
{
Tail = before;
}
after->Next = before;
}
void KeywordList::deleteNode(Node *n)
{
if(n->Prev == NULL )
{
Head = n->Next;
}
else
{
n->Prev->Next = n->Next;
}
if ( n->Next == NULL )
{
Tail = n->Prev;
}
else
{
n->Next->Prev = n->Prev;
}
}
void KeywordList::deleteAllNodes()
{
while( Head != NULL )
deleteNode( Head );
}
void KeywordList::displayAll()
{
Node *n;
for( n = Head; n != NULL; n = n->Next )
{
cout <<" slot: "<<hashfunction(n->data)<< " data :" << n->data<< endl;
}
}
void KeywordList::emptyTable()
{
cout << " emptysymboltable " << endl;
for( int i = 0; i < 20; i++ )
table[i] = NULL;
}
Node* KeywordList::lookup(string value)
{
Node* n;
for( n = Head; n != NULL; n = n->Next )
{
if( value == n->data)
{
return n;
// eventuele cout
}
}
return NULL;
}
unsigned int KeywordList::hashfunction(string s )
{
Node* stringHashing = new Node;
stringHashing->data = s;
int b = 39482;
int c = 11923;
unsigned int hash = 0;
unsigned i;
for( i = 0; i < stringHashing->data.length(); i++ )
{
hash = hash * c + stringHashing->data.at(i);
c = c * b;
}
cout << "hashing value" << c << ": " << (hash & 0x7FFFFFFF) % 13<< endl;
return( hash & 0x7FFFFFFF ) % 13;
}
int main(){
int test = 0;
int list = 14;
string toevoeg;
KeywordList objecttest;
while( test != 10 )
{
cout << "add strings: ";
cin >> toevoeg;
cout << endl;
objecttest.addToList( toevoeg );
test++;
}
objecttest.displayAll();
cin >> toevoeg;
}