散列(带链)学生数据库(位折叠/加法散列) [英] Hashing(with chains) a student database (bit folding/ additive hashing)

查看:143
本文介绍了散列(带链)学生数据库(位折叠/加法散列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据学生的ID对学生数据库进行哈希处理(他们的所有信息都是通过文本文件读取的).我必须使用的哈希函数是ID中每个字符的ASCII码之和的模,然后除以代表哈希存储桶"数量的奇数.

I'm trying to hash a students database based on their ID (all their information is read through a text file). The hash-function I must use is the modulo of the sum of the ASCII code of each character in the ID, divided by an odd number representing the number of the hash 'buckets'.

例如,如果学生ID为AE797989且m = 11:

For example, if the students ID is AE797989 and m=11:

哈希(AE797989)= [ASCII('A')+ ASCII('E')+ ASCII('7')+ ASCII('9')+ ASCII('7')+ ASCII('9') + ASCII('8')+ ASCII('9')] mod m.

Hash(AE797989)= [ASCII(‘A’)+ ASCII(‘E’)+ ASCII(‘7’)+ ASCII(‘9’)+ ASCII(‘7’)+ ASCII(‘9’)+ ASCII(‘8’)+ ASCII(‘9’)] mod m.

我的文本文件由19名学生组成,看起来像这样:

My text file consists of 19 students and looks something like this:

AE797989史密斯·约翰19.00 UE354567沃尔什·奥黛丽(Walsh Audrey)12.00 ... (其中包含其ID号,姓氏,名字和等级)

AE797989 Smith John 19.00 UE354567 Walsh Audrey 12.00 ... (it contains their ID number, surname, first name and grade)

在我的主机中,我有一个选项,用户可以选择用我的文本文件中的学生填充数据库.在那种情况下,我调用我的函数(位于我的主函数之外):void insertToHash,在其中打开我的文件,并尝试通过调用我专门为哈希创建的函数为每个学生创建一个存储桶;称为int *哈希.

In my main, I have an option where the user can choose to fill the database with the students in my text file. In that case, I call my function(located outside of my main): void insertToHash , where I open my file, and try to create a bucket for each student by calling a function I exclusively made for the hashing; called int* Hash.

我找到了我提到的用Java编写的哈希函数的代码,但是由于字符串在C语言中的构想不同,因此在编译时会收到警告. 我找到的代码:

I found a code for the hash-function I mentioned written in Java but since Strings are conceived differently in C, I get warnings when I compile. The code I found:

    int h(String x, int M) {
   char ch[];
   ch = x.toCharArray();
   int xlength = x.length();

   int i, sum;
   for (sum=0, i=0; i < x.length(); i++)
     sum += ch[i];
   return sum % M;
 }

这是我提到的功能:

struct hash *hashTable = NULL;

struct node 
{
float grade;
char AM[100];
char first_name[100];
char last_name[100];
struct node *next;
}node;

struct hash 
{
struct node *head;
int count;
};

struct node * createNode(char *AM, char *first_name, char *last_name, float grade) 
{
struct node *newnode;

newnode = (struct node *) malloc(sizeof(struct node));
strcpy(newnode->AM, AM);
newnode->grade = grade;
strcpy(newnode->first_name, first_name);
strcpy(newnode->last_name, last_name);
newnode->next = NULL;
return newnode;
}

int* Hash(char **AM, int n)
{    
int i;
char* hashNum=0;

  for (i=0; i< 8; i++)
  {
  char *am = AM[i];
  hashNum += am;
  }

  int hashIndex = atoi(hashNum);
  return hashIndex % n;
}

void insertToHash(char *AM, char *first_name, char *last_name, float grade) 
{
FILE *fp;
fp = fopen ("Foitites-Vathmologio-DS.txt","rb");
if (fp == NULL) 
                { 
                fprintf(stderr,"Could not open file");  
                return;
                } 

while(fscanf(fp,"%s %s %s %d",node.AM, node.first_name, node.last_name, &node.grade) != EOF)
{   
int hashIndex = Hash(*AM, 19);

struct node *newnode = createNode(AM, first_name, last_name, grade);
/* head of list for the bucket with index "hashIndex" */

if (!hashTable[hashIndex].head) 
{
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count = 1;
return;
}

/* adding new node to the list */
newnode->next = (hashTable[hashIndex].head);
/*
* update the head of the list and no of
* nodes in the current bucket
*/
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count++;}
fclose(fp);
return;
}

对于我的函数Hash(char ** AM,int n),我得到警告:传递'Hash'的参数1使指针从整数开始而没有强制转换(我实际上得到了警告,并且我已经尝试解决它,但让情况变得更糟)

For my function Hash(char **AM, int n) I get the warning: passing argument 1 of 'Hash' makes pointer from integer without a cast (I actually get that warning a lot and I've tried to fix it but keep making it worse)

对于我的一行:hashNum + = am;我收到错误消息:无效的二进制操作数(+具有"char *"和"char *")

For my line: hashNum += am; I get the error: invalid operands to binary (+ have 'char *' and 'char *')

我迷路了,非常感谢您的帮助,谢谢!

I'm lost and would really appreciate the help, thanks!

推荐答案

好,您的类型错误.

这里

int* Hash(char **AM, int n) 

您告诉我们该函数将返回指向整数的指针.但是,该函数返回一个整数.

you tell that the function is to return a pointer to an integer. However, the function returns an integer.

此外,您还说该函数需要一个指向char的指针的指针(和一个int).但是,当您将其命名为

Further, you tell that the function expects a pointer to a pointer to a char (and an int). However when you call it like

int hashIndex = Hash(*AM, 19); 

您传递了一个char(和一个int).

you pass a char (and an int).

我希望您的功能更像:

int Hash(char *AM, int n)
{    
  int i;
  int hashIndex = 0;

  for (i=0; i< 8; i++)
  {
    hashIndex += AM[i];
  }

  return hashIndex % n;
}

并被称为:

int hashIndex = Hash(AM, 19); 

这篇关于散列(带链)学生数据库(位折叠/加法散列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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