数据结构——杂凑表(拉链法)
2008-01-24 14:02
316 查看
Hash
设某元素的关键字为key,用函数Hash(key)的值作为该元素的地址,所构造的查找表称为哈希表(或散列表),函数Hash(key)称为哈希(Hash)函数(或散列函数)。
若两个关键字k1 != k2,但H(k1) = H(k2)。这种现象称为冲突,k1和k2称为同义词。
要避免冲突,需要构造一个恰当的哈希函数。常见的构造哈希函数的方法有:直接定址法、除留余数法、平方取中法等。
有时冲突是不能完全避免的,须妥善处理。
处理冲突的方法:链地址法(拉链法)、开放定址法
链地址法(拉链法)
将所有发生冲突的元素存储在一个链表中,并将这些链表的表头指针存放在一个表中。所构造的散列表称为开散列表。
插入操作的最坏情况运行时间为O(1),查找操作的最坏情况运行时间与表的长度成正比。
ChainedHashMain.c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "hash.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*简单操作示例*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char **argv)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashTable t;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int a
= ...{47, 7, 29, 11, 16, 92, 22, 8, 3, 0, 37, 89, 4, 21};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashCreate(&t, a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("[+] hash table create ok. ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = ChainedHashSearch(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintNodeKey(p);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashInsert(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashDelete(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = ChainedHashSearch(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintNodeKey(p);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
hash.h
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#ifndef _HASH_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define _HASH_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define M 11
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define N 14
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define DELETE -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef int KeyType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef struct Node...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KeyType key;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct Node *next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}CHashNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef CHashNode *CHashTable[M];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Hash(KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashInsert(CHashTable *t, KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashCreate(CHashTable *t, int *a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashDelete(CHashTable *t, KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CHashNode *ChainedHashSearch(CHashTable *t, KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void PrintNodeKey(CHashNode *ph);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#endif
hash.c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "hash.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Hash(KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return k % M;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*根据a[0..N-1]中结点建立散列表t[0..M-1]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashCreate(CHashTable *t, int *a)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 0; i < M; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(*t)[i] = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 0; i < N; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashInsert(&(*t), a[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*将关键字k插入散列表t中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashInsert(CHashTable *t, KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int addr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = ChainedHashSearch(&(*t), k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("Duplicate Key! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addr = Hash(k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (CHashNode *)malloc(sizeof(CHashNode));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->key = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->next = (*t)[addr];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(*t)[addr] = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*在散列表t中查找关键字k
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CHashNode *ChainedHashSearch(CHashTable *t, KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int addr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addr = Hash(k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (*t)[addr];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(p && p->key != k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = p->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*在散列表t中删除关键字k
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashDelete(CHashTable *t, KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p, *q;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int addr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addr = Hash(k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (*t)[addr];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if((p == NULL) || (p->key == DELETE))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("No such key! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(p && p->key != k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = p->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(q != p)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q->next = p->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->key = DELETE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void PrintNodeKey(CHashNode *p)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("It's a NULL node! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d ", p->key);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
设某元素的关键字为key,用函数Hash(key)的值作为该元素的地址,所构造的查找表称为哈希表(或散列表),函数Hash(key)称为哈希(Hash)函数(或散列函数)。
若两个关键字k1 != k2,但H(k1) = H(k2)。这种现象称为冲突,k1和k2称为同义词。
要避免冲突,需要构造一个恰当的哈希函数。常见的构造哈希函数的方法有:直接定址法、除留余数法、平方取中法等。
有时冲突是不能完全避免的,须妥善处理。
处理冲突的方法:链地址法(拉链法)、开放定址法
链地址法(拉链法)
将所有发生冲突的元素存储在一个链表中,并将这些链表的表头指针存放在一个表中。所构造的散列表称为开散列表。
插入操作的最坏情况运行时间为O(1),查找操作的最坏情况运行时间与表的长度成正比。
ChainedHashMain.c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "hash.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*简单操作示例*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char **argv)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashTable t;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int a
= ...{47, 7, 29, 11, 16, 92, 22, 8, 3, 0, 37, 89, 4, 21};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashCreate(&t, a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("[+] hash table create ok. ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = ChainedHashSearch(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintNodeKey(p);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashInsert(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashDelete(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = ChainedHashSearch(&t, 92);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintNodeKey(p);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
hash.h
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#ifndef _HASH_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define _HASH_H
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define M 11
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define N 14
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define DELETE -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef int KeyType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef struct Node...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KeyType key;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct Node *next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}CHashNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef CHashNode *CHashTable[M];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Hash(KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashInsert(CHashTable *t, KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashCreate(CHashTable *t, int *a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashDelete(CHashTable *t, KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CHashNode *ChainedHashSearch(CHashTable *t, KeyType k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void PrintNodeKey(CHashNode *ph);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#endif
hash.c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "hash.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Hash(KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return k % M;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*根据a[0..N-1]中结点建立散列表t[0..M-1]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashCreate(CHashTable *t, int *a)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 0; i < M; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(*t)[i] = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 0; i < N; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChainedHashInsert(&(*t), a[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*将关键字k插入散列表t中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashInsert(CHashTable *t, KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int addr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = ChainedHashSearch(&(*t), k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("Duplicate Key! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addr = Hash(k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (CHashNode *)malloc(sizeof(CHashNode));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->key = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->next = (*t)[addr];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(*t)[addr] = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*在散列表t中查找关键字k
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CHashNode *ChainedHashSearch(CHashTable *t, KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int addr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addr = Hash(k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (*t)[addr];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(p && p->key != k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = p->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*在散列表t中删除关键字k
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int ChainedHashDelete(CHashTable *t, KeyType k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CHashNode *p, *q;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int addr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addr = Hash(k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (*t)[addr];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if((p == NULL) || (p->key == DELETE))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("No such key! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(p && p->key != k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = p->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(q != p)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q->next = p->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->key = DELETE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void PrintNodeKey(CHashNode *p)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("It's a NULL node! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d ", p->key);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 数据结构——杂凑表(拉链法)
- 数据结构——杂凑表(开放定址法)
- 基于ARM的智能灯光控制系统(4)数据结构
- c语言实现通用数据结构(二):通用队列
- 数据结构实验之链表五:单链表的拆分
- Mini-Notes: 数据结构与算法-[第三部分]排序
- 数据结构常见的八大排序算法(详细整理)
- 数据结构实验之图论七:驴友计划
- 数据结构中顺序栈c语言代码实现
- 数据结构上机实践第11周项目2 - 操作用邻接表存储的图
- sql -- 查看数据表创建结构
- 巧用SQL中的WITH(树型结构数据的查询)
- 深入理解PHP内核(五)变量及数据类型-变量的结构和类型
- 数据结构之顺序栈的操作(C语言)
- Scala2.11.7学习笔记(四)常用数据结构
- SQLserver数据文件(MDF)的页面文件头结构剖析
- Redis 数据结构之SDS
- Day16、文件拷贝、数据结构(逻辑结构、物理结构)顺序物理结构、链式物理结构
- 数据结构实验之链表四:有序链表的归并
- 数据结构:实验六(单循环链表实现链式队列)