The C language implements a simple address book

  • 2020-06-23 01:16:25
  • OfStack

This paper shares the specific code of C language for your reference. The specific content is as follows

Function Description:

1. Store the relevant information of the contact person, including name, gender, telephone number and remarks
2. Output all address book information
3. Empty your address book
4. User interaction interface
5. Insert new contact information
6. Find contacts, including "search by name" and "search by phone number"
7. Delete contacts, including "delete by Name" and "delete by phone number"
8. Filter contact information by gender

In this address book code, involving C advanced and data structure 1 knowledge, including Pointers, structure, data structure (linked list) and so on.

Code:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 20
#define OK 1
#define Error 0
typedef struct Node // define 1 A structure that stores data 
{            
 char name[SIZE];
 char s;
 long int number;
 char remark[SIZE];
 struct Node *next;
}Node;
typedef struct Node *LinkList; // A pointer to a structure is defined 
 
/************ create 1 An empty node, as the head node ***************/
LinkList CreateEmptyLinkList()
{
 LinkList p;
 p=(LinkList)malloc(sizeof(Node));  // Manual application 1 Address of heap area, which provides space for nodes 
 if(p==NULL)
 {
 printf("CreateEmptyLinkList Error\n");
 exit(0);
 }
 p->next=NULL;
 return p;
}
 
 
/************ Add a new contact in the header ************************/
 
int CreateLinkList(LinkList Q)
{
 LinkList p;
 char name[SIZE],s,remark[SIZE];
 long int num;
 p=(LinkList)malloc(sizeof(Node));
 if(p==NULL)
 {
 printf("CreateLinkList Error\n");
 return Error;
 }
 printf(" Please enter your name: ");
 scanf("%s",name);
 strcpy(p->name,name);
 printf(" Please enter gender ( m/ Male, f/ Female) : ");
 scanf("%s",&s);
 while(s!='m'&&s!='f')
 {
 printf(" Input error \n");
 printf(" Please enter gender ( m/ Male, f/ Female) : ");
 scanf("%s",&s);
 }
 printf(" Please enter the telephone number: ");
 scanf("%ld",&num);
 printf(" Please enter comments: ");
 scanf("%s",remark);
 p->s=s;
 p->number=num;
 strcpy(p->remark,remark);
 p->next=Q->next;
 Q->next=p;
 return OK;
}
/********************* Make the original contact information ****************/
int FirstCreateLinkList(LinkList Q)
{
 LinkList x,y,z;
 x=(LinkList)malloc(sizeof(Node));
 if(x==NULL)
 {
 printf("CreateLinkList Error\n");
 return Error;
 }
 strcpy(x->name,"LiLi");
 x->s='f';
 x->number=10110;
 x->next=Q->next;
 strcpy(x->remark,"LiLi");
 Q->next=x;
 y=(LinkList)malloc(sizeof(Node));
 if(y==NULL)
 {
 printf("CreateLinkList Error\n");
 return Error;
 }
 strcpy(y->name,"NaNa");
 y->s='f';
 y->number=11100;
 strcpy(y->remark,"NaNa");
 y->next=Q->next;
 Q->next=y;
 z=(LinkList)malloc(sizeof(Node));
 if(z==NULL)
 {
 printf("CreateLinkList Error\n");
 return Error;
 }
 strcpy(z->name,"LiuLiu");
 z->s='m';
 z->number=10000;
 strcpy(z->remark,"LiuLiu");
 z->next=Q->next;
 Q->next=z;
 return OK;
}
/***************** Delete by name 1 A contact ****************************/
 
int DeleteNameLinkList(LinkList L,char name1[SIZE])
{
 LinkList p,q;
 p=L;
 while(p->next && strcmp(p->next->name,name1))
 {
 p=p->next;
 }
 if(!p->next)
 {
 printf(" This address book does not have a contact to delete, delete failed! \n");
 return Error;
 }
 else
 {
 q=p->next;
 printf(" Deleted contact :\n");
 printf(" Name: %s\n",q->name);
 if(q->s=='m')
 printf(" Gender: male \n");
 else
 printf(" Gender: female \n");
 printf(" Telephone No. : %ld\n",q->number);
 printf(" Remark: %s\n",q->remark); 
 
 p->next=q->next;
 free(q);
 return OK;
 }
}
/***************** Delete by number 1 A contact ****************************/
 
int DeleteNumLinkList(LinkList L,long int j)
{
 LinkList p,q;
 p=L;
 while((p->next->number!=j)&&((p->next)!=NULL))
 {
 p=p->next;
 if((p->next)==NULL)
 {
  printf(" This address book does not have a contact to delete, delete failed! \n");
  return Error;
 }
 }
 q=p->next;
 printf(" Deleted contact :\n");
 printf(" Name: %s\n",q->name);
 if(q->s=='m')
 printf(" Gender: male \n");
 else
 printf(" Gender: female \n");
 printf(" Telephone No. : %ld\n",q->number);
 printf(" Remark: %s\n",q->remark); 
 p->next=q->next;
 free(q);
 return OK;
}
 
/****************** Search by name 1 A contact ****************************/
int FindNameLinkList(LinkList L,char name2[SIZE])
{
 LinkList p,q;
 p=L;
 while(strcmp(p->next->name,name2)!=0&&(p->next!=NULL))
 {
 p=p->next;
 if(p->next==NULL)
 {
  printf(" The address book does not have the person you are looking for. The search failed \n");
  return Error;
 }
 }
 q=p->next;
 printf(" Find records: \n");
 printf(" Name: %s\n",q->name);
 if(q->s=='m')
 printf(" Gender: male \n");
 else
 printf(" Gender: female \n");
 printf(" Telephone No. : %ld\n",q->number);
 printf(" Remark: %s\n",q->remark);
 printf("*********************************\n");
 return OK;
}
/****************** Search by number 1 A contact ****************************/
int FindNumLinkList(LinkList L,long int j)
{
 LinkList p,q;
 p=L;
 while((p->next->number!=j)&&((p->next)!=NULL))
 {
 p=p->next;
 if((p->next)==NULL)
 {
  printf(" The address book does not have the person you are looking for. The search failed \n");
  return Error;
 }
 }
 q=p->next;
 printf(" Find records: \n");
 printf(" Name: %s\n",q->name);
 if(q->s=='m')
 printf(" Gender: male \n");
 else
 printf(" Gender: female \n");
 printf(" Telephone No. : %ld\n",q->number);
 printf(" Remark: %s\n",q->remark);
 printf("*********************************\n");
 return OK;
}
/****************** To find the 1 A contact ****************************/
int FindLinkList(LinkList L)
{
 LinkList head = L;   
 printf("*********************************\n");
 printf(" Please enter how to find contacts: \n");
 printf("1: According to the name \n");
 printf("2: According to the number \n");
 printf("0: return \n");
 printf("*********************************\n");
 printf(" Please select: ");
 int k=3;  // Make sure the following while Cycle operation 
 while(k)
 {
 scanf("%d",&k);
 char Delname1[SIZE];
 long int N;
 if(k>2||k<0)
 {
  printf(" Input error, please re-enter: ");
  scanf("%d",&k);
  while(getchar()!='\n')
  printf("\n");
 }
 switch(k)
 {
  case 1:
  printf(" Please enter your name: ");
  scanf("%s",Delname1);
  FindNameLinkList(head,Delname1);
  k=0;
  break;
  case 2:
  printf(" Please enter the number: ");
  scanf("%ld",&N);
  FindNumLinkList(head,N);
  k=0;
  break;
 }
 
 }
}
/******************** Empty your contact information *************************/
int ClearLinkList(LinkList L)
{
 LinkList p,q;
 p=L->next;
 while(p)
 {
 q=p->next;
 free(p);
 p=q;
 }
 L->next=NULL;
 printf(" Cleared all contacts successfully \n");
 return OK;
}
 
/********************* Screen all male contacts **********************/
int ScreenMaleLinkList(LinkList L)
{
 LinkList p;
 p=L->next;
 int i=0;
 while(p)
 {
 if(p->s=='m')
 {
  printf(" Name: %s\n",p->name);
  if(p->s=='m')
  printf(" Gender: male \n");
  else
  printf(" Gender: female \n");
  printf(" Telephone No. : %ld\n",p->number);
  printf(" Remark: %s\n",p->remark); 
  printf("*********************************\n");
 }
 p=p->next;
 i++;
 }
 if(i==0&&!p)
 {
 printf(" No male contact \n");
 }
 return OK;
}
/********************* Select female contacts **********************/
int ScreenFemaleLinkList(LinkList L)
{
 LinkList p;
 p=L->next;
 int i=0;
 while(p)
 {
 if(p->s=='f')
 {
  printf(" Name: %s\n",p->name);
  if(p->s=='m')
  printf(" Gender: male \n");
  else
  printf(" Gender: female \n");
  printf(" Telephone No. : %ld\n",p->number);
  printf(" Remark: %s\n",p->remark); 
  printf("*********************************\n");
 }
 p=p->next;
 i++;
 }
 if(i==0&&!p)
 {
 printf(" No female contact \n");
 }
 return OK;
}
/************************ Address book function interface *****************/
void FunctionalInterface()
{
 system ("clear");
 printf("*********************************\n");
 printf(" Welcome to use Linux The address book! \n");
 printf("*********************************\n");
 printf("1: Output all contact information \n");
 printf("2: Insert a new contact \n");
 printf("3: delete 1 A contact \n");
 printf("4: Find a contact \n");
 printf("5: Empty all contact information \n");
 printf("6: Screen all male contacts \n");
 printf("7: Screen all female contacts \n");
 printf("0: exit \n");
 printf("*********************************\n");
}
/***************** delete 1 A contact ****************************/
 
int DeleteLinkList(LinkList L)
{
 LinkList head = L;
 printf("*********************************\n");
 printf(" Please enter how to delete the contact: \n");
 printf("1: According to the name \n");
 printf("2: According to the number \n");
 printf("0: return \n");
 printf("*********************************\n");
 printf(" Please select: ");
 int i=3;
 while(i)
 {
 scanf("%d",&i);
 char Delname[SIZE];
 long int j;
 if(i>2||i<0)
 {
  printf(" Input error, please re-enter: ");
  scanf("%d",&i);
  while(getchar()!='\n')
  printf("\n");
 }
 switch(i)
 {
  case 1:
  printf(" Please enter your name: ");
  scanf("%s",Delname);
  DeleteNameLinkList(head,Delname);
  i=0;
  break;
  case 2:
  printf(" Please enter the number: ");
  scanf("%ld",&j);
  DeleteNumLinkList(head,j);
  i=0;
  break;
 }
 
 }
 
}
/******************* Go through and print the entire list **********************/
int PrintfLinkList(LinkList L)
{
 LinkList p,q;
 q=p=L->next;
 int i=0;
 while(q)
 {
 i++;
 q=q->next;
 if(i==0&&!p)
 {
 printf(" There is no contact \n");
 }
 }
 printf(" A total of %d A contact \n",i);
 while(p)
 {
 printf("*********************************\n");
 printf(" Name: %s\n",p->name);
 if(p->s=='m')
  printf(" Gender: male \n");
 else
  printf(" Gender: female \n");
 printf(" Telephone No. : %ld\n",p->number);
 printf(" Remark: %s\n",p->remark); 
 p=p->next;
 }
 printf("*********************************\n");
 return OK;
 
}
 
int main()
{
 int a;
 LinkList head;
 head=CreateEmptyLinkList();
 FirstCreateLinkList(head);
 FunctionalInterface();
 while(a)
 {
 printf(" Please enter the function you want to select :");
 scanf("%d",&a);
 if(a>7||a<0)
 {
  printf(" Input error, please re-enter: ");
  scanf("%d",&a);
  while(getchar()!='\n')
  printf("\n");
 }
 switch(a)
 {
  case 1:
  PrintfLinkList(head);
  break;
  case 2:
  CreateLinkList(head);
  break;
  case 3:
  DeleteLinkList(head);
  break;
  case 4:
  FindLinkList(head);
  break;
  case 5:
  ClearLinkList(head);
  break;
  case 6:
  ScreenMaleLinkList(head);
  break;
  case 7:
  ScreenFemaleLinkList(head);
  break;
  }
 if(a!=0)
 {
  printf(" Please press enter to continue: ");
  getchar();
  if(getchar()=='\n')
  FunctionalInterface();
 }
 }
 printf(" Welcome to use again \n");
 return 0;
}

The above is for their own preparation, personal ability is limited, if there is a mistake, please criticize and correct.


Related articles: