散列表链地址法基本思想是将相同散列地址的元素放在同一个单链表中,即称同义词链表。例如此时我们设一个散列函数H(key)=key%n,则可以定义一个一维数组,大小为n,数组元素对应关键字模n所得的数字。
如下图:n为13,各关键字模13后插入对应值的表中。
代码部分
存储结构:
typedef struct hashnode
{
int data;
struct hashnode* next;
}hashnode,*hashbit;
data存元素值,next指向冲突元素地址。
相关函数:
void inithash(hashbit H[],int n) //初始化
{
int i;
for(i=0;i<n;i++)
{
H[i]=new hashnode;
H[i]->data=i;
H[i]->next=NULL;
}
}
void inserthash(hashbit H[],int x,int n) //插入
{
int i=x%n;
hashbit p=new hashnode;
hashbit q=new hashnode;
q->data=x;
p=H[i];
while(p