#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef struct List{
int data;
struct List *netc;
}List;
typedef struct mean{
List *next;
int data1;
}Mean;
//尾插法
void print(struct mean *list,int e,int key,int a){
int n=e%key;//取余找对应地址
List *p=new List;// 开辟一个结构体
p->data=e;//存入数据
if(list[n].next==NULL){//判断是否为插入的第一个结点
p->netc=list[n].next;
list[n].next=p;
} else{//使end一直指向尾节点
List *end;
end=list[n].next;
while(end->netc){
end=end->netc;
}
end->netc=p;
p->netc=NULL;
}
}
//头插法
void print1(struct mean *list,int e,int key,int a){
int n=e%key;
List *p=new List;
p->data=e;
p->netc=list[n].next;
list[n].next=p;
}
void search(struct mean *list,int e,int key,int a){
int n=e%key;
int d=1;
int sum=0;
List *p;
p=list[n].next;
while(p){
sum++;
if(p->data!=e){
p=p->netc;
}else{
d=0;
printf("查找的链表下标为:%d \n比较次数为:%d\n",n,sum);
break;
}
}
if(d){
printf("查找失败\n");
}
}
/*
13 13
12
19 14 23 1 68 20 84 27 55 11 10 79
*/
int main(){
int a,key,m,element,e;//表长,模数;
scanf("%d %d",&a,&key);
struct mean *list=(Mean*)malloc(sizeof(Mean)*a);
for(int i=0;i<13;i++){
list[i].next=NULL;
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&element);
print1(list,element,key,a);
}
printf("录入数据成功\n");
scanf("%d",&e);
search(list,e,key,a);
return 0;
}
01-08
760