current position:Home>How to solve the run-time exception of test times

How to solve the run-time exception of test times

2022-01-27 07:51:32 CSDN Q & A

### subject :【 Problem description 】

Design hash table to realize telephone number query system . The following functionality is required :

1、 Each element in the hash table has the following data items : Phone number 、 user name 、 address ;

2、 Read in the elements , Create a hash table with the phone number as the keyword ( The length of the hash table is 8, Storage location: 0 To 7);

3、 The hash table is hashed by division and residue method , That is, take the telephone number as the keyword , Send the telephone number to 11 Add bits , According to the model 7 Remainder ;

4、 Use chain address method to solve conflicts ;

Be sure to use hash table , If you use other search methods , The score of this question is 0.
【 Input form 】

On the first line, enter the number of elements n

below n Element information of behavior input

The following lines query information , Each line of information is a telephone number , The corresponding output is name , If there is no such result, output No Person

【 Output form 】

According to the element information queried in the input , For output
【 The sample input 】

5

Zhangsan 13911233322 China

Lisi 15911225487 Baoding

Wangwu 13865894587 Ncepu

Liuliu 19845786987 Beijing

Liqi 16896874521 Xiongan

15911225487

19845786987

14523658745

【 Sample output 】

Lisi

Liuliu

No Person

#include<iostream>#include<string>using namespace std;int hash1(int key){    int key1=0;    while(key>0){        key1+=key%10;        key=key/10;    }    key1=key1%7;    return key1;}typedef struct hashnode{    int key;    string name;    string address;    struct hashnode* next;}*phashnode;void search(phashnode hst[],int key){    int h=hash1(key);    phashnode p=hst[h];    while(p->key!=key){        p=p->next;        if(p==NULL){            cout<<"No Person"<<endl;        }    }cout<<p->name<<endl;}void insert1(phashnode hst[],int key,string name,string address){    int h=hash1(key);    if(hst[h]==NULL){        phashnode q=new hashnode;        q->key=key;        q->name=name;        q->address=address;        q->next=NhLL;        hst[h]->next=q;    }    else    {        phashnode s=hst[h];        while(s->key!=key&&s->next!=NULL)            s=s->next;        if(s->key==key)        return;        else{            phashnode q=new hashnode;            q->key=key;            q->next=NULL;            s->next=q;        }    }}int main(){    phashnode hst[7];    int n;    cin>>n;    string name;    string address;    int key;    for(int i=0;i<n;i++){            cin>>name>>key>>address;        insert1(hst,key,name,address);    }    while(cin>>key){        search(hst,key);    }return 0;}````



Refer to the answer 1:

q->next=NhLL;
Change it to
q->next=NULL;

img




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270751304554.html

Random recommended