使用Hashindex和结构的ListRecords [英] ListRecords using Hashindex and structures

查看:88
本文介绍了使用Hashindex和结构的ListRecords的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试编写一个函数,该函数在哈希索引的链接列表中列出所有学生记录.记录按以下顺序打印:以hashtable [0]开头的列表中的记录首先按student_id的升序打印,然后以hashtable [1]开头的列表中的记录按以下顺序升序打印: student_id等...打印记录的顺序取决于哈希索引中记录的位置.每条记录以以下格式打印:
student_id:lastname:firstname:gpa
如果哈希索引中没有记录,请打印:
没有记录
功能是:
void listRecords(Student **hashtable);
因此,这是我到目前为止所做的,但仍然没有用:

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:

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

推荐答案

您的代码似乎有几个问题.对于您的哈希表的设置方式,我还不太清楚.您的代码似乎有些混乱.据我所知:

1)您的变量 s 未初始化,因此它具有垃圾值,使用它是未定义的行为.

2)大概不能保证每个哈希表槽都有实际的条目.该插槽可能是空的.您的代码不处理此问题.

3)您的问题文本指出您为每个哈希表插槽使用一个链表,但是您的代码似乎写成好像每个插槽都是一个完整的,固定大小的数组,并且插槽数与哈希表相同有.如果它是一个链表,则您的代码需要以这种方式进行处理.如果它确实是每个插槽中的一个数组,则仍然必须有某种方法来确定其中的有效条目的实际数量.

4)解决了先前的问题后,尝试对每个哈希槽的条目进行排序的尝试将需要引起注意.

您可能希望将其分解为功能,以使组织和职责更加清晰.也许您的listRecords可以将哈希槽的职责委派给listSlotRecords函数.您可能还想分离出sortSlotRecords函数.
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.


这篇关于使用Hashindex和结构的ListRecords的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆