!283
2022-05-31 18:02
采纳率: 60%
浏览 16

想对字符串哈希,但一直错误

想对字符串进行哈希,但一直错误,希望能帮忙解决,有完整代码在下面
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max 100
#define Maxsize 10
typedef char  datatype;
typedef struct Node {
    datatype data;
    struct Node* next;
}Node;
typedef struct head {
    struct Node arr[Maxsize];
}Hash;
int ZifuAscall(char ch[]) {//将字符转为ASCALL码的和
    int length = strlen(ch);
    int sum = 0, n = 0;
    for (int i = 0; i < length; i++) {
        n = (int)ch[i];
        sum = sum + n;
    }
    return sum;
}
void Hashinitiate(Hash* head) {//表头初始化
    for (int i = 0; i < Maxsize; i++) {
        head->arr[i].next = NULL;//将表头的NEXT域设为空
    }
}
bool HashInsert(Hash* head, char srt[]) {//将数据进行插入
    Node* p = (struct Node*)malloc(sizeof(Node));//申请节点
    p->data= srt[Max];//将字符存储到链表中
    int s = ZifuAscall(srt);//将字符串的和赋值给s;
    int index= s % 10;//取模
    p->next = head->arr[index].next;//使用头插法进行插入
    head->arr[index].next = p;
    return true;
}
void HashSearch(Hash* head, char srt[]) {//进行查找
    int index;
    int s = ZifuAscall(srt);
    index = s % Maxsize;
    Node* p = head->arr[index].next;
    for (p; p != NULL; p = p->next) {
        if (strcmp(p->data,srt[Max])==0) {
            printf("查找成功,数据为:%s\n", p->data);
        }
        else printf("查找失败\n");
    }
}
int main() {
    char ax[10][Max] = { "zhang","qweqwe","adqwqe","qweqeq","qweqw","zhag","qwqwe","adqwe","qwqeq","qweq" };
    Hash head;
    Hashinitiate(&head);
    for (int i = 0; i < Maxsize; i++) {
        HashInsert(&head, ax[i]);
    }
    printf("查找ax[10][Max] = {zhang,qweqwe,adqwqe,qweqeq,qweqw,zhag,qwqwe,adqwe,qwqeq,qweq }\n");
    for (int i = 0; i < Maxsize; i++) {
        HashSearch(&head, ax[i]);
    }    
}

想得出主函数里面的结果

1条回答 默认 最新

相关推荐 更多相似问题