椎名真白mash 侧头木235655
关注数: 50 粉丝数: 102 发帖数: 4,681 关注贴吧数: 135
自己想用链表做一个通讯录管理系统,遇到了一些问题,求助 代码如下,只做到一半,其他东西目前没发现问题,del函数和find函数调用时不时会出现问题,楼下详细补充 #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct contact { char* name; long num; struct contact* next; } Contact; void addfast(Contact* head){ system("cls"); int flag = 1; int i = 1; char u[30] = ""; Contact *p1, *p2; while (i!=0) { p1 = malloc(sizeof(Contact)); puts("请输入联系人姓名:"); scanf("%s", u); p1->name = strdup(u); printf("请输入联系人号码:"); scanf("%d", &p1->num); if (flag == 1) { head->next = p1; } else { p2->next = p1; } p2 = p1; flag++; puts("还要继续添加吗 输入0退出,输入其他整数继续"); scanf("%d", &i); } p2->next = NULL; } void putall(Contact* head) { while (head->next != NULL) { head = head->next; printf("姓名:%s\t 号码:%d\n", head->name, head->num); } } void insert(int n, Contact* head) { char n1[30] = ""; Contact* newnode; newnode = malloc(sizeof(Contact)); for (int i = 1; i < n ; i++) { head = head->next; if (head == NULL) { printf("位置错误"); return; } } puts("请输入联系人姓名:"); scanf("%s", n1); newnode->name = strdup(n1); puts("请输入联系人号码:"); scanf("%d", &newnode->num); newnode->next = head->next; head->next = newnode; } void del(Contact* head,char *lk) { Contact* mid; int f = 0; while (head->next != NULL) { if (strcmp(lk, head->next->name) == 0) { mid = head->next; f++; head->next = head->next->next; free(mid); } head = head->next; }if(f==0)printf("未查找到你要删除的联系人"); } void find(Contact* head, char* a) { while (head!=NULL && head->next != NULL) { if (strcmp(head->next->name, a) == 0) { printf("该联系人的号码是%d", head->next->num); return; } head = head->next; }puts("未查找到该联系人\n"); } int main() { int a = 0; Contact* head; head = (Contact*)malloc(sizeof(Contact)); if (head == NULL)printf("qa"); head->next = NULL; while (1) { printf(" 欢迎使用huahua的通讯录管理系统\n"); printf("\t请输入数字以进行功能选择:\n"); puts("\t1-批量初始化添加联系人"); puts("\t2-指定位置添加联系人"); puts("\t3-删除指定联系人"); puts("\t4-查找指定联系人"); puts("\t5-显示所有联系人"); puts("\t6-修改指定联系人"); puts("\t7-清空联系人"); puts("\t0-退出系统"); scanf("%d", &a); switch (a) { case 1: addfast(head); system("cls"); break; case 2:printf("你希望新联系人在第几个位置:"); int b = 0; scanf("%d",&b); insert(b, head); break; case 3:puts("你要删除的联系人是:");char lk[30]; scanf("%s", lk); del(head,lk); break; case 4:puts("你要查找的联系人是:"); char xx[30]; scanf("%s", xx); find(head, xx); break; case 5:putall(head); break; case 0:return 0; } } return 0; }
1 下一页