Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i am trying to write a function that lists all the student records in linked lists in the hash index. The records are printed in the following order: the records in the list headed by hashtable[0] are printed first in the ascending order of the student_id, then the records in the list headed by hashtable[1] are printed in the ascending order of the student_id and so on.. the order of the printed records depends on the positions of the records in the hash index. Each record is printed in the following format :
student_id:lastname:firstname:gpa
if there is no record in the hash index, print:
no record
the function is :
void listRecords(Student **hashtable);
So this is what i've done so far but it still doesn't work:

C++
void listRecords(Student **hashtable)
{
int i, j;
Student temp;
Student *s;
for(i=0; i<HASHTABLESIZE; i++)
{
for(j=i; j<HASHTABLESIZE; j++)
{
if(s[i].student_id > s[j].student_id)
{
temp = s[j];
s[j] = s[i];
s[i] = temp;
}printf("%d:%s:%s:%.1f\n", s[i].student_id, s[i].lastname, s[i].firstname, s[i].gpa);
}
}printf("no record\n");
}




thanks a lot
Posted
Updated 20-Mar-10 10:17am
v3

1 solution

There seems to be several problems with your code. I'm not entirely clear on how your hash table is set up. There seems to be some confusion in your code. As far as I can tell:

1) Your variable s is not initialized, so it has a garbage value and using it is undefined behavior.

2) Presumably, there is no guarantee that each hashtable slot has actual entries. The slot could be empty. Your code doesn't deal with this.

3) The text of your question states that you are using a linked list for each hash table slot, but your code seems to be written as if each slot is a full, fixed-size array with the same number of slots as the hash table had. If it is a linked list, your code needs to handle it that way. If it is really an array in each slot, you still have to have some way to determine the actual number of valid entries in it.

4) Your attempt to sort the entries for each hash slot will need attention after you have resolved the previous issues.

You may want to split this up into functions to keep the organization and responsibilities clearer. Perhaps your listRecords could delegate responsibility for a hash slot to a listSlotRecords function. You might also want to separate out a sortSlotRecords function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900