|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<winuser.inl>
- #include<stdio.h>
- #include <cstdlib>
- #include<string.h>
- void tail_add(struct People** head);
- struct People
- {
- char name[20];
- char phone[20];
- struct People *next;
- };
- void getInput(struct People* people,int wether_change_phone = 0)
- {
- printf("enter name:");
- scanf_s("%s", people->name, 20);
- if (wether_change_phone == 0)
- {
- printf("enter phone:");
- scanf_s("%s", people->phone, 20);
- }
- else
- {
- printf("enter new phone:");
- scanf_s("%s", people->phone, 20);
- }
- }
- void printfInfo(struct People *head)
- {
- struct People* people;
- people = head;
- printf("name: %s \n", people->name);
- printf("phone:%s \n", people->phone);
- }
- void tail_add(struct People** head)
- {
- struct People* people,*temp;
- people = (struct People*)(malloc(sizeof(struct People))); //动态申请结构体
- if (people == NULL)
- {
- printf("memory failed");
- exit(1);
- }
- getInput(people,0);
- //找尾节点
- if (*head == NULL)
- {
- *head = people;
- people -> next = NULL;
- }
- else
- {
- temp = *head;
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = people;
- people -> next = NULL;
- }
- }
- void find(struct People** head,char find_name[])
- {
- struct People * people;
- people = *head;
- //判断书否为空链表,为空,直接结束
- if (people == NULL)
- {
- printf("no contact people!!!!\n");
- }
- else
- {
- while (people != NULL)
- {
- if (!strcmp( people->name, find_name))
- {
- printfInfo(people);
- break;
- }
- else
- {
- people = people->next;
- }
- }
- }
- //while循环查找是否有符合条件的人或者到达链表的末端,跳出循环,
- //找到相应的人,打印信息
- }
- void releaseMemory(struct People** head)
- {
- struct People* people, * temp;
- people = *head;
- while (people->next != NULL)
- {
- temp = people;
- people = people->next;
- free(temp);
- }
- }
- void chang_info(struct People** head, char find_name[])
- {
- struct People* people;
- people = *head;
- //遍历链表,找到姓名相同的联系人,修改其信息
- if (people == NULL)
- {
- printf("no contact people!!!!\n");
- }
- else
- {
- while (people != NULL)
- {
- if (!strcmp(people->name, find_name))
- {
- getInput(people, 1);
- break;
- }
- else
- {
- people = people->next;
- }
- }
- }
- }
- void delete_info(struct People** head, char delete_name[])
- {
- struct People* previous, * current;
- //直接给previous赋值null,current赋值头节点,后面就不需要再用中间变量来存储之后,再进行交换地址了
- //current此时就是一个空的结构体,头指针给他之后,他指向了链表的头节点
- current = *head;
- previous = NULL;
- //找到删除值的位置的前后节点的位置
- while (current != NULL && current->name != delete_name)
- {
- previous = current;
- current = current->next;
- }
- //改变链表节点的指向,删除节点
- if (previous == NULL) //空链表,直接将头指针指向他
- {
- printf("not found anything\n");
- }
- else
- {
- if (previous == NULL) //链表只有一个节点
- {
- *head = current->next;
- }
- else
- {
- previous->next = current->next;//前面用的是current 来判断有没有找到要删除的节点的位置,所以删除就是currennt的节点指向下一个
- }
- free(current);
- }
- }
- void show_all(struct People** head)
- {
- struct People* people;
- people = *head;
- printf("all people in the phonebook is:\n");
- while (people != NULL)
- {
- printfInfo(people);
- people = people->next;
- }
- printf("\n");
-
- }
- int main()
- {
- printf("---欢迎使用通讯录管理程序--\n");
- printf("---1:插入新的联系人-----\n");
- printf("---2:查找已有的联系人---\n");
- printf("---3更改已有的联系人----\n");
- printf("---4:删除已有的联系人---\n");
- printf("---5:显示当前通讯录----\n");
- printf("---6:显退出当前通讯录---\n");
- printf("\n");
- int order;
- char name[20];
- struct People *phonebook = NULL;
- do
- {
- printf("请输入指令:\n");
- scanf_s("%d", &order);
- switch (order)
- {
- case 1:
- tail_add(&phonebook);
- break;
- case 2:
- printf("enter the name you want to find:\n");
- scanf_s("%s",name,20);
- find(&phonebook,name);
- break;
- case 3:
- printf("enter the name you want to change:\n");
- scanf_s("%s", name, 20);
- chang_info(&phonebook, name);
- break;
- case 4:
- printf("enter the name you want to delete:\n");
- scanf_s("%s", name, 20);
- delete_info(&phonebook, name);
- break;
- case 5:
- show_all(&phonebook);
- break;
- case 6:
- printf("退出界面!!1");
- exit(1);
- default:
- printf("wrong enter !!!!!!");
- break;
- }
- } while (order != 6);
- }
复制代码
本帖最后由 jackz007 于 2021-1-9 16:13 编辑
这一句错误
- while (current != NULL && current->name != delete_name)
复制代码
比较字符串应该通过函数,不能像数值那样直接比。
- void delete_info(struct People ** head , char delete_name[])
- {
- struct People * previous , * current ;
- printf("\n") ;
- if(* head) {
- for(current = previous = * head ; current ; previous = current , current = previous -> next) {
- if(! strcmp(current -> name , delete_name)) { // 字符串比较
- if(current == previous) * head = current -> next ; // 在头节点找到
- else previous -> next = current -> next ; // 在其它节点找到
- free(current) ; // 释放内存
- break ; // 退出循环
- }
- }
- if(current) printf("节点已删除") ; // 如果找到节点,循环中途退出,current 一定不是 NULL
- else printf("没有找到节点") ; // 否则,必然是正常到达循环终点,current 一定是 NULL
- } else {
- printf("链表为空\n") ;
- }
- printf("\n") ;
- }
复制代码
此外,在 main() 中,为什么不把菜单显示纳入循环?如果操作多了,每次输入指令的时候,都会看不到菜单,还得靠 "盲人摸象"?
|
|