C语言链表实现的通讯录系统

最近要用c做一个嵌入式项目,写了一个简单的通讯录系统,界面就是linux的命令行界面,比较丑哈,目前只有简单的增删改查功能,后期可改成用文件或者数据库存储数据,即可实现数据的持久性。

C语言链表实现的通讯录系统_第1张图片

  • 联系人结构体:
    成员比较少,需要其他的再添加就行了
#include
unsigned int number;//联系人个数
//结构体
struct contact
{
 int num;   //编号
 char name[20];     //姓名
 char phone[20];  //手机号
 char address[100];  //地址
 struct  contact * next;
};
typedef struct contact * Book; 
typedef struct contact Contact; 

  • 创建联系人:
#include
#include
#include"contact.h"
Book create_contact(Book book_head)
{
 Book new_contact=(Book)malloc(sizeof(Contact));
 if(new_contact==NULL)
 {
  printf("CREATE FAIL\n");
 }
 printf("PLEASE INPUT CONTACT INFORMATION\n");
 printf("INPUT NAME:");
 scanf("%s",new_contact->name);
 printf("INPUT PHONE:");
 scanf("%s",new_contact->phone);
 printf("INPUT ADDRESS:");
 scanf("%s",new_contact->address);
 insert_contact_sort(book_head,new_contact);
}
  • 联系人排序插入链表

C语言链表实现的通讯录系统_第2张图片
插入和删除的时候联系人的num值都会动态改变,这个num值感觉弄得有点多余了

#include
#include
#include"contact.h"
unsigned int number;
//按名字首字母排序插入
void insert_contact_sort(Book book_head,Book new_contact)
{
 Book q=book_head;
 Book p=book_head->next;
 while(p!=NULL&&strcmpi(p->name,new_contact->name)<0)
 {
  q=p;
  p=p->next;
 }
 new_contact->next=p;
 q->next=new_contact;
 number++;
 if(p!=NULL)
  new_contact->num=p->num;
 else
  new_contact->num=number;
 while(p!=NULL)
 {
  p->num+=1;
  p=p->next;
 }
 show_book(book_head);
}

  • 删除单个联系人
    C语言链表实现的通讯录系统_第3张图片
#include
#include
#include
#include
#include"contact.h"
extern unsigned int number;
void delete_contact(Book book_head)
{
 show_book(book_head);
 char name[20];
 printf("PLEASE INPUT DELETE CONTACT NUM:");
 scanf("%s",name);
 Book q=book_head;
 Book p=q->next;
 Book r=NULL;
 while(p!=NULL&&strcmp(p->name,name)!=0)
 {
  q=p;
  p=p->next;
 }
 if(p!=NULL)
 {
  r=p->next;
  q->next=p->next;
  free(p);
  while(r!=NULL)
  {
   r->num-=1;
   r=r->next;
  }
  number-=1;printf("DELETE SUCCESS!!!\n");
  show_book(book_head);
 }
 else
 {
  printf("DELETE FAIL,NOT FIND THE CONTACT NUM!!!\n");
 }
}

  • 修改联系人信息

C语言链表实现的通讯录系统_第4张图片

#include
#include
#include"contact.h"
unsigned int number;
//修改指定联系人
void modify_contact(Book book_head)
{
 show_book(book_head);
 int num;
 Book p=book_head->next;
 printf("PLEASE INPUT CONTACT NUM:");
 scanf("%d",&num);
 while(p!=NULL&&p->num!=num)
 {
  p=p->next;
 }
 if(p!=NULL)
 {
  char  old_phone[11];
  char  old_name[20];
  char  old_address[100];
  strcpy(old_phone,p->phone);
  strcpy(old_name,p->name);
  strcpy(old_address,p->address);
  bool flag=true;
  while(flag)
  {
   printf("\n1.PHONE\n");
   printf("2.NAME\n");
   printf("3.ADDRESS\n");
   printf("4.EXIT MODIFY\n");
   printf("PLEASE CHOOSE MODIFY ITEM:");
   int func_num;
   scanf("%d",&func_num);
   switch(func_num)
   {
    case 1:printf("INPUT NEW PHONE:");
        scanf("%s",p->phone);
        printf("MODIFY SUCCESS,PHONE FROM %s TO %s\n",old_phone,p->phone);show_contact(p);break;
    case 2:printf("INPUT NEW NAME:");
        scanf("%s",p->name);
        printf("MODIFY SUCCESS,NAME FROM %s TO %s\n",old_name,p->name);show_contact(p);break;
    case 3:printf("INPUT NEW ADDRESS:");
        scanf("%s",p->address);
        printf("MODIFY SUCCESS,ADDRESS FROM %s TO %s\n",old_address,p->address);show_contact(p);break;
    case 4:show_contact(p);flag=false;break;
    default:printf("INPUT FAIL!\n");break;
   }
  }
 }
 else
 {
  printf("NOT FIND THE CONTACT\n");
 }
}

整体差不多就是这样,各个函数是独立的,在使用makefile编译链接的,上面还有几个函数的代码没给出来。

你可能感兴趣的:(c语言)