数据结构上机制作通讯录

weixin_46112546 2020-01-07 09:17:20
求解程序的问题并修改,孩实在改不过来了 #include<stdio.h> #include<stdlib.h> #include<string.h> struct address { //定义结构 char name[10]; char street[50]; char city[10]; char nation[15]; char tel[7]; struct address*next; //后继指针 struct address*prior; //前驱指针 } struct address*start; //首结点 struct address*last; //尾节点 struct address*find(char*);//声明查找函数 void enter(); //函数声明 void search(); void save(); void load(); void list(); void ddelete(struct address**start,struct address**last); void insert(struct address*i,struct address**start,struct address**last); void inputs(char*,char*,int); void display(struct address*); int menu_select(void); void main() { start=last=NULL; for(;;) { switch(menu_select()) { case1:enter(); continue; case2:ddelete(&start,&last); continue; case3:list(); continue; case4:search(); continue; case5:save(); continue; case6:load(); continue; case7:exit(0); } } } int menu_select(void) //主目录 { char s[80]; int c; printf("********^欢迎使用DOS通讯录系统^*********\n"); printf("********请在其他操作之前先导入**********\n"); printf("****************************************\n"); printf("*********1输入信息**********\n"); printf("*********2删除信息**********\n"); printf("*********3显示信息**********\n"); printf("*********4查找**************\n"); printf("*********5存盘**************\n"); printf("*********6导入************\n"); printf("*********7退出************\n"); printf("*****************************\n"); do{ printf("\n Please enter you choice:\n"); gets(s); c=atoi(s); }while(c<0||c>7); return c; //返回输入值 } void enter() //输入函数,本函数循环输入资料,当输入姓名为空时退出 { struct address*info; //定义当前结点 for(;;) { info=(struct address*)malloc(sizeof(struct adress)); //为当前结点分配空间 if(!info) { printf("\n Out of memory"); exit(0); //如果分配空间失败,退出程序 } printf("输入空姓名结束:\n"); inputs("请输入 姓名:",info->name,10); if(!info->name[0])break; //如果输入姓名为空,结束循环 inputs("请输入 街道:",info->street,50); inputs("请输入 城市:",info->city,15); inputs("请输入 民族:",info->nation,15); inputs("请输入 电话:",info->tel,7); insert(info,&start,&last); //调用结点插入函数 } } void inputs(char*prompt,char*s,int count) //输入函数,有越界检测功能 { char p[255]; do {print(prompt); fgets(p,254,stdin); if(strlen(p)>count) printf("\nToo Long\n"); } while(strlen(p)>count); p[strlen(p)-1]=0; strcpy(s,p); } void insert( struct address*i, struct address**start, struct address**last ) //数据插入函数 { if(*last==NULL) //如果尾节点为空,意味着当前链表为空 { i->next=NULL; i->prior=NULL; *last=i; *start=i; return; } else { (*last)->next=i; i->prior=*last; i->next=NULL; *last=(*last)->next; } } void ddelete(struct address**start,struct address**last) //删除函数 { struct address *info; char s[80]; inputs("请输入 姓名;",s,10); //输入欲删除结点name域内容 info=find(s); //查找该内容 if(info) //如果找到 { printf("Deleting……\n"); if(*start==info) //如果该结点为首结点,把该结点的下驱作为作为新的首结点(入口) { *start=info->next; if(*start) (*start)->prior=NULL; else *last=NULL; } else //如果欲删除的结点不是首结点 { info->prior->next=info->next; /*令该结点的前驱的next指针指向该结点的后驱,又 令该结点的后驱prior指点指向该结点的前驱*/ if(info!=*last) //如果该节点是尾结点,则令该节点的前驱为尾结点 info->next->prior=infor->prior; else *last=info->prior; } free(info); //释放该结点所占用的内存 printf("-OK,删除成功!\n"); } } struct address *find(char *name) //查找函数,形参为欲查找结点的name域 { struct address *info; info=start; while(info) { if(!strcmp(name,info->name)) return info; info=info->next; } printf("未找到相关信息,\n"); return NULL; } //输出整个链表 void list(void) { struct address*info; info=start; if(info ==NULL) printf("当前记录为空!"); else printf("姓名\t街道\t\t城市\t民族\t电话\t\n"); while(info) { display(info); if(info->next==NULL){break;}info=info->next; } printf("\n\n"); } void display(struct address*info) //输出传入结点函数 { printf("%s\t",info->name); printf("%s\t",info->street); printf("%s\t",info->city); printf("%s\t",info->nation); printf("%s\t",info->tel); printf("\n"); } void search(void) //查找函数 { char name[40]; struct address *info; printf("输入要查找的姓名:"); //输入欲查找的姓名 gets(name); info=find(name); if(!info) printf("姓名不存在\n"); //如果没找到,显示Not found else display(info); //如果找到,显示该结点资料 } void save(void) //保存函数 { struct address*info; FILE*fp; fp=fopen("record,txt","wb"); //生成文件 if(!fp) { printf("Cannot open file.\n"); return; } printf("\nSaveing……\n"); info=start; while(info) //把链表写入文件 { fwrite(info,sizeof(struct address),1,fp); info=info->next; } printf("-ok!\n"); fclose(fp); //链表全部写入文件后,关闭文件 } void load() //调用预存文件函数 { register int t, size; struct address *info,*temp=0; char*p; FILE*fp; //打开文件 if((fp=fopen("record.txt", "r"))==NULL) { printf("Cannot open file!\n"); return; } printf("\n\nLoading...\n"); //调用文件 size=sizeof(struct address); //为结点分配内存 start=(struct address*)malloc(size); if(!start) //如果读取失败,返回 { printf("Out of memory!\n"); exit(0); } info=start; p=(char*)info; while((*p++=getc(fp))!=EOF) { for(t=0;t;<size-1;++t) *p++=getc(fp); info->next=(struct address*)malloc(size); if(!info->next) { printf("Out of memory!\n"); return; } info->prior=temp; temp=info; info=info->next; p=(char*)info; } temp->next=0; last=temp; start->prior=0; fclose(fp); printf("-OK!\n"); }
...全文
178 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
huashoutinghou 2020-02-21
  • 打赏
  • 举报
回复
没看明白你的问题是啥啊?

6,108

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 数据库报表
社区管理员
  • 数据库报表社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧