Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use hash table to realize address book in C language

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use hash table to achieve address book in C language", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language how to use hash table to achieve address book" article.

1. Requirement analysis

The demo program is written in C language to complete the generation of hash table, telephone number insertion, and search and other functions.

(1) enter the relevant information of the corresponding contact as prompted.

(2) output the stored contact data in the corresponding output form.

(3) the program can achieve the functions of establishing, adding, searching and printing.

(4) the program can judge the illegal data entered by the user and guide the correct input.

2. Summary design

When storing a record of a telephone number, if a certain corresponding relationship is established between the storage location and its keyword so that each keyword corresponds to a unique storage location in the storage structure, then when searching, the image f (K) of a given value K can be found according to the corresponding relation f. If there is a connection in the storage structure, find the checked record. This correspondence f is called a Hash function or hash function. The table established according to the above idea is called a hash table or hash table. The design of this case focuses on the establishment, search and modification of hash tables. no, no, no.

3. Detailed design

# include # include # include typedef struct node {char num [11], name [15], address [20], city [15], etp [20]; struct node * next;} NUM; struct NUM * num_list [19]; int hash (char num []) {int iLnum [I]! ='\ 0transitive num [I]!) {karm10simk + Numi]-48; / / convert characters to numbers} k = (k) / / divide to find the hash address return k;} / c divide the remainder to deal with the phone number void create () {struct node * p1; int K1; while (massively zero) {printf ("enter the information you want to add: num name address city etp,\ n"); p1 = (struct node *) malloc (sizeof (struct node)); scanf ("% s", p1-> num); scanf ("% s", p1-> name) Scanf ("% s", p1-> address); scanf ("% s", p1-> city); scanf ("% s", p1-> etp); k1=hash (p1-> num); / / pass the num array value as an argument to the hash function to get K1 p1-> next=num_list [K1]; / / assign the value obtained by K1 as the storage address of the array to the next node of the header node num_ list [K1] = p1 / / then pass the data of p1 to the array, so p1 can release to generate printf as the next node ("end, press 1, enter again, press 0\ n"); scanf ("% d", & m);} printf ("address table has been created\ n");} void dlter () {char num [11]; int K1; int find=0; struct node * f Printf ("Please check the phone number of the contact to be modified:\ n:"); scanf ("% s", num); k1=hash (num); fumbnumList [K1]; while (faded null) {if (strcmp (f-> num,num) = = 0) {printf ("found it! Please enter the profile of the person you want to modify:\ n "); scanf ("% s%s%s ", f-> num,f- > name,f- > address,f- > city,f- > etp); find=1;} flipf-> next;} if (find=0) printf (" Node not found for deletion! ") ;} void list () {struct node * f racket / print node pointer int i; printf ("print address book as follows:\ n"); for (iDeplotted inumhand f-> name,f- > address,f- > city,f- > etp); void add () {char num [11], name [15], address [20], city [15], etp [20]; struct node * p1 Int K1; printf ("Please enter the newly added person's information: phone name and address city mailbox\ n"); p1 = (struct node *) malloc (sizeof (struct node)); scanf ("% s%s%s", num,name,address); strcpy (p1-> num,num); strcpy (p1-> name,name); strcpy (p1-> address,address); strcpy (p1-> city,city); strcpy (p1-> etp,etp) K1=hash (p1-> num); p1-> next=num_ list [K1]; num_ list [K1] = p1; printf ("ok\ n");} void search () {char num [11]; int K1; int find=0; struct node * f; printf ("Please enter the caller's phone number:"); scanf ("% s", num); k1=hash (num); fannum _ list [K1] While (flips null) {if (strcmp (f-> num,num) = = 0) {printf ("contact information to be found: num:%s name:%s address:%s city:%s etp:%s\ n", f-> num,f- > name,f- > address,f- > city,f- > etp); find=1;} frankf-> next } if (find=0) printf ("this contact was not found!") ;} void main () {int i; char x; for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report