[20221216]已知某哈希表的装载因子小于1,哈希函数H(key)为关键字(标识符)的第一一个字母在字母表中的序
号,处理冲突的方法为线性探测开放定址法。试编写一个按第一个字母的顺序输出哈希表中所有关键字的算法
[注意:必须使用print函数输出输出关键字,不得使用printf]。
哈希表的类型HashTable定义如下:
#define SUCCESS
#define UNSUCCESS
#define DUPLICATE -1
typedef char StrkeyType[4];
typedef struct {
StrKeyType key; //关键字项
int
tag;
//标记0:空; 1:有效; -1:己删除
void *any;
//其他信息
} RcdType2;
typedef struct {
RcdType2 *rcd;
//存储空间基址
int
size;
//哈希表容量
int
count; // 表中当前记录个数
i HashTable;
实现下列函数:
void PrintKeys(HashTable ht, void(*print)(StrKeyType));
/*依题意用print输出关键字*/
函数输入举例:
0 1 2 3 4 5 6 7 8 9 10
YZ CO -KR
上述哈希表中只有两个关键字: YZ和CO。位置10的关键字前面有“”号,表示该关键字已被删除。
注意:
对于上述关键字,必须先使用print函数打印c0,然后打印Yz, 即结果为: COYZ
#include "allinclude.h" //DO NOT edit this line
void PrintKeys(HashTable ht, void(*print)(StrKeyType)){
/*请调用形参中的print函数输出关键字 */
int i,j;
int k;
int h=ht.count;
StrKeyType *t;
StrKeyType e;
t=(StrKeyType *)malloc((sizeof(StrKeyType ))*h);
for(i=0,j=0;i<ht.size;i++)//遍历整个哈希表
{
if(ht.rcd[i].tag==1)
{
t[j][0]=ht.rcd[i].key[0];//复制key
t[j][1]=ht.rcd[i].key[1];
t[j][2]=NULL;//不能忘记此处要置为空
t[j][3]=NULL;
j++;
}
}
k=j-1;//数组长度
int change;
int lastChange=k;
for(i=k;i>0&&lastChange!=0;i--)
{
change=0;
for(j=0;j<lastChange;j++)
{
if(t[j+1][0]<t[j][0])//若i+1比i小;
{
e[0]=t[j][0];
e[1]=t[j][1];
t[j][0]=t[j+1][0];//小的到前面去
t[j][1]=t[j+1][1];
t[j+1][0]=e[0];
t[j+1][1]=e[1];
change=j;
}
}
lastChange=change;
}
for(i=0;i<=k;i++)
print(t[i]);
}