Hash链表

本文介绍了一种使用PHP实现的简单哈希表数据结构。通过自定义哈希函数和链地址法解决冲突,实现了基本的插入和查找操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php

/*
  +------------------------------------------------------------------------------
  | datetime 2013-10-29  12:46:44
  +------------------------------------------------------------------------------
  | author baijm
  +------------------------------------------------------------------------------
 */

/**
 * hash节点的数据结构,用类模拟一个数据链表
 */
class HashNode {

    public $key;
    public $value;
    public $nextNode;

    public function __construct($key, $value, $nextNode = NULL) {
        $this->key = $key;
        $this->value = $value;
        $this->nextNode = $nextNode;
    }

}

class HashTable {

    private $buckets;    //用于存储数据的数组
    private $size = 10;  //数组的大小

    public function __construct() {
        //$this->buckets=new SplFixedArray($this->size);
        $this->buckets = array();
    }

    /**
     * 计算key的hash值,使用最简单的hash算法
     * @param type $key
     * @return type
     */
    private function hashFunc($key) {
        for ($i = 0, $len = strlen($key); $i < $len; $i++) {
            $hashVal+=ord($key{$i});
        }
        return $hashVal % $this->size;
    }

    public function insert($key, $value) {
        $index = $this->hashFunc($key);
        if (isset($this->buckets[$index])) {
            $nextNode = new HashNode($key, $value, $this->buckets[$index]);
        } else {
            $nextNode = new HashNode($key, $value);
        }
        $this->buckets[$index] = $nextNode;
    }

    public function find($key) {
        $index = $this->hashFunc($key);
        $current = $this->buckets[$index];
        while(isset($current)){
            if($current->key==$key){
                break;
            }else{
                $current=$current->nextNode;
            }
        }
        print_r($this->buckets);
        return $current->value;
    }

}

$hash = new HashTable();
$hash->insert('key1', 'value1');
$hash->insert('key12', 'value12');
$hash->find('key1');
$hash->find('key12');
?>

 

转载于:https://www.cnblogs.com/bai-jimmy/p/3394123.html

<think>我们使用C语言实现一个哈希链表(也称为链式哈希表)。哈希链表结合了哈希表和链表的优点:通过哈希函数快速定位到桶,每个桶是一个链表,用于处理哈希冲突。我们将实现以下功能:1.初始化哈希链表2.插入键值对(如果键已存在则更新)3.根据键查找值4.根据键删除元素5.销毁哈希链表为了简单起见,我们假设键为整数,值为字符串(字符数组)。但设计上可以扩展。步骤:1.定义链表节点结构(包含键、值、指向下一个节点的指针)2.定义哈希表结构(包含桶数组、桶的数量、当前元素个数)3.实现哈希函数(简单的取模)4.实现插入、查找、删除操作注意:由于C语言没有内置的字符串类型,我们在处理值的时候需要小心内存分配。代码实现如下:</think>### C语言实现哈希链表 哈希链表Hash Linked List)结合了哈希表的快速查找和链表的顺序访问特性。以下是完整的C语言实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义链表节点结构 typedef struct Node { char *key; // 键(字符串) int value; // 值(整数) struct Node *next; // 指向下一个节点的指针 } Node; // 定义哈希链表结构 typedef struct { Node **buckets; // 桶数组(存储链表头指针) int size; // 桶的数量 int count; // 当前存储的元素数量 } HashLinkedList; // 哈希函数:计算键的哈希值 unsigned int hash(const char *key, int size) { unsigned int hash_value = 0; for (int i = 0; key[i] != '\0'; i++) { hash_value = 31 * hash_value + key[i]; // 使用31作为乘数 } return hash_value % size; } // 创建新节点 Node* create_node(const char *key, int value) { Node *node = (Node*)malloc(sizeof(Node)); if (node == NULL) { perror("Memory allocation failed"); exit(EXIT_FAILURE); } // 分配内存并复制键 node->key = strdup(key); if (node->key == NULL) { perror("Memory allocation failed"); free(node); exit(EXIT_FAILURE); } node->value = value; node->next = NULL; return node; } // 初始化哈希链表 HashLinkedList* create_hash_linked_list(int size) { HashLinkedList *hll = (HashLinkedList*)malloc(sizeof(HashLinkedList)); if (hll == NULL) { perror("Memory allocation failed"); exit(EXIT_FAILURE); } hll->size = size; hll->count = 0; hll->buckets = (Node**)calloc(size, sizeof(Node*)); if (hll->buckets == NULL) { perror("Memory allocation failed"); free(hll); exit(EXIT_FAILURE); } return hll; } // 插入或更新键值对 void put(HashLinkedList *hll, const char *key, int value) { // 计算桶索引 unsigned int index = hash(key, hll->size); // 检查键是否已存在 Node *current = hll->buckets[index]; while (current != NULL) { if (strcmp(current->key, key) == 0) { // 键已存在,更新值 current->value = value; return; } current = current->next; } // 创建新节点并添加到链表头部 Node *new_node = create_node(key, value); new_node->next = hll->buckets[index]; hll->buckets[index] = new_node; hll->count++; } // 获取键对应的值 int get(HashLinkedList *hll, const char *key) { unsigned int index = hash(key, hll->size); Node *current = hll->buckets[index]; while (current != NULL) { if (strcmp(current->key, key) == 0) { return current->value; } current = current->next; } // 键不存在,返回特殊值表示未找到 return -1; // 实际应用中可能需要更好的错误处理 } // 删除指定键的节点 int delete(HashLinkedList *hll, const char *key) { unsigned int index = hash(key, hll->size); Node *current = hll->buckets[index]; Node *prev = NULL; while (current != NULL) { if (strcmp(current->key, key) == 0) { if (prev == NULL) { // 删除链表头节点 hll->buckets[index] = current->next; } else { prev->next = current->next; } // 释放内存 free(current->key); free(current); hll->count--; return 1; // 删除成功 } prev = current; current = current->next; } return 0; // 键不存在 } // 销毁哈希链表并释放所有内存 void destroy_hash_linked_list(HashLinkedList *hll) { for (int i = 0; i < hll->size; i++) { Node *current = hll->buckets[i]; while (current != NULL) { Node *temp = current; current = current->next; free(temp->key); free(temp); } } free(hll->buckets); free(hll); } // 打印哈希链表内容(调试用) void print_hash_linked_list(HashLinkedList *hll) { printf("Hash Linked List (Size: %d, Count: %d):\n", hll->size, hll->count); for (int i = 0; i < hll->size; i++) { printf("Bucket %d: ", i); Node *current = hll->buckets[i]; while (current != NULL) { printf("[%s:%d] -> ", current->key, current->value); current = current->next; } printf("NULL\n"); } } // 测试示例 int main() { // 创建哈希链表(桶大小为5) HashLinkedList *hll = create_hash_linked_list(5); // 插入键值对 put(hll, "apple", 10); put(hll, "banana", 20); put(hll, "orange", 30); put(hll, "grape", 40); put(hll, "lemon", 50); // 打印初始状态 print_hash_linked_list(hll); printf("\n"); // 测试查找 printf("Value of 'banana': %d\n", get(hll, "banana")); printf("Value of 'pear': %d\n", get(hll, "pear")); // 应返回-1 // 测试更新 put(hll, "banana", 25); printf("Updated value of 'banana': %d\n", get(hll, "banana")); // 测试删除 printf("Delete 'orange': %s\n", delete(hll, "orange") ? "Success" : "Failed"); printf("Delete 'pear': %s\n", delete(hll, "pear") ? "Success" : "Failed"); // 打印最终状态 printf("\nAfter operations:\n"); print_hash_linked_list(hll); // 清理内存 destroy_hash_linked_list(hll); return 0; } ``` ### 关键实现解析 1. **数据结构**: - `Node`: 链表节点,包含键、值和指向下一个节点的指针 - `HashLinkedList`: 哈希链表结构,包含桶数组、桶数量和元素计数 2. **核心函数**: - `hash()`: 使用简单乘法哈希函数计算键的哈希值 - `create_node()`: 创建新节点并复制键字符串 - `put()`: 插入或更新键值对(处理冲突使用链地址法) - `get()`: 查找键对应的值 - `delete()`: 删除指定键的节点 - `destroy_hash_linked_list()`: 释放所有分配的内存 3. **内存管理**: - 使用`strdup()`复制键字符串 - 所有动态分配的内存都有对应的释放操作 - 使用`calloc()`初始化桶数组为NULL指针 4. **冲突处理**: - 使用链地址法(Separate Chaining)处理哈希冲突 - 每个桶是一个链表,冲突元素添加到链表头部 ### 性能特点 | 操作 | 平均时间复杂度 | 最坏时间复杂度 | |------------|----------------|----------------| | 插入(put) | O(1) | O(n) | | 查找(get) | O(1) | O(n) | | 删除(delete)| O(1) | O(n) | | 空间复杂度 | O(n + m) | (n元素数,m桶数)| ### 优化建议 1. **动态扩容**: ```c // 当负载因子(元素数/桶数)超过阈值(如0.7)时扩容 if ((float)hll->count / hll->size > 0.7) { resize(hll, hll->size * 2); } ``` 2. **改进哈希函数**: ```c // 使用更复杂的哈希函数如MurmurHash或FNV-1 unsigned int fnv1_hash(const char *key, int size) { unsigned int hash = 2166136261u; for (; *key; key++) { hash ^= *key; hash *= 16777619; } return hash % size; } ``` 3. **链表优化**: - 将链表改为双向链表以便快速删除 - 实现链表尾部指针减少插入时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值