The C language implements the address book based on hash tables

  • 2020-06-03 07:36:51
  • OfStack

This paper shares the specific code of C language based on hash table to achieve the address book for your reference, the specific content is as follows

1. Demand analysis

This demo program is written in C language to complete the generation of hash table, telephone number insertion, and search and other functions.
(1) Input relevant information of the corresponding contact person as prompted;
(2) Output the stored contact information in the corresponding output form;
(3) The program can achieve the functions of establishing, adding, finding and printing;
(4) The program can judge the illegal data entered by the user and guide the correct input.

2. The profile design

When storing a record of a phone number, if a certain correspondence is established between the storage location and its key so that each key corresponds to one and only one storage location in the storage structure, an image like f(K) with a given value of K can be found in the lookup. If there is a close in the storage structure to find the checked record. This correspondence, f, is called an Hash function or a hash function. The table set up along these lines is called a hash table or hash table. This case design mainly investigates the establishment, search and modification of hash table.

3. The detailed design


#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
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 i,k=0; 
  for(i=0;num[i]!='\0';i++) 
  { 
    k=10*k+num[i]-48;  // Characters are converted to Numbers  
  } 
  k=(k%19);    // Find the hash address by division  
  return k; 
}//c The divisor method deals with phone Numbers  
void create() 
{ 
  struct node *p1; 
  int k1,m=0; 
  while(m==0) 
  { 
   printf(" Please enter the information of the person 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);// with num The array value is passed as a parameter to the hash function k1 
   p1->next=num_list[k1];// � k1 The resulting value is assigned to the bottom of the header as the storage address of the array 1 A node  
   num_list[k1]=p1;//  �  again p1 Is passed to an array, so p1 Can be released as below 1 Node generation  
   printf(" The end of the press 1 , please press to enter again 0\n"); 
   scanf("%d",&m); 
  } 
  printf(" The contact sheet has been created \n"); 
} 
  void dlter() 
{ 
   char num[11]; 
  int k1; 
  int find=0; 
  struct node *f; 
    printf(" Please check the telephone number of the contact to be modified: \n:"); 
    scanf("%s",num); 
    k1=hash(num); 
    f=num_list[k1]; 
    while(f!=NULL) 
    { 
      if(strcmp(f->num,num)==0) 
      { 
       printf(" Found it! Please enter the information of the person to be modified: \n"); 
       scanf("%s%s%s",f->num,f->name,f->address,f->city,f->etp); 
       find=1; 
      } 
      f=f->next; 
    } 
    if(find=0) 
      printf(" No node to delete found! "); 
 
 
  } 
void list() 
{ 
  struct node *f;// Print node pointer  
  int i; 
 
  printf(" Print the address book as follows: \n"); 
  for(i=0;i<19;i++) 
    { 
      f=num_list[i]; 
      while(f!=NULL) 
      { 
        printf("--->num:%s\t name:%s\t address:%s\t city:%s\t etp:%s\t \n",f->num,f->name,f->address,f->city,f->etp); 
        f=f->next; 
 
      } 
    } 
 
  } 
 
 
void add() 
{ 
  char num[11],name[15],address[20],city[15],etp[20]; 
  struct node *p1; 
  int k1; 
  printf(" Please enter the information of the newly added person : The phone   The name   address   city   email \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 telephone number of the inquirer :"); 
    scanf("%s",num); 
    k1=hash(num); 
    f=num_list[k1]; 
    while(f!=NULL) 
    { 
      if(strcmp(f->num,num)==0) 
      { 
        printf(" The contact information you are looking for  :num:%s name:%s address:%s city:%s etp:%s\n",f->num,f->name,f->address,f->city,f->etp); 
        find=1; 
      } 
      f=f->next; 
    } 
    if(find=0) 
      printf(" This contact was not found! "); 
 
 
  } 
void main() 
{ 
  int i; 
  char x; 
  for(i=0;i<19;i++) 
  { 
    num_list[i]=NULL; 
  } 
  while(1) 
  { 
 
//          system("cls"); 
          printf("\n"); 
        printf(" ★★★★★ address book ★★★★★★★★★ ★★★★★★★★★ ★★★★★★★★★ ★★★★ \n"); 
        printf(" U in pieces ---------------------------------- Painted in pieces \n"); 
        printf(" U | 1. Establish a painted | \n"); 
        printf(" Painted u | | \n"); 
        printf(" U | 2. Find the painted | \n"); 
        printf(" Painted u | | \n"); 
        printf(" U | 3. Add painted | \n"); 
        printf(" Painted u | | \n"); 
        printf(" U | 4. Modify the painted | \n"); 
        printf(" Painted u | | \n"); 
        printf(" U | 5. Painted printing | \n"); 
        printf(" Painted u | | \n"); 
        printf(" U | 6. The end of the painted | \n"); 
        printf(" U in pieces ---------------------------------- Painted in pieces \n"); 
        printf(" U u u u u u u u u u u u u u u u u u u u u \n"); 
 
//    x=getchar(); 
    scanf("%s",&x); 
    switch(x) 
    { 
      case '1': create();break; 
      case '2': search();break; 
      case '3': add();break; 
      case '4': dlter();break; 
      case '5': list();break; 
      case '6': return; 
      default:printf(" Please re-enter; \n"); 
    } 
  } 
} 

Related articles: