|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct list
- {
- char name[20];
- char phone[20];
- struct list* next;
- }*head = NULL;
- void addPerson(struct list** head);
- void getInput(struct list* person);
- void findPerson(struct list* head);
- void changePerson(struct list* head);
- void delPerson(struct list* head);
- void displayContacts(struct list* head);
- void releasePerson(struct list** head);
- void addPerson(struct list** head)
- {
- struct list* person, * temp;
- person = (struct list*)malloc(sizeof(struct list));
- if (person == NULL)
- {
- printf("分配内存失败。\n");
- exit(1);
- }
- getInput(person);
- if (*head!=NULL)
- {
- temp = *head;
- *head = person;
- person->next = NULL;
- }
- else
- {
- *head = person;
- person->next = NULL;
- }
- }
- void getInput(struct list* person)
- {
- printf("请输入联系人姓名:\n");
- scanf("%s", &person->name);
- printf("请输入联系人电话。\n");
- scanf("%s", &person->phone);
- }
- void findPerson(struct list* head)
- {
- char ch[25];
- printf("请输入联系人姓名:\n");
- scanf("%s", ch);
- while (strcmp(head->name,ch)&&head!=NULL)
- {
- head = head->next;
- }
- if (head==NULL)
- {
- printf("没有此联系人\n");
- }
- else
- {
- printf("姓名:%s", head->name);
- printf("电话号码:%s", head->phone);
- }
- }
- void changePerson(struct list*head)
- {
- char ch[25];
- int num = 1;
- printf("请输入联系人姓名:\n");
- scanf("%s", ch);
- while (strcmp(head->name, ch) && head != NULL)
- {
- head = head->next;
- }
- if (head == NULL)
- {
- printf("没有此联系人\n");
- }
- else
- {
- while (num)
- {
- printf("1.改写名字 2.改写姓名 0.退出\n");
- scanf("%d", &num);
- switch (num)
- {
- case 0:
- printf("退出\n");
- break;
- case 1:
- printf("请输入姓名。\n");
- scanf("%s", &head->name);
- break;
- case 2:
- printf("请输入电话。\n");
- scanf("%s", head->phone);
- break;
- default:
- printf("输入错误");
- break;
- }
- }
- }
- }
- void delPerson(struct list* head)
- {
- char ch[20];
- printf("请输入姓名。\n");
- scanf("%s", &ch);
- struct list* previous, * current;
- current = head;
- while (strcmp(current->name,ch)&¤t!=NULL)
- {
- previous = current;
- current = current->next;
- }
- if (current == NULL)
- {
- printf("没有此联系人\n");
- }
- else
- {
- previous->next = current->next;
- free(current);
- }
- }
- void displayContacts(struct list* head)
- {
- while (head!=NULL)
- {
- printf("姓名:%s 电话:%s\n",head->name,head->phone);
- head = head->next;
- }
- }
- void releasePerson(struct list** head)
- {
- struct list* temp;
- while (*head!=NULL)
- {
- temp = *head;
- *head = (*head)->next;
- free(temp);
- }
- }
- int main(void)
- {
- int num = 1;
- while (num)
- {
- printf("请输入(1-6):\n");
- printf("请输入命令:\n.....||1(添加新的联系人) ||.....\n.....||2:(查找联系人) ||.....\n.....||3:(更改联系人) ||.....\n.....||4:(删除联系人) ||.....\n.....||5:(查看所有联系人)||.....\n.....||6:(退出) ||.....");
- scanf("%d", &num);
- switch (num)
- {
- case 1:
- addPerson(&head);
- break;
- case 2:
- findPerson(head);
- break;
- case 3:
- changePerson(head);
- break;
- case 4:
- delPerson(head);
- break;
- case 5:
- displayContacts(head);
- break;
- case 6:
- printf("提出系统.\n");
- releasePerson(&head);
- break;
- default:
- break;
- }
- }
- return 0;
- }
复制代码 |
|