当前位置:文档之家› 数据结构 链表的应用 通讯录管理

数据结构 链表的应用 通讯录管理

数据结构链表的应用通讯录管理实验要求:一、设计一个含有多个菜单项的程序,菜单项内容如下:1通讯录链表的建立2通讯录链表的插入3通讯录链表的查询4通讯录链表的删除5通讯录链表的输出0退出管理系统要求只能用0-5来选择菜单项,其他的输入无效二、分别实现多个菜单项的功能,要求每个功能在操作时都有确认提示三、编写一个程序实现整个通讯录管理系统,把主菜单的生成和各菜单项功能的实现作为子程序或者函数或者过程来实现。

四、要求关键语句必须要写注释。

#include"stdio.h"#include"string.h"#include"stdlib.h"typedefstructabc{//通讯录结点类型charnum[5];//编号charname[9];//姓名charsex[3];//性别charphone[13];//电话charaddr[31];//地址}DataType;typedefstructnode{//结点类型定义DataTypedata;//结点数据域structnode*next;//结点指针域}ListNode;typedefListNode*LinkList; LinkListhead;ListNode*p;//函数说明intmenu_select();LinkListCreateList(void); voidInsertNode(LinkListhead,ListNode*p); ListNode*ListFind(LinkListhead); voidDelNode(LinkListhead); voidPrintList(LinkListhead);//主函数voidmain(){for(;;){switch(menu_select()){case1:printf("**********************************\n"); printf("*通讯录链表的建立*\n");printf("**********************************\n"); head=CreateList();break;case2:printf("*通讯者信息的添加*\n");printf("**********************************\n");printf("编号(4)姓名(8)性别(3)电话(11)地址(31)\n");printf("*************************************\n");p=(ListNode*)malloc(sizeof(ListNode));//申请新结点scanf("%s%s%s%s%s",p->data.num,p->,p->data.sex, p->data.phone,p->data.addr);InsertNode(head,p);break;case3:printf("***********************************\n");printf("*通讯录信息的查询*\n");printf("***********************************\n");p=ListFind(head);if(p!=NULL){printf("编号姓名性别联系电话地址\n");printf("--------------------------------------------------\n"); printf("%s,%s,%s,%s,%s\n",p->data.num,p->,p->data.sex,p->data.phone,p->data.addr);printf("---------------------------------------------------\n");}elseprintf("没有查到要查询的通讯者!\n");break;case4:printf("*通讯录信息的删除*\n");printf("***********************************\n"); DelNode(head);//删除结点break;case5:printf("************************************\n"); printf("*通讯录链表的输出*\n");printf("************************************\n"); PrintList(head);break;case0:printf("\t再见!\n");return;}}}intmenu_select(){intsn;printf("通讯录管理系统\n");printf("===================\n"); printf("1.通讯链表的建立\n");printf("2.通讯者结点的插入\n");printf("3.通讯者结点的查询\n");printf("4.通讯者结点的删除\n");printf("5.通讯录链表的输出\n");printf("0.退出管理系统\n");printf("==========================\n");printf("请选择0-5:");for(;;){scanf("%d",&sn);if(sn<0||sn>5)printf("\n\t输入错误,重选0-5:");elsebreak;}returnsn;}LinkListCreateList(void){//尾插法建立带头结点的通讯录链表算法LinkListhead=(ListNode*)malloc(sizeof(ListNode));//申请头结点ListNode*p,*rear;intflag=0;//结束标志置0rear=head;//尾指针初始指向头结点while(flag==0){p=(ListNode*)malloc(sizeof(ListNode));//申新结点printf("编号(4)姓名(8)性别电话(11)地址(31)\n");printf("--------------------------------------------------------------------------------------\n");scanf("%s%s%s%s%s",p->data.num,p->,p->data.sex,p->data.phone, p->data.addr);rear->next=p;//新结点连接到尾结点之后rear=p;//尾指针指向新结点printf("结束建表吗?(1/0):");scanf("%d",&flag);}rear->next=NULL;//终端结点指针置空returnhead;//返回链表头指针}voidInsertNode(LinkListhead,ListNode*p){ListNode*p1,*p2;p1=head;p2=p1->next;while(p2!=NULL&&strcmp(p2->data.num,p->data.num)<0) {p1=p2;//p1指向刚访问过的结点p2=p2->next;//p2指向表的下一个结点}p1->next=p;//插入p所指向的结点p->next=p2;//连接表中剩余的结点}ListNode*ListFind(LinkListhead){//有序通讯录链表上的查找ListNode*p;charnum[5];charname[9];intxz;printf("==================\n"); printf("1.按编号查询\n");printf("2.按姓名查询\n");printf("==================\n"); printf("请选择:");p=head->next;//假定通讯录表带头结点scanf("%d",&xz);if(xz==1){printf("请输入要查找者的编号:");scanf("%s",num);while(p&&strcmp(p->data.num,num)<0)p=p->next;if((p==NULL)||strcmp(p->data.num,num)>0) p=NULL;//没有查到要查找的通讯}elseif(xz==2){printf("请输入要查找者的姓名:");scanf("%s",name);while(p&&strcmp(p->,name)!=0) p=p->next;}returnp;}voidDelNode(LinkListhead){charjx;ListNode*p,*q;p=ListFind(head);//调用查找函数if(p==NULL){printf("没有查到要删除的通讯者!\n");return;}printf("真的要删除该结点吗?(y/n):");scanf("%c",&jx);if(jx=='y'||jx=='Y'){q=head;while((q!=NULL)&&(q->next!=p))q=q->next;q->next=p->next;//删除结点free(p);//释放被删结点空间printf("通讯者已被删除!\n");}}voidPrintList(LinkListhead){ListNode*p;p=head->next;printf("编号姓名性别联系电话地址\n");printf("--------------------------------------------------------------------------------\n" );while(p!=NULL){printf("%s,%s,%s,%s,%s\n",p->data.num,p->,p->data.sex,p->data.phone,p->data.addr);printf("---------------------------------------------------------------------------------\n ");p=p->next;//后移一个结点}}。

相关主题