哈希表设计

#include<iostream>
#include<string>
using namespace std;
#define HASH_LENGTH 50               //哈希表的长度        
#define M 47                         //随机数
#define NAME_NO 30                   //人名的个数       
typedef struct     
{   char *py;    //名字的拼音
    int k;       //拼音所对应的整数
}NAME;
NAME NameList[HASH_LENGTH];    //全局变量NAME      
typedef struct    //哈希表
{   char *py;   //名字的拼音
    int k;      //拼音所对应的整数
    int si;     //查找长度
}HASH;
HASH HashList[HASH_LENGTH];        //全局变量HASH                    
void InitNameList() //姓名(结构体数组)初始化         
{   char *f;
    int r,s0,i;
    NameList[0].py="baojie";
    NameList[1].py="chengaoyang";
    NameList[2].py="chenguangzhong";
    NameList[3].py="chenliangliang";
    NameList[4].py="chenyongzhou";
    NameList[5].py="fengchao";
    NameList[6].py="gexiangfeng";
    NameList[7].py="huting";
    NameList[8].py="huangpinjin";
    NameList[9].py="jiangxiaojia";
    NameList[10].py="laidongjie";
    NameList[11].py="liyexiao";
    NameList[12].py="lidaohui";
    NameList[13].py="lijue";
    NameList[14].py="lizhuoqun";
    NameList[15].py="linfujun";
    NameList[16].py="luobin";
    NameList[17].py="luokeqing";
    NameList[18].py="nichao";
    NameList[19].py="panhuafeng";
    NameList[20].py="sijun";
    NameList[21].py="songzhanhui";
    NameList[22].py="sunzhengqing";
    NameList[23].py="wanghaofeng";
    NameList[24].py="wangjunshuai";
    NameList[25].py="wangqinde";
    NameList[26].py="wangzejun";
    NameList[27].py="wangkeke";
    NameList[28].py="weixing";
    NameList[29].py="wurenke";

    for(i=0;i<NAME_NO;i++)
{   s0=0;
        f=NameList[i].py;
        for(r=0;*(f+r)!='/0';r++)
    /* 方法:将字符串的各个字符所对应的ASCII码相加,所得的整数做为哈希表的关键字*/
            s0=*(f+r)+s0;
        NameList[i].k=s0;
}
}
void CreateHashList() //建立哈希表  
{ int i;
    for(i=0; i<HASH_LENGTH;i++)
{   HashList[i].py="";
        HashList[i].k=0;
        HashList[i].si=0;
}
    for(i=0;i<HASH_LENGTH;i++)
{   int sum=0;
        int adr=(NameList[i].k)%M;  
   //哈希函数
        int d=adr;
        if(HashList[adr].si==0)     //如果不冲突
   { HashList[adr].k=NameList[i].k;
           HashList[adr].py=NameList[i].py;

注:数据结构严蔚敏版实习报告

以下是一个简单的哈希表设计 C 语言代码实现,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 100 typedef struct { char* key; int value; } HashNode; typedef struct { HashNode** nodes; int size; } HashTable; HashTable* createHashTable(int size) { HashTable* hashtable = (HashTable*)malloc(sizeof(HashTable)); hashtable->nodes = (HashNode**)calloc(size, sizeof(HashNode*)); hashtable->size = size; return hashtable; } int hash(char* key, int size) { int hash = 0; int len = strlen(key); for (int i = 0; i < len; i++) { hash = (hash * 31 + key[i]) % size; } return hash; } void put(HashTable* hashtable, char* key, int value) { int hashValue = hash(key, hashtable->size); HashNode* node = hashtable->nodes[hashValue]; while (node != NULL) { if (strcmp(node->key, key) == 0) { node->value = value; return; } node = node->next; } node = (HashNode*)malloc(sizeof(HashNode)); node->key = key; node->value = value; node->next = hashtable->nodes[hashValue]; hashtable->nodes[hashValue] = node; } int get(HashTable* hashtable, char* key) { int hashValue = hash(key, hashtable->size); HashNode* node = hashtable->nodes[hashValue]; while (node != NULL) { if (strcmp(node->key, key) == 0) { return node->value; } node = node->next; } printf("Error: Key not found\n"); return -1; } void delete(HashTable* hashtable, char* key) { int hashValue = hash(key, hashtable->size); HashNode* node = hashtable->nodes[hashValue]; HashNode* prev = NULL; while (node != NULL) { if (strcmp(node->key, key) == 0) { if (prev == NULL) { hashtable->nodes[hashValue] = node->next; } else { prev->next = node->next; } free(node); return; } prev = node; node = node->next; } printf("Error: Key not found\n"); } void printHashTable(HashTable* hashtable) { for (int i = 0; i < hashtable->size; i++) { printf("%d: ", i); HashNode* node = hashtable->nodes[i]; while (node != NULL) { printf("(%s, %d) ", node->key, node->value); node = node->next; } printf("\n"); } } int main() { HashTable* hashtable = createHashTable(SIZE); put(hashtable, "apple", 3); put(hashtable, "banana", 2); put(hashtable, "orange", 1); printHashTable(hashtable); printf("Get value of apple: %d\n", get(hashtable, "apple")); delete(hashtable, "banana"); printHashTable(hashtable); return 0; } ``` 解释一下上述代码: - `HashTable` 结构体包含一个 `nodes` 数组,用来存储哈希表中的节点,以及 `size` 表示哈希表的大小。 - `HashNode` 结构体包含一个 `key` 字符串和一个 `value` 整数,表示键值对。 - `createHashTable` 函数创建一个哈希表,并返回其指针。 - `hash` 函数接收一个键和哈希表的大小,计算该键的哈希值,并返回对应哈希表中的置。 - `put` 函数接收键值对,将其插入哈希表中。如果该键已经存在,则更新对应的值。 - `get` 函数接收一个键,返回对应的值。如果该键不存在,则输出错误信息,并返回 -1。 - `delete` 函数接收一个键,将其从哈希表中删除。如果该键不存在,则输出错误信息。 - `printHashTable` 函数打印哈希表的内容,用于调试。 - `main` 函数创建一个哈希表,插入若干键值对,然后调用各种函数测试其功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值