通讯者信息查找功能 总是查无此人 为什么 我真想不明白哪里错了
#include"first.h"#include"second.h"
int main()
{
node *head,*p;
for(;;){
switch(menu_select()){
case 1:
printf("通讯录链表的建立\n");
head=CreatList();
break;
case 2:
printf("通讯者信息的插入\n");
p=(node *)malloc(sizeof(node));
printf("输入编号,姓名,性别,电话,地址\n");
scanf("%s,%s,%s,%s,%s",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
InsertNode(head,p);
break;
case 3:
printf("通讯者信息的查询\n");
p=ListFind(head);
if(p!=NULL){
printf("编号 姓名 性别电话 地址分别是:");
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
}else{
printf("没查到要查询的通讯者!\n");
}
break;
case 4:
printf("通讯者信息的修改\n");
ChangeNode(head);
break;
case 5:
printf("通讯者信息的删除\n");
DelNode(head);
break;
case 6:
printf("通讯录链表的输出\n");
printfList(head);
break;
case 0:
printf("再见\n");
return 0;//不可少
}
//break;
}
return 0;
}
#ifndef FIRST_H_INCLUDED
#define FIRST_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct datatype1 datatype;
typedef struct node1 node;
struct datatype1{
char num[5];
char name[9];
char sex[3];
char phone[13];
char addr[31];
};
struct node1{
datatype data;
node *next;
};
int menu_select();
node *CreatList();
void InsertNode(node *l,node *p);
node *ListFind(node *l);
void ChangeNode(node *l);
void printfList(node *l);
#endif // FIRST_H_INCLUDED
#ifndef SECOND_H_INCLUDED
#define SECOND_H_INCLUDED
int menu_select(){
int sn;
printf(" 通讯录管理系统\n");
printf("=======================\n");
printf("1.通讯录链表的建立\n");
printf("2.通讯者信息的插入\n");
printf("3.通讯者信息的查询\n");
printf("4.通讯者信息的修改\n");
printf("5.通讯者信息的删除\n");
printf("6.通讯者信息的输出\n");
printf("0.退出管理系统\n");
printf("=======================\n");
printf(" 请选择0-6\n");
for(;;){
scanf("%d",&sn);
if(sn<0||sn>6)
printf("\n\t输入错误,重选0—6:");
else
break;
}
return sn;
}
node *CreatList(){
node *head,*rear,*s;
char flag='y';
head=(node *)malloc(sizeof(node));
head->next=NULL;
rear=head;
while(flag=='y'||flag=='Y'){
s=(node *)malloc(sizeof(node));
printf("请顺序输入编号,姓名,性别,电话和地址:\n");
scanf("%s,%s,%s,%s,%s\n",s->data.num,s->data.name,s->data.sex,s->data.phone,s->data.addr);
s->next=rear->next;rear->next=s;rear=s;
printf("若想继续输入请按y,否则请按n:");
getchar();
scanf("%c",&flag);
}
return head;
}
void InsertNode(node *l,node *p){
node *s=l->next,*t=s->next;
while(t!=NULL&&(t->data.num<p->data.num)){
s=t;
t=t->next;
}
p->next=s->next;
s->next=p;
}
node *ListFind(node *l){
node *p;
char num[5];
char name[9];
int select;
printf(" 查询方式的选择\n");
printf("======================\n");
printf(" 选择1进行编号查询\n");
printf(" 选择2进行姓名查询\n");
printf("======================\n");
printf("请进行查询方式的选择:");
scanf("%d\n",&select);
switch(select){
case 1:
p=l->next;
printf("请输入要查询的编号:");
scanf("%s",num);
while(p!=NULL&&strcmp(p->data.num,num)<0)
p=p->next;
if(p==NULL||strcmp(p->data.num,num)>0)
p=NULL;
break;
case 2:
p=l->next;
printf("请输入要查询的姓名:");
scanf("%s",name);
while(p!=NULL&&strcmp(p->data.name,name)!=0)
p=p->next;
break;
//default: if(select!=1&&select!=2){
//printf("请输入正确的选择\n");
//return ListFind(l);
}
return p;
}
void ChangeNode(node *l){
node *p=ListFind(l);
if(p!=NULL){
printf("未修改前:");
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
printf("=================================================================================================");
printf("请输入编号,姓名,性别,电话,地址:");
scanf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
}
else{
printf("未找到此人!\n");
}
}
void DelNode(node *l){
node *p,*q;
p=ListFind(l);
if(p==NULL){
printf("无此人,无法删除\n");
}
q=l;
while(q!=NULL&&q->next!=p){
q=q->next;
}
q->next=p->next;
free(p);
}
void printfList(node *l){
node *p=l->next;
while(p!=NULL){
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
p=p->next;
}
}
#endif // SECOND_H_INCLUDED