hash链表

hash链表概述

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

hash链表实现

本文的hash链表实现:hash头部用单链表、其他的hash节点用双向链表。实现主要取自Linux内核实现,本文做了移植。本文代码可从http://download.csdn.net/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函数里,调用主测试程序即可。

以下是一个简单的C语言实现哈希链表的构建的示例代码,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #define HASH_SIZE 10 typedef struct node { int key; int value; struct node* next; } Node; typedef struct hash_table { Node* table[HASH_SIZE]; } HashTable; int hash(int key) { return key % HASH_SIZE; } void put(HashTable* ht, int key, int value) { int index = hash(key); Node* node = ht->table[index]; while (node) { if (node->key == key) { node->value = value; return; } node = node->next; } node = (Node*)malloc(sizeof(Node)); node->key = key; node->value = value; node->next = ht->table[index]; ht->table[index] = node; } int get(HashTable* ht, int key) { int index = hash(key); Node* node = ht->table[index]; while (node) { if (node->key == key) { return node->value; } node = node->next; } return -1; } void print_hash_table(HashTable* ht) { for (int i = 0; i < HASH_SIZE; i++) { printf("index %d: ", i); Node* node = ht->table[i]; while (node) { printf("(%d, %d) ", node->key, node->value); node = node->next; } printf("\n"); } } int main() { HashTable ht; for (int i = 0; i < HASH_SIZE; i++) { ht.table[i] = NULL; } put(&ht, 1, 10); put(&ht, 11, 20); put(&ht, 21, 30); put(&ht, 2, 40); put(&ht, 12, 50); put(&ht, 22, 60); printf("get(1) = %d\n", get(&ht, 1)); printf("get(11) = %d\n", get(&ht, 11)); printf("get(21) = %d\n", get(&ht, 21)); printf("get(2) = %d\n", get(&ht, 2)); printf("get(12) = %d\n", get(&ht, 12)); printf("get(22) = %d\n", get(&ht, 22)); print_hash_table(&ht); return 0; } ``` 在这个示例中,我们定义了一个哈希表结构体`HashTable`和一个链表结构体`Node`。`HashTable`中包含一个长度为`HASH_SIZE`的指向`Node`结构体的指针数组,每个指针指向哈希表中一个链表的头节点。`Node`结构体包含一个键值对和一个指向下一个节点的指针。 我们通过hash函数计算键的哈希值,并将键值对插入到哈希表中。当需要查询一个键值对时,我们通过哈希值找到对应的链表,并在链表中查找对应的节点。 该示例中的哈希表使用的是链表法解决哈希冲突的方式。当哈希值相同的键值对需要插入到同一个链表中时,我们将新的键值对插入到链表头部,这样就可以在O(1)时间内完成插入操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值