hash表的实现

更新时间:2024-06-02 05:10:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

hash表的实现

#include #define M 13 typedef int KeyType; KeyType ht[M]; int p=13;

int hash(KeyType k)

{

return k%p;

} void init()

{ int i;

for(i=0;i

ht[i]=0; }

void insert(KeyType k)

{ int d,i; d=hash(k);

for(i=0;i

ht[(i+d)%M]=k;

}

int search(KeyType k)

{ int i,d; d=hash(k); if(ht[d]==k) return d; else {

for(i=1;i

if(i

d=(d+i)%M; return d;

} else return -1;

}

}

void print()

{ int i;

for(i=0;i

}

void main()

/*{ KeyType k; init();

printf(\scanf(\while(k!=0)

{ insert(k); scanf(\

} print();

printf(\

scanf(\if(search(k)!=-1)

printf(\

else

printf(\

}*/ int result; char h,select; //clrscr(); printf(\

printf(\

\

printf(\

printf(\线性表的顺序表示--------------------- \

printf(\

lable0:

printf(\线性表L不存在,是否创建线性表L(Y/N)?:\

h=getch();

if(h=='y'||h=='Y') result=init();

else goto end;

if(result==0||result==-1){printf(\线性表初始长度不合法,必须是大于或等于1!!\

lable0;}

if(result==2) goto lable1;

lable1: //clrscr(); printf(\

printf(\

***********************************************************

\

printf(\printf(\ 恭喜你!线性表L已创建好,

继续操作! \

printf(\printf(\ 1.插入新的数据元素请按

I \

printf(\

printf(\ 2.表尾插入新的数据元素

请按 p \

printf(\printf(\ 3.删除表中数据元素请按

D \

printf(\printf(\ 4.删除表尾元素请按

K \

printf(\

printf(\ 5.清空线性表请按

Q \

printf(\

printf(\ 6.返回线性表表长请按

F \

printf(\

printf(\ 7.查找数据元素请按

L \

printf(\

printf(\ 8.输出线性表元素请按

V \

printf(\

printf(\

***********************************************************

\

printf(\

printf(\ 请输入你的选择:\

select=getch();

if(select=='i'||select=='I')

{ int i,e; int flag1;

printf(\请输入插入位置和元素值:\

scanf(\flag1=insertlist(i,e); if(flag1==0 ||flag1==1)

{printf(\插入位置不合法或扩充存储空间失败,是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} else

{printf(\插入元素成功!是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} }

else if(select=='p'||select=='P')

{ int i,e; int flag1; printf(\请输入插入元素值:\

scanf(\flag1=append(e); if(flag1==1)

{printf(\扩充存储空间失败,是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} else

{printf(\插入元素成功!是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} }

else if(select=='d'||select=='D')

{ int i,e; int flag1; printf(\请输入删除元素的位序:\

scanf(\flag1=deletelist(i); if(flag1==0)

{printf(\删除元素不存在或是空表,操作失败!,是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} else

{printf(\删除元素成功!是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} }

else if(select=='k'||select=='K')

{ int i,e; int flag1;

flag1=deletelist_end();

if(flag1==0)

{printf(\是空表,操作失败!,是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} else

{printf(\删除表尾元素成功!是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} }

else if(select=='q'||select=='Q')

{

clearlist();

printf(\清空线性表成功!是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

}

else if(select=='f'||select=='F')

{

printf(\当前线性表长度为:\printf(\

printf(\是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

}

else if(select=='l'||select=='L')

{ int i,e; int flag1;

printf(\请输入要查找的元素值:\

scanf(\flag1=find(e); printf(\if(flag1==0)

{printf(\是空表,或该数据元素不存在!,是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} else {

printf(\找到了,是L中的第\

printf(\printf(\个数据元素\

printf(\

printf(\是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

}

}

else if(select=='v'||select=='V')

{ int i,e; int flag1;

printf(\当前表中数据元素有:\

flag1=print(); if(flag1==0) {printf(\是空表!\printf(\

printf(\是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} else {

printf(\是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} } else { printf(\选择有误!是否继续(Y/N)?\

select=getch();

if(select=='y'||select=='Y') goto lable1;

goto end;

} end:; }

本文来源:https://www.bwwdw.com/article/qjl6.html

Top