void InsertHTZip(HashTableZip ha[], int& n, int p, KeyType k)
{
int adr;
adr = k % p;
NodeType* q;
q = (NodeType*)malloc(sizeof(NodeType));
q->key = k;
q->next = NULL;
if (ha[adr].firstp == NULL)
ha[adr].firstp = q;
else
{
q->next = ha[adr].firstp;
ha[adr].firstp = q;
}
n++;
}
创建
void CreateHTZip(HashTableZip ha[], int& n, int m, int p, KeyType keys[], int n1)
{
for (int i = 0; i < m; i++)
ha[i].firstp = NULL;
n = 0;
for (int i = 0; i < n1; i++)
InsertHTZip(ha, n, p, keys[i]);
}
删除
//删除
bool DeleteHTZip(HashTableZip ha[], int& n, int m, int p, KeyType k)
{
int adr;
adr = k % p;
NodeType* q, * preq;
q = ha[adr].firstp;
if (q == NULL)
return false;
if (q->key == k) //首结点
{
ha[adr].firstp = q->next;
free(q);
n--;
return true;
}
preq = q;
q = q->next;
// 一直找
while (q != NULL)
{
if (q->key == k)
break;
q = q->next;
}
// 判断是不是找到了
if (q != NULL)
{
preq->next = q->next;
free(q);
n--;
return true;
}
else
return false;
}
查找
// 查找
void SearchHTZip(HashTableZip ha[], int p, KeyType k)
{
int adr, i = 0;
adr = k % p;
NodeType* q;
q = ha[adr].firstp;
// 一直找
while (q != NULL)
{
i++;
if (q->key == k)
break;
q = q->next;
}
// 判断是不是找到了
if (q != NULL)
cout << "成功:关键字:" << k << ",比较" << i << "次" << endl;
else
cout << "失败:关键字:" << k << ",比较" << i << "次" << endl;
}
ASL计算
void ASLZip(HashTableZip ha[], int n, int m)
{
int succ = 0, unsucc = 0;
int s;
NodeType* q;
for (int i = 0; i < m; i++)
{
s = 0;
q = ha[i].firstp;
while (q != NULL)
{
q = q->next;
s++;
succ += s;
}
unsucc += s;
}
cout << "成功ASL" << n << "-" << succ * 1.0 / n;
cout << "不成功ASL" << n << "-" << unsucc * 1.0 / m;
}