1 /****************************/ 2 /*主控菜单处理测试程序main.c*/ 3 /****************************/ 4 #include<stdio.h> 5 #include<string.h> 6 #include<malloc.h> 7 #include<stdlib.h> 8 typedef struct{ //通讯录结点类型 9 char num[5]; //编号 10 char name[9]; //姓名 11 char sex[3]; //性别 12 char phone[13]; //电话 13 char addr[31]; //地址 14 }DataType; 15 16 typedef struct node{ //结点类型 17 DataType data; //结点数据域 18 struct node *next; //结点指针域 19 }ListNode; 20 21 typedef ListNode *LinkList; 22 LinkList head; 23 ListNode *p; 24 //函数说明 25 26 int menu_select(); 27 LinkList CreateList(void); 28 void InsertNode(LinkList head,ListNode *p); 29 ListNode *ListFind(LinkList head); 30 void DelNode(LinkList head); 31 void PrintList(LinkList head); 32 //主函数 33 void main () 34 { 35 for (;;){ 36 switch(menu_select()) 37 { 38 case 1: 39 printf("************************************** "); 40 printf("* 通 讯 录 链 表 的 建 立 * "); 41 printf("************************************** "); 42 head=CreateList(); 43 break; 44 case 2: 45 printf("************************************** "); 46 printf("* 通 讯 者 信 息 的 添 加 * "); 47 printf("************************************** "); 48 printf("编号(4) 姓名(8) 性别 电话(11) 地址(31) "); 49 printf("************************************** "); 50 p=(ListNode *)malloc(sizeof(ListNode)); //申请新结点 51 scanf("%s%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr); 52 InsertNode(head,p); 53 break; 54 case 3: 55 printf("*************************************** "); 56 printf("* 通 讯 录 信 息 的 查 询 * "); 57 printf("*************************************** "); 58 p=ListFind(head); 59 if(p!=NULL){ 60 printf("编 号 姓 名 性 别 联系电话 地 址 "); 61 printf("*************************************** "); 62 printf("%s,%s,%s,%s,%s ",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr); 63 printf("------------------------------------------"); 64 } 65 else 66 printf("没查到要查询的通讯者"); 67 break; 68 case 4: 69 printf("*************************************** "); 70 printf("* 通 讯 录 信 息 的 删 除 * "); 71 printf("*************************************** "); 72 DelNode(head); //删除结点 73 break; 74 case 5: 75 printf("*************************************** "); 76 printf("* 通 讯 录 链 表 的 输 出 * "); 77 printf("*************************************** "); 78 PrintList(head); 79 break; 80 case 0: 81 printf(" 再见! "); 82 return; 83 } 84 } 85 } 86 87 /*************************/ 88 /* 菜单选择函数程序*/ 89 /*************************/ 90 int menu_select() 91 { 92 int sn; 93 printf(" 通讯录管理系统 "); 94 printf("===================== "); 95 printf(" 1.通讯录链表的建立 "); 96 printf(" 2.通讯录链表的插入 "); 97 printf(" 3.通讯录链表的查询 "); 98 printf(" 4.通讯录链表的删除 "); 99 printf(" 5.通讯录链表的输出 "); 100 printf(" 0.退出管理系统 "); 101 printf("===================== "); 102 printf(" 请 选 择 0-5: "); 103 for(;;) 104 {scanf("%d",&sn); 105 if(sn<0||sn>5) 106 printf(" 输入错误,重选0-5: "); 107 else 108 break; 109 } 110 return sn; 111 } 112 113 /**********************************/ 114 /*用尾插法建立的通讯录链表函数*/ 115 /********************************/ 116 117 118 LinkList CreateList(void) 119 { //尾插法建立带头结点的通讯录链表算法 120 LinkList head=(ListNode *)malloc(sizeof(ListNode)); //申请头结点 121 ListNode *p,*rear; 122 int flag=0; //结束标志置0 123 rear=head; //尾指针初始指向头结点 124 while(flag==0) 125 {p=(ListNode *)malloc(sizeof(ListNode)); //申请新结点 126 printf("编号(4) 姓名(8) 性别 电话(11) 地址(31) "); 127 printf("---------------------------------- "); 128 scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr); 129 rear->next=p; //新结点连接到尾结点之后 130 rear=p; //尾指针指向新结点 131 printf("结束建表吗?(1/0):"); 132 scanf("%d",&flag); //读入一个标志数据 133 } 134 rear->next=NULL; //终端结点指针域置空 135 return head; //返回链表头指针 136 } 137 138 /*******************************/ 139 /*在通讯录链表head中插入结点*/ 140 /*******************************/ 141 142 void InsertNode(LinkList head,ListNode *p) 143 {ListNode *p1,*p2; 144 p1=head; 145 p2=p1->next; 146 while(p2!=NULL && strcmp(p2->data.num,p->data.num)<0) 147 {p1=p2; //p1指向刚访问过的结点 148 p2=p2->next; //p2指向表的下一个结点 149 } 150 p1->next=p; //插入p所指向的结点 151 p->next=p2; //连接表中剩余部分 152 } 153 154 155 /************************************/ 156 /* 有序通讯录链表上的查找*/ 157 /*********************************/ 158 159 ListNode *ListFind(LinkList head) 160 { //有序通讯录链表上的查找 161 ListNode *p; 162 char num[5]; 163 char name[9]; 164 int xz; 165 printf("===================== "); 166 printf(" 1.按编号查询 "); 167 printf(" 2.按姓名查询 "); 168 printf("===================== "); 169 printf(" 请选择: "); 170 p=head->next; //假定通讯录表带头结点 171 scanf("%d",&xz); 172 if(xz==1){ 173 printf("请输入要查找者的编号: "); 174 scanf("%s",&num); 175 getchar(); 176 while(p&&strcmp(p->data.num,num)<0) 177 p=p->next; 178 if(p==NULL||strcmp(p->data.num,num)>0) 179 p=NULL; 180 } 181 else if(xz==2) 182 { printf("请输入要查找者的姓名: "); 183 scanf("%s",&name); 184 while(p&&strcmp(p->data.name,name)!=0) 185 p=p->next; 186 } 187 return p; 188 } 189 190 191 192 193 194 void DelNode(LinkList head) 195 {char jx; 196 ListNode *p,*q; 197 p=ListFind(head); 198 if(p==NULL) 199 {printf("没有查到要查询的通讯者 ! "); 200 return; 201 } 202 printf("真的要删除该结点吗?: "); 203 scanf("%c",&jx); 204 if(jx=='y'||jx=='Y') 205 {q=head; 206 while(q!=NULL&&q->next!=p) 207 q=q->next; 208 if(q!=NULL) 209 q->next=p->next; 210 free(p); 211 printf("通讯者已被删除! "); 212 } 213 } 214 215 216 217 218 219 void PrintList(LinkList head) 220 {p=head->next; 221 printf("编 号 姓 名 性 别 联系电话 地 址 "); 222 printf("--------------------------------------------------- "); 223 while(p!=NULL) 224 { printf("%s,%s,%s,%s,%s ",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr); 225 printf("--------------------------------------------------- "); 226 p=p->next; 227 } 228 }