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;
	
}

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

void KeywordList::displayAll()
{
	Node *n;
	for( n = Head; n != NULL; n = n->Next )
	{
		cout <<" slot: "<<hashfunction(n->data)<< " data :" << n->data<< endl;
	}
}

You could create a function that displays all the entries that go in a particular slot.

void DisplaySlot (int slotNumber)
{
     Node *n;
     cout << "slot: " << slotNumber;
     for( n = Head; n != NULL; n = n->Next )
     {
          if (hashfunction(n->data) == slotNumber)
               cout << n->data << ", ";
     }     
     cout << endl;
}

Call this function for every possible slot number from displayAll(). It does a lot more traversing than your original method so you lose some efficiency. It will also display hash slots with no data and leaves an extra comma at the end, though both of these issues can be fixed with a little code tweaking. This is one way to do it, but not the only way.

Thanks man! works perfectly :)
I edited it so it can display all my slots.

void KeywordList::DisplaySlot()
  {
		 Node *n;
		 int slotNumber = 0;
		 for( int i = 0; i <= 13; i++ )
		 {
		 cout << "SLOT : " << slotNumber;	 
		 for( n = Head; n != NULL; n = n->Next )
			 {
				 if ( hashfunction(n->data) == slotNumber )
				 cout << n->data << ", ";
			 }
				 cout << endl;
				slotNumber++;
		 }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.