hash链表

hash链表概述

hash链表是hash表和链表的结合,使用比较方便。

hash链表实现

本文的hash链表实现:hash头部用单链表、其他的hash节点用双向链表。实现主要取自Linux内核实现,本文做了移植。本文代码可从http://download.youkuaiyun.com/detail/it_pcode/6632905下载。

hash实现

#ifndef HLIST_H_
#define HLIST_H_

#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

/*通过父结构体type中的成员member的已知地址ptr,来寻找当前ptr地址所属的父结构体type的地址*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

/*内核预加载内容到RAM,在此不做实现*/
#define prefetch(x) (x)

/*
 * Double linked lists with a single pointer list head.
 * Mostly useful for hash tables where the two pointer list head is
 * too wasteful.
 * You lose the ability to access the tail in O(1).
 */

struct hlist_head {
	struct hlist_node *first;
};

struct hlist_node {
	struct hlist_node *next, **pprev;
};

#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h) {
	h->next = NULL;
	h->pprev = NULL;
}

static inline int hlist_unhashed(const struct hlist_node *h) {
	return !h->pprev;
}

static inline int hlist_empty(const struct hlist_head *h) {
	return !h->first;
}

static inline void __hlist_del(struct hlist_node *n) {
	struct hlist_node *next = n->next;
	struct hlist_node **pprev = n->pprev;
	*pprev = next;
	if (next)
		next->pprev = pprev;
}

static inline void hlist_del(struct hlist_node *n) {
	__hlist_del(n);
	n->next = NULL;
	n->pprev = NULL;
}

static inline void hlist_del_init(struct hlist_node *n) {
	if (!hlist_unhashed(n)) {
		__hlist_del(n);
		INIT_HLIST_NODE(n);
	}
}

static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) {
	struct hlist_node *first = h->first;
	n->next = first;
	if (first)
		first->pprev = &n->next;
	h->first = n;
	n->pprev = &h->first;
}

/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
		struct hlist_node *next) {
	n->pprev = next->pprev;
	n->next = next;
	next->pprev = &n->next;
	*(n->pprev) = n;
}

static inline void hlist_add_after(struct hlist_node *n,
		struct hlist_node *next) {
	next->next = n->next;
	n->next = next;
	next->pprev = &n->next;

	if (next->next)
		next->next->pprev = &next->next;
}

/*
 * Move a list from one list head to another. Fixup the pprev
 * reference of the first entry if it exists.
 */
static inline void hlist_move_list(struct hlist_head *old,
		struct hlist_head *new) {
	new->first = old->first;
	if (new->first)
		new->first->pprev = &new->first;
	old->first = NULL;
}

#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define hlist_for_each(pos, head) \
	for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
	     pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
	     pos = n)

/**
 * hlist_for_each_entry	- iterate over list of given type
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry(tpos, pos, head, member)			 \
	for (pos = (head)->first;					 \
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_continue(tpos, pos, member)		 \
	for (pos = (pos)->next;						 \
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_from(tpos, pos, member)			 \
	for (; pos && ({ prefetch(pos->next); 1;}) &&			 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @n:		another &struct hlist_node to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
	for (pos = (head)->first;					 \
	     pos && ({ n = pos->next; 1; }) && 				 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = n)

#endif /* HLIST_H_ */

hash链表实例

首先实现了自己的hash list初始化、键存在与否判定以及key值查找等函数。然后是主测试程序。

#define MAX_LEN 20

struct hash_node {
	struct hlist_node hnode;
	int age;
};

struct hlist_head hashead[MAX_LEN];

void init_hlist() {
	int i = 0;
	for (i = 0; i < MAX_LEN; i++) {
		INIT_HLIST_HEAD(&hashead[i]);
	}
}

int key_exists(struct hlist_head *head, int key) {
	struct hash_node * node;
	struct hlist_node *hlistnode;
	hlist_for_each_entry(node, hlistnode, head,hnode) {
		if (node->age == key) {
			return 1;
		}
	}
	return 0;
}

struct hash_node * hlist_search(int age) {
	struct hash_node *node, *data;
	int i = 0;
	struct hlist_node *hlistnode;
	for (i = 0; i < MAX_LEN; i++) {
		hlist_for_each_entry(node, hlistnode, &hashead[i],hnode) {
			data = container_of(&node->hnode, struct hash_node, hnode);
			if (data->age == age) {
				return data;
			}
		}
	}
	return NULL;
}

void testhlist() {

	init_hlist();
	int i = 0;
	struct hash_node * node;
	struct hlist_node *hlistnode;

	srand(time(NULL));
	for (i = 0; i < 4 * MAX_LEN; i++) {
		node = malloc(sizeof(struct hash_node));
		INIT_HLIST_NODE(&node->hnode);
		node->age = rand() % (4 * MAX_LEN);
		if (key_exists(&hashead[node->age % MAX_LEN], node->age) == 0) {
			hlist_add_head(&node->hnode, &hashead[node->age % MAX_LEN]);
		}
	}
	for (i = 0; i < MAX_LEN; i++) {
		printf("head %d has member :", i);
		hlist_for_each_entry(node, hlistnode, &hashead[i],hnode) {
			printf("%d  ", node->age);
		}
		printf("\n");
	}

	for (i = 0; i < MAX_LEN; i++) {
		node = hlist_search(i);
		if (NULL != node) {
			printf("found %d\n", i);
			hlist_del(&node->hnode);
		}
	}
	printf("after clear \n");
	for (i = 0; i < MAX_LEN; i++) {
		printf("head %d has member :", i);
		hlist_for_each_entry(node, hlistnode, &hashead[i],hnode) {
			printf("%d  ", node->age);
		}
		printf("\n");
	}

}

在main函数里,调用主测试程序即可。

<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. **链表优化**: - 将链表改为双向链表以便快速删除 - 实现链表尾部指针减少插入时间
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值