- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef struct{//定义链表数据类型
- char num[5]; //编号
- char name[8]; //姓名
- char sex[10]; //性别
- char tel[11]; //电话
- char address[100]; //地址
- }datatype;
- typedef struct node{//储存结构为链表
- datatype data;
- struct node * next;
- }listnode;
- typedef listnode * linklist;
- int menu_select( );//选择菜单
- linklist createlist(void);//创建链表
- void insertnode(linklist head,listnode *p);//添加数据
- listnode *listfind(linklist head);//数据查找返回找到的节点指针
- void delnode(linklist head);//数据删除
- void printlist(linklist head);//数据输出
- void main()
- {
- linklist head;//接受返回的链表
- listnode *p;//接受返回的节点
- for( ; ; ){
- switch(menu_select() )
- {
- case 1:
- printf(" ***********************************************\n");
- printf(" ************** 通讯录链表的建立 ***************\n");
- printf(" ***********************************************\n");
- head=createlist();
- break;
- case 2:
- printf(" ***********************************************\n");
- printf(" ************** 通讯录信息添加 *****************\n");
- printf(" ***********************************************\n");
- printf(" ** 编号(4) 姓名(8) 性别(10) 电话(11) 地址(30)**\n");
- printf(" ***********************************************\n");
- p=(listnode *)malloc(sizeof(listnode));
- scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.tel,p->data.address);
- insertnode(head,p);
- break;
- case 3:
- 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->data.name,p->data.sex,p->data.tel,p->data.address);
- printf(" ------------------------------\n");
- }
- else
- printf("没有你要找的人 ! \n");
- break;
- case 4:
- printf(" ***********************************\n");
- printf(" **********通讯信息的删除***********\n");
- printf(" ***********************************\n");
- delnode(head);
- break;
- case 5:
- printf(" ***********************************\n");
- printf(" **********通讯录链表的输出*********\n");
- printf(" ***********************************\n");
- printlist(head);
- break;
- case 0:
- printf("\t 谢谢您的使用! \n");
- return;
- }
- }
- }
- int menu_select()
- {
- int sn;
- printf(" \n");
- printf(" \n");
- printf(" \n");
- printf(" 通讯录管理系统 \n");
- 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 \n\n");
- for(; ;)
- {
- scanf("%d",&sn);
- if(sn<0||sn>5)
- printf("\t输入错误\n");
- else
- break;
- }
- return sn;
- }
- linklist createlist(void)
- {
- linklist head=(listnode *)malloc(sizeof(listnode));//建立头节点
- listnode *p,*rear;//p为要创建的节点指针,rear为在链表上移动的指针
- int flag=1;
- rear=head;
- while(flag==1)
- {
- p=(listnode *)malloc(sizeof(listnode));
- printf(" 编号(5) 姓名(8) 性别(10) 电话(11) 地址(100) \n");
- printf(" --------------------------------------------\n");
- scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex ,p->data.tel,p->data.address);//数据输入
- rear->next=p;
- rear=p;
- printf(" 继续建表吗? (1.是/0.不):");
- scanf("%d",&flag);
- }
- if(flag==1)
- printf("创建成功!\n");
- rear->next=NULL;
- return head;
- }
- void insertnode(linklist head,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=p2->next;
- }
- p1->next=p;
- p->next=p2;
- printf("插入成功!\n");
- }
- listnode * listfind(linklist head)
- {
- listnode * p;
- char num[5];
- char name[8];
- int xz;
- printf("--------------------\n");
- printf(" 1.编号查询\n");
- printf(" 2.姓名查询\n");
- printf("--------------------\n");
- printf("请选择: \n");
- 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;
- }
- else
- if(xz=2){
- printf("请输入要查询者的姓名: ");
- scanf("%s",name);
- while(p && strcmp(p->data.name,name)!=0)
- p=p->next;
- }
- return p;
- }
- void delnode(linklist head)
- {
- int jx;
- listnode *p,*q;
- p=listfind(head);
- if(p==NULL){
- printf("没有要删除的通讯者!:\n");
- return;
- }
- printf ("真的要删除该结点吗?(1.是/0.不):");
- scanf("%d",&jx);
- if (jx==1){
- q=head;
- while(q!=NULL && q->next!=p)
- q=q->next;
- q->next=p->next;
- free(p);
- printf("通讯者已被删除!\n");
- }
- }
- void printlist(linklist head)
- {
- listnode * p;
- p=head->next;
- printf(" 编号 姓名 性别 电话 地址\n");
- printf(" -----------------------------\n");
- while(p!=NULL)
- {
- printf(" %s, %s, %s, %s, %s\n",p->data.num,p->data.name,p->data.sex,p->data.tel,p->data.address);
- printf(" ------------------------------\n");
- p=p->next;
- }
- }
复制代码 |