【数据结构】哈希表的创建、查找(C语言实现)

程序代码:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

#include<malloc.h>

#define ElemType int

#define HASHSIZE 11

#define NUM 9

#define NULLKEY -54321

typedef struct {

    ElemType elem[HASHSIZE];

    int count;

}HashTable;

void InitHashTble(HashTable* H) {

    H->count = HASHSIZE;

    for (int i = 0; i < HASHSIZE; i++) {

         H->elem[i] = NULLKEY;

    }

}

int HashFunction(ElemType key, int p) {

    return(key % p);

}

void InsertHashTable(HashTable* H, ElemType key)

{

    int address;

    address = HashFunction(key, HASHSIZE);

    while (H->elem[address] != NULLKEY)

    {

         address = HashFunction((address + 1), HASHSIZE);

    }

    H->elem[address] = key;

}

int SearchHashTable(HashTable* H, int key) {

    int address;

    address = HashFunction(key, HASHSIZE);

    if (H->elem[address] == NULLKEY) { return -1; }

    else {

         while (H->elem[address] != key) {

             address = HashFunction(address + 1, HASHSIZE);

             if (H->elem[address] == NULLKEY) { return -1; }

         }

         return address;

    }

}

int main() {

    HashTable* H=(HashTable*)malloc(sizeof(HashTable));

    InitHashTble(H);

    int key;

    printf("请依次输入需要存储的%d个整数\n", NUM);

    for (int i = 0; i < NUM; i++) {

         printf("第%d个整数为:\n", i + 1);

         scanf("%d", &key);

         InsertHashTable(H, key);

    }

    printf("\n请输入你要查找的整数:\n");

    scanf("%d", &key);

    int t = SearchHashTable(H, key);

    if (t == -1) { printf("查找失败!不存在该整数\n"); }

    else { printf("该整数存储在第%d个位置\n", t); }

    return 0;

}

运行截图:

 

实验小结

本次的实验要求弄清楚最关键的两个模块,即插入和查找,首先要有哈希函数生成映射地址、有哈希表保存元素,然后要有自己设定的解决冲突的办法,这个程序是采用向下挪动一个办法,直到找到为空的地方保存。在查找中也是,先要通过哈希函数生成映射地址,通过这个地址参看哈希表中时候有元素,考虑到会有冲突的产生,那么必须要通过循环查找,要么找到元素,要么直到为空跳出查找。这也是这个程序的难点所在。总体来说,哈希表对于提高储存和查找效率方面有很大的提升。实验难度不是很大。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
哈希表是一种常见的数据结构,用于快速查找和存储键值对。在C语言中,可以使用数组和指针来实现哈希表。 下面是一个简单的示例代码,展示了如何使用哈希表实现插入、查找和删除操作: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 10 // 哈希表节点结构 typedef struct { char* key; int value; } Node; // 哈希表结构 typedef struct { Node** nodes; } HashTable; // 创建哈希表 HashTable* createHashTable() { HashTable* hashTable = (HashTable*)malloc(sizeof(HashTable)); hashTable->nodes = (Node**)malloc(sizeof(Node*) * TABLE_SIZE); for (int i = 0; i < TABLE_SIZE; i++) { hashTable->nodes[i] = NULL; } return hashTable; } // 哈希函数 int hash(char* key) { int sum = 0; int length = strlen(key); for (int i = 0; i < length; i++) { sum += key[i]; } return sum % TABLE_SIZE; } // 插入键值对到哈希表 void insert(HashTable* hashTable, char* key, int value) { int index = hash(key); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->key = key; newNode->value = value; hashTable->nodes[index] = newNode; } // 查找键对应的值 int find(HashTable* hashTable, char* key) { int index = hash(key); Node* node = hashTable->nodes[index]; if (node != NULL && strcmp(node->key, key) == 0) { return node->value; } return -1; // 未找到 } // 从哈希表中删除键值对 void removeNode(HashTable* hashTable, char* key) { int index = hash(key); Node* node = hashTable->nodes[index]; if (node != NULL && strcmp(node->key, key) == 0) { free(node); hashTable->nodes[index] = NULL; } } // 销毁哈希表 void destroyHashTable(HashTable* hashTable) { for (int i = 0; i < TABLE_SIZE; i++) { Node* node = hashTable->nodes[i]; if (node != NULL) { free(node); } } free(hashTable->nodes); free(hashTable); } int main() { HashTable* hashTable = createHashTable(); insert(hashTable, "apple", 10); insert(hashTable, "banana", 5); printf("Value for 'apple' is %d\n", find(hashTable, "apple")); printf("Value for 'banana' is %d\n", find(hashTable, "banana")); removeNode(hashTable, "apple"); printf("Value for 'apple' after removal is %d\n", find(hashTable, "apple")); destroyHashTable(hashTable); return 0; } ``` 这段代码实现了一个简单的哈希表,其中的 `createHashTable()` 函数用于创建哈希表,`insert()` 函数用于插入键值对,`find()` 函数用于查找键对应的值,`removeNode()` 函数用于从哈希表中删除键值对,`destroyHashTable()` 函数用于销毁哈希表。 在这个示例中,哈希表使用了基本的除留余数法作为哈希函数,将字符串的 ASCII 码之和对表大小取余得到索引。每个索引位置存储一个指向节点的指针,节点结构包含键和值。 注意:这只是一个简单的示例,实际应用中可能需要解决冲突、扩容等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃头鸭鸭鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值