- void f2()
- {
- cout<<"input HashTable address length:";
- cin>>len;
- p=new node*[len];
- cout<<"Input the value of M in Hash function H(k)=k%M:";
- cin>>M;
- cout<<"Input the number of key to be hashed into the hash table:";
- cin>>numbers;
- for(i=0;i<len;i++)
- {
- p[i]=NULL;
- }
- cout<<"please input the keys:";
- for(i=0;i<numbers;i++)
- {
- cin>>num;
- mod=num%M;
- if(p[mod]==NULL)
- {
- p[mod]=new node;
- p[mod]->data=num;
- p[mod]->next=NULL;
- ASL+=1;
- }
- else
- {
- s=new node;
- s->data=num;
- s->next=NULL;
- Insert(p[mod],s);
- ASL+=ListLength(p[mod]);
- }
- }
- for(i=0;i<len;i++)
- {
- if(p[i]==NULL)
- ASL1+=0;
- else
- ASL1+=ListLength(p[i]);
- }
- cout<<"the hash table is as followed:"<<endl<<endl;
- for(i=0;i<len;i++)
- {
- cout<<setiosflags(ios::left)<<setw(3)<<i;
- if(p[i]==NULL)
- cout<<'^'<<endl;
- else
- {
- Print(p[i]);
- cout<<endl;
- }
- }
- cout<<endl;
- cout<<"Hash search successfully using chain address method to resolve conflicts and the average search length ASL is:";
- cout<<(float)ASL/numbers<<endl;
- cout<<"Hash search unsuccessfully using chain address method to resolve conflicts and the average search length ASL is:";
- cout<<(float)ASL1/len<<endl;
- }
来源: http://www.bubuko.com/infodetail-2674723.html