hash表的实现

hash表的实现
hash表的实现

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

printf("%4d",ht[i]);

printf("\n");

}

void main()

/*{

KeyType k;

init();

printf("input keys\n");

scanf("%d",&k);

while(k!=0)

{

insert(k);

scanf("%d",&k);

}

print();

printf("\ninput the key which you want to search\n");

scanf("%d",&k);

if(search(k)!=-1)

printf("success! and its location is %d",search(k));

else

printf("unsuccess !");

}*/

int result;

char h,select;

//clrscr();

printf("\n\n\n\n");

printf("***********************************************************

");

printf("\n\n\n\n");

printf(" ----------------------线性表的顺序表示--------------------- ");

printf("\n\n\n");

lable0:

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

h=getch();

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

else goto end;

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

lable0;}

if(result==2) goto lable1;

lable1:

//clrscr();

printf("\n\n\n\n");

printf("

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

");

printf("\n\n\n\n");

printf(" 恭喜你!线性表L已创建好,继续操作!");

printf("\n\n\n\n");

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

I ");

printf("\n\n");

printf(" 2.表尾插入新的数据元素请按p ");

printf("\n\n");

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

D ");

printf("\n\n");

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

K ");

printf("\n\n");

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

Q ");

printf("\n\n");

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

F ");

printf("\n\n");

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

L ");

printf("\n\n");

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

V ");

printf("\n\n\n\n");

printf("

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

");

printf("\n\n");

printf(" 请输入你的选择:");

select=getch();

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

{

int i,e;

int flag1;

printf("请输入插入位置和元素值:");

scanf("%d,%d",&i,&e);

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("%d",&e);

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("%d",&i);

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("%d\n\n",length());

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("%d",&e);

flag1=find(e);

printf("\n\n");

if(flag1==0)

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

select=getch();

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

goto end;

}

else

{

printf("找到了,是L中的第");

printf("%d",flag1);

printf("个数据元素");

printf("\n\n");

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("\n当前表中数据元素有:");

flag1=print();

if(flag1==0)

{printf("是空表!");

printf("\n\n");

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:;

}

相关主题
相关文档
最新文档