用c实现HASH表创建、插入、查找、删除、打印

转自:http://blog.csdn.net/zww0815/article/details/6892560

/************************************************************************
用c实现HASH表创建、插入、查找、删除、打印,实现并不是最完美的,欢迎指正补充!后续还有更多类似的实现放上来,
欢迎关注!!!
对本贴有更好的方法或建议可以给本人留言或发邮件:
Email:zww0815@qq.com
Thanks!
************************************************************************/
#include <stdio.h>
#include <STDLIB.H>
#include <MEMORY.H>

#define STATUS int
#define FALSE 0
#define TRUE 1
#define VOID void

/****************************************
a)定义hash表和基本数据节点  
*****************************************/
typedef struct _Node
{
	int data;
	struct _Node *next;
}NODE;

typedef struct _HASH_TABLE
{
	NODE * value[10];
}HASH_TABLE;


/****************************************
b)创建hash表
*****************************************/
HASH_TABLE * create_hash_table()
{
	HASH_TABLE* pHashTbl = (HASH_TABLE*)malloc(sizeof(HASH_TABLE));
	memset(pHashTbl,0,sizeof(HASH_TABLE));
	return pHashTbl;
}


/****************************************
c)在hash表当中寻找数据
*****************************************/
NODE * find_data_in_hash(HASH_TABLE* pHashTbl,int data)
{
	NODE* pNode;
	if (NULL == pHashTbl)
	{
		return NULL;
	}

	/*获得HASH表索引,为NULL则直接返回NULL*/
	if (NULL == (pNode = pHashTbl->value[data%10]))
	{
		return NULL;
	}

	/*在该索引下的单链表中查找节点*/
	while(pNode)
	{
		if ( data == pNode->data)
		{
			/*找到节点就返回当前节点*/
			return pNode;
		}
		/*当前节点不是,指向下一节点*/
		pNode = pNode->next;
	}

	/*没找到返回NULL,不过在这返回没有意义*/
	//return NULL;
}

/****************************************
d)在hash表当中插入数据
*****************************************/
STATUS insert_data_into_hash(HASH_TABLE* pHashTbl,int data)
{
	NODE* pNode;
	if (NULL == pHashTbl)
	{
		return FALSE;
	}

	if (NULL == pHashTbl->value[data%10])
	{
		pNode = (NODE*)malloc(sizeof(NODE));
		memset(pNode,0,sizeof(NODE));
		pNode->data = data;
		pHashTbl->value[data%10] = pNode;

		return TRUE;
	}

	if (NULL == find_data_in_hash(pHashTbl,data))
	{
		return FALSE;
	}

	pNode = pHashTbl->value[data%10];
	while(pNode->next)
	{
		pNode = pNode->next;
	}

	pNode->next = (NODE*)malloc(sizeof(NODE));
	memset(pNode->next,0,sizeof(NODE));
	pNode->next->data = data;

	return TRUE;
}

/****************************************
 e)从hash表中删除数据
*****************************************/
STATUS delete_data_from_hash(HASH_TABLE* pHashTbl,int data)
{
	NODE* pHead;
	NODE* pNode;

	if (NULL == pHashTbl || NULL == pHashTbl->value[data%10])
	{
		return FALSE;
	}

	if (NULL == (pNode = find_data_in_hash(pHashTbl,data)))
	{
		return FALSE;
	}

	if (pNode == pHashTbl->value[data%10])
	{
		pHashTbl->value[data%10] = pNode->next;
		goto final;
	}

	pHead = pHashTbl->value[data%10];
	
	while(pNode != pHead->next)
	{
		pHead = pHead->next;
	}

	pHead->next = pNode->next;

final:
	free(pNode);
	return TRUE;
}


/****************************************
 f)打印hash表中所有数据
 如:
 [Hash idx]     [value]
 0-------------NULL
 1-------------1 251
 2-------------22
 3-------------123 43
 4-------------NULL
 5-------------55 15 235 525 725 275 545
 6-------------NULL
 7-------------257
 8-------------NULL
*****************************************/
VOID print_hash_data(HASH_TABLE* pHashTbl)
{
	NODE* pNode;
	int i=0;
	if (NULL == pHashTbl)
	{
		printf("ERROR:The hash is NULL\n");
	}

/*
	if (NULL == (pNode = pHashTbl->value[10]))
	{
		printf("ERROR:The hash node is NULL\n");
	}
*/
	printf("[Hash idx]     [value]\n");
	do 
	{   
		printf("   %d-------------",i);
		if (NULL == pHashTbl->value[i])
		{
			i++;
			printf("NULL\n");
			continue;
		}

		pNode = pHashTbl->value[i];
		
		while(pNode)
		{
			printf("%d ",pNode->data);
			pNode = pNode->next;
		}
		printf("\n");
		i++;
	} while (i<10);

	printf("\n");
}

int main()
{
	HASH_TABLE* pHashTbl = create_hash_table();
	
	(VOID)insert_data_into_hash(pHashTbl,22);
	(VOID)insert_data_into_hash(pHashTbl,22);
	(VOID)insert_data_into_hash(pHashTbl,123);
	(VOID)insert_data_into_hash(pHashTbl,436);
	(VOID)insert_data_into_hash(pHashTbl,55);
	(VOID)insert_data_into_hash(pHashTbl,157);
	(VOID)insert_data_into_hash(pHashTbl,235);
	(VOID)insert_data_into_hash(pHashTbl,256);
	(VOID)insert_data_into_hash(pHashTbl,525);
	(VOID)insert_data_into_hash(pHashTbl,724);
	(VOID)insert_data_into_hash(pHashTbl,278);
	(VOID)insert_data_into_hash(pHashTbl,209);
	(VOID)insert_data_into_hash(pHashTbl,67);
	(VOID)insert_data_into_hash(pHashTbl,54);
	(VOID)insert_data_into_hash(pHashTbl,546);
	(VOID)insert_data_into_hash(pHashTbl,350);
	(VOID)insert_data_into_hash(pHashTbl,101);
	(VOID)insert_data_into_hash(pHashTbl,23);

	print_hash_data(pHashTbl);

	(VOID)delete_data_from_hash(pHashTbl,55);

	print_hash_data(pHashTbl);


	return 0;
}


类似的一篇博文: http://blog.csdn.net/feixiaoxing/article/details/6885657

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值