求修改C语言程序 题目要求 利用链表写一个简单的通讯录,开始输入人名字、电话。 能够进行查找 就行了。
发布网友
发布时间:2022-05-13 15:38
我来回答
共1个回答
热心网友
时间:2023-10-14 18:31
//帮你改了,可以查找第一条记录,你那个查找最还也弄个循环,遍历所有的记录
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct TXL)
struct TXL
{
char name;
int num;
struct TXL*next;
};
int n;char a ;
struct TXL*creat()
{
struct TXL*head;
struct TXL*p1,*p2;
n=0;
p1=p2=(struct TXL*)malloc(LEN);
scanf("%c,%d",&p1->name,&p1->num);
head=NULL;
while (p1->name!='0')
{n=n+1;
if(n==1){head=p1;head->next=NULL;}
else
{ p2->next=p1;
p2=p1;}
p1=(struct TXL*)malloc(LEN);
scanf("%c,%d",&p1->name,&p1->num);}
p1->next=NULL;
return(head);
}
void find(struct TXL* head)
{struct TXL*p;
printf("please input name\n");
getchar();
scanf("%c",&a);
p=head;
if(a==p->name)
printf("Name:%c TEL:%d",p->name,p->num);
else
printf("Can't find");
}
void main()
{struct TXL *head;
head=creat();
find(head);
}追问看懂了 0就结束
追答嗯,你定义char 就要用'0'