温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言单链表如何实现通讯录管理系统

发布时间:2021-05-31 14:21:18 来源:亿速云 阅读:275 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关C语言单链表如何实现通讯录管理系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

通讯录管理系统

保存人的信息有: 
名字   name
电话   telephone
性别   sex
年龄   age

用一个结构体来装这些信息:

struct infor{  char name[20];  int age;  char sex[8];  char telephone[16]; };

实现功能有:

增加联系人
删除联系人
修改联系人
寻找联系人
显示联系人

首先建立链表的基本功能创建头链表,创建节点,插入节点

struct addre* Creathead(){                        //创建头链表  struct addre *headnode = (struct addre*)malloc(sizeof(struct addre));  if (headnode == NULL){   printf("malloc error\n");  }  headnode->next = NULL;  return headnode; } struct addre* Creatlist(struct infor *list){      //创建节点  struct addre *node = (struct addre*)malloc(sizeof(struct addre));  if (node == NULL){   printf("malloc error\n");  }  node->next = NULL;  node->people.age = list->age;  strcpy(node->people.name, list->name);  strcpy(node->people.sex, list->sex);  strcpy(node->people.telephone, list->telephone);  return node; } void Addlist(struct addre *headnode,struct infor *list){ //插入节点  struct addre *t = headnode;  while (t->next != NULL){   t = t->next;  }  struct addre *nodelist = Creatlist(list);  t->next = nodelist;  nodelist->next = NULL; }

然后在实现通讯录的功能

void Addpeople(struct addre* node){                     //添加人的信息  struct infor *a=malloc(sizeof(struct infor));       // 创建动态信息结构体指针  if (a == NULL){   printf("malloc error\n");  }  printf("请输入名字\n");  scanf("%s", a->name);  printf("请输入年龄\n");  scanf("%d", &a->age);  printf("请输入性别\n");  scanf("%s", a->sex);  printf("请输入电话号码\n");  scanf("%s", a->telephone);  Addlist(node, a);                        //用尾插法插入该人信息  printf("添加成功!\n"); } void Deletepeople(struct addre *node){                //删除人的信息  char *str = malloc(sizeof(char)* 10);  if (str == NULL){                                 //通过名字寻找   printf("malloc error\n");  }  printf("请输入要删除人的姓名\n");  scanf("%s", str);  struct addre *strat = node;  struct addre *end = node->next;  int flag = 0;                                    //判断是否找到 0为未找到,1 找到  while (end){                    //判断end的  不然会越界   if (strcmp(end->people.name, str) == 0){    flag = 1;    break;   }   node = node->next;                           //到下一个链表   strat = node;                               //一个指向前面 一个指向后面,删除将end删除,前面那个直接指向end的指向   end = node->next;  }  if (flag){   strat->next = end->next;   printf("删除成功\n");   free(end);  }  else{   printf("没找到!\n");  } } void Modifyinfor(struct addre *node){              //修改人的信息  char *str = malloc(sizeof(char)* 10);          //通过名字寻找  if (str == NULL){   printf("malloc error\n");  }  printf("请输入要修改人的姓名\n");  scanf("%s", str);  int flag = 0;  while (node){   if (strcmp(node->people.name, str) == 0){    flag = 1;    break;   }   node = node->next;  }  if (flag){   printf("请重新输入该人信息\n");   printf("请输入名字\n");   scanf("%s", node->people.name);   printf("请输入年龄\n");   scanf("%d", &node->people.age);   printf("请输入性别\n");   scanf("%s", node->people.sex);   printf("请输入电话号码\n");   scanf("%s", node->people.telephone);   printf("修改成功\n");  }  else{   printf("没找到\n");  } } void Foundpeople(struct addre *node){                //找到某人的信息并打印出来  char *str = malloc(sizeof(char)* 10);            //通过名字寻找  if (str == NULL){   printf("malloc error\n");  }  printf("请输入要查找人的姓名\n");  scanf("%s", str);  int flag = 0;  while (node){   if (strcmp(node->people.name, str) == 0){    flag = 1;    break;   }   node = node->next;  }  if (flag){   printf("name\tage\tsex\ttelephone\n");   printf("%s\t", node->people.name);   printf("%d\t", node->people.age);   printf("%s\t", node->people.sex);   printf("%s\t", node->people.telephone);  }  else{   printf("没找到!\n");  } } void Display(struct addre *node){  struct addre *pmove = node->next; //要从头节点的下一个节点显示信息  printf("name\tage\tsex\ttelephone\n");  while (pmove){   printf("%s\t%d\t%s\t%s\n", pmove->people.name, pmove->people.age, pmove->people.sex, pmove->people.telephone);   pmove = pmove->next;  } }

其它代码

菜单:

void Menu(){  printf("+-----------------+\n");  printf("+-1.add   2.delet-+\n");  printf("+-3.modify 4.seek-+\n");  printf("+-5.display6.exit-+\n");  printf("+-----------------+\n");  printf("Please Enter Your Select\n"); }

本人使用的时多文件的方式上面的代码都在函数定义的源文件里实现

main函数的源文件代码:

#include"addressbank.h" /*********************** 信息: 名字   name 年龄   age 电话   telephone 性别   sex 功能:  增加  删除  修改  寻找  打印  显示 ***********************/ int main(){  struct addre* node = Creathead();  int quit = 0;  while (!quit){      Menu();   int select = 0;   scanf("%d", &select);   switch (select){   case 1:    Addpeople(node);    break;   case 2:    Deletepeople(node);    break;   case 3:    Modifyinfor(node);    break;   case 4:    Foundpeople(node);    break;   case 5:    Display(node);    break;   case 6:    quit = 1;    break;   default:    printf("Enter Error!\n");    break;   }  }  system("pause");  return 0; }

声明的头文件:

#ifndef __ADDRESSBANK_H__ #define __ADDRESSBANK_H__ #include<stdio.h> #include<string.h> struct infor{//信息结构体  char name[20];  int age;  char sex[8];  char telephone[16]; }; struct addre{ //链表  struct infor people;  struct addre *next; }; //功能函数 extern void Menu(); extern void Addpeople(struct addre *node); extern void Deletepeople(struct addre *node); extern void Modifyinfor(struct addre *node); extern void Foundpeople(struct addre *node); extern void Display(struct addre *node); //下面未链表函数 extern struct addre* Creatlist(struct infor *list); extern struct addre* Creathead(); extern void Addlist(struct addre *headnode, struct infor *list); #endif

关于“C语言单链表如何实现通讯录管理系统”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI