954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating a hash table from a linked list

Dear friends,

hello to you all as this is my first thread - question to the forum.

This is my problem, which is a school exersize:

I create a linked list which contains three numbers r,c,d per node

From this list I have to create a hash table htable[7] with hash function
hk=d mod 7

my idea of doing it is through the following code, where htable and fnl are hash tables and curr is a structure of the same type.

while (curr != NULL)
	{
		if (htable[hk]==NULL) {
			htable[hk]=curr;
			fnl[hk]=curr;
		}
		else {
			fnl[hk]->next=curr;
			fnl[hk]=curr;
		}
	curr=curr->next;

doing it what I get as a result is that each of the linked lists of the htable have all the correct nodes AND all the rest of the nodes of the initial linked list from the node the htable[i] should end to the end of the list.

Is any one able to explain why is that happening and how should it be adressed?

Thanks a lot in advance for your time and effort!!!

cuckas
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Could you provide the exact text of the whole exercise?

Martin B
Junior Poster in Training
60 posts since Oct 2010
Reputation Points: 14
Solved Threads: 6
 

let me do this for you,as it seems that we have the same exercise with cuckas. we have a structure struct *cell {int row, int column, int distance, struct cell *next}. Row and column refer to the position of a pawn on a game table. Distance refers to the distance that the pawn may have fro its original position. So, we re asked to find the possible positions that the pawn can move to. By calling a function, this strust *cell is filled with numbers and we finally have something like that:
6 4 0
5 4 0
6 4 1
4 4 2
5 5 2 and so on...As i mentioned earlier,the third number is the distance.So we have to create a hash table struct cell *htable[7]. We have to use distance (d) as the key and the hash function will be h(k)= d mod7. In case of collision we must use the method of hash table with chaining.
All in all,we must write a function QHashTable that will take the elements of the list (by giving it a pointer that refers to the first node) and make the desired hash table. We are also requested to write another function,called QHashTableDisplay that will use the hash table that we created and print the number of nodes that every hash table position has. I hope that you can help!

ambageo
Newbie Poster
9 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: