C language implements a simple address book

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

C language to write a simple address book, say simple 1 function is simple, 2 is also not add read and write file operation, just as a linked list operation of an exercise, hope to give the novice 1 this guidance and help.

Code:


/*  Please note the source          */ 
/*  By Urahara (ID:blueboy82006)      */ 
/* http://blog.csdn.net/blueboy82006     */ 
#include <stdio.h> 
#include <stdlib.h> 
#include<string.h> 
#include<ctype.h> 
typedef struct node{ 
 char name[12]; 
 char phone[13]; 
 struct node *next; 
}LNode; 
LNode *h;        // The first node  
static int flag=1;     // Address book creation tag  
int CreateNode(void)     // Create a single circular linked list  
{ 
 if(flag) 
 { 
  LNode *p; 
  h=(LNode *)malloc(sizeof(LNode)); 
  h->next=NULL; 
  p=h; 
  flag--; 
  return 1; 
 } 
 else 
  return 0; 
} 
int InsertNode(void)    // Insert the function  
{ 
 if(!flag) 
 { 
  LNode *t; 
  char name1[12]; 
  char phone1[13]; 
  printf("/n Enter name: "); 
  scanf("%s",name1); 
  printf("/n Enter contact number: "); 
  scanf("%s",phone1); 
  t=(LNode *)malloc(sizeof(LNode)); 
  strcpy(t->name,name1); 
  strcpy(t->phone,phone1); 
  t->next=h->next; 
  h->next=t; 
  return 1; 
 } 
 else 
  return 0; 
} 
int SearchNode(void)    // Query function  
{ 
 if(!flag) 
 { 
  LNode *p; 
  int x=0; 
  char name1[12]; 
  printf("/n Input query name: "); 
  scanf("%s",name1); 
  p=h; 
  printf("/n Query results: "); 
  while(p->next) 
  { 
   p=p->next; 
   if(strcmp(p->name,name1)==0) 
   { 
    printf("/n Name: %s/n Contact Number: %s/n",p->name,p->phone); 
    x++; 
   } 
  } 
  if(!x) 
   printf("/n No information found! "); 
  return 1; 
 }else 
  return 0; 
} 
int DeletNode(void)    // Delete function  
{ 
 if(!flag) 
 { 
  LNode *pre,*p; 
  char name1[12]; 
  p=h; 
  printf("/n Enter the name of the contact to delete: "); 
  scanf("%s",name1); 
  while(p->next) 
  { 
   pre=p; 
   p=p->next; 
   if(strcmp(p->name,name1)==0) 
   { 
    pre->next=p->next; 
    free(p); 
    printf(" Delete successful! /n"); 
    break; 
   } 
  } 
  if(!p->next) 
   printf("/n No information to delete found! "); 
  return 1; 
 } 
 else 
  return 0; 
} 
int PrintNode(void)    // Output function  
{ 
 if(!flag) 
 { 
  LNode *p; 
  p=h; 
  while(p->next) 
  { 
   p=p->next; 
   printf("/n Name: %s /t Contact Number: %s",p->name,p->phone); 
  } 
  return 1; 
 } 
 else 
  return 0; 
} 
int main()       // The main function  
{ 
 char n; 
 printf("/n/n"); 
 printf("/t/t    The address book /t/n"); 
 printf("/t/t| -- -- -- -- -- -- -- -- |/n"); 
 printf("/t/t|        |/n"); 
 printf("/t/t| [1]  Create an address book      |/n"); 
 printf("/t/t| [2]  Insert the name of the contact    |/n"); 
 printf("/t/t| [3]  Find the name of the contact    |/n"); 
 printf("/t/t| [4]  Delete the name of the contact    |/n"); 
 printf("/t/t| [5]  Output all contact information    |/n"); 
 printf("/t/t| [0]  exit       |/n"); 
 printf("/t/t|        |/n"); 
 printf("/t/t|  If you have not created a new table, please do so first!   |/n"); 
 printf("/t/t|        |/n"); 
 printf("/t/t| -- -- -- -- -- -- -- -- |/n"); 
 printf("/n"); 
 printf(" Please enter your options (0-5):"); 
 while(1) 
 { 
  scanf("%s",&n); 
  while(!(n=='0'||n=='1'||n=='2'||n=='3'||n=='4'||n=='5')) 
  { 
   printf(" Please enter your options (0-5):"); 
   scanf("%s",&n); 
  } 
  system("CLS"); 
  switch(n) 
  { 
  case '0':{ 
   printf("/n/t/t Thank you for your use! /n"); 
   return 0; 
     } 
  case '1':{ 
   if(CreateNode()) 
    printf(" Address book set up successfully! /n"); 
   else 
    printf(" The address book has been set up, no need to repeat! /n"); 
   break; 
     } 
  case '2':{ 
   if(InsertNode()) 
    printf(" Added successfully! /n"); 
   else 
    printf(" Add failed, please create address book first! /n"); 
   break; 
     } 
  case '3':{ 
   if(SearchNode()) 
    printf("/n"); 
   else 
    printf(" Search failed, please create address book first! /n"); 
   break; 
     } 
  case '4':{ 
   if(DeletNode()) 
    printf("/n"); 
   else 
    printf(" Delete failed, please create address book first! /n"); 
   break; 
     } 
  case '5':{ 
   if(PrintNode()) 
    printf("/n Above is all contact information ./n"); 
   else 
    printf("ERROR , please create the address book first! /n"); 
    
   break; 
     } 
  default:{printf(" Input does not meet the requirements! ");} 
  } 
  printf("/n/n"); 
  printf("/t/t    The address book /t/n"); 
  printf("/t/t| -- -- -- -- -- -- -- -- |/n"); 
  printf("/t/t|        |/n"); 
  printf("/t/t| [1]  Create an address book      |/n"); 
  printf("/t/t| [2]  Insert the name of the contact    |/n"); 
  printf("/t/t| [3]  Find the name of the contact    |/n"); 
  printf("/t/t| [4]  Delete the name of the contact    |/n"); 
  printf("/t/t| [5]  Output all contact information    |/n"); 
  printf("/t/t| [0]  exit       |/n"); 
  printf("/t/t|        |/n"); 
  printf("/t/t|  If you have not created a new table, please do so first!   |/n"); 
  printf("/t/t|        |/n"); 
  printf("/t/t| -- -- -- -- -- -- -- -- |/n"); 
  printf("/n"); 
 } 
} 

Related articles: