C语言中关于通过使用链表来查询数据

来源:百度知道 编辑:UC知道 时间:2024/09/23 20:46:58
# include<stdio.h>
# include<stdlib.h>
struct abc
{ int a;
char *b;
int c;
struct abc *d;
};
struct abc *abc(int n);
struct abc *nm(struct abc *p,struct abc *q);
main()
{ int n;
struct abc *p,*q,*w,*r,a;
system("cls");
printf("请输入班级人数:\n");
scanf("%d",&n);
system("cls");
printf("请输入这些同学的信息(例如,1 王浩然 98):\n");
w=q=p=abc(n);
while(p!=NULL)
{
scanf("%d%s%d",&p->a,p->b,&p->c);
p=p->d;
}
system("cls");
printf("您输入的这些同学的信息是:\n");
while(q!=NULL)
{
printf("%d %s %d\n",q->a,q->b,q->c);
q=q->d;
}
printf("请输入要查询的数据:\n");
scanf("%d",&a.a);
printf("您要的信息是:\n");
r=nm(w,&a);
if(r!=NULL)
printf("%d %s %d\n&

strcmp(p->a,q->a)
p->a
q->a
a是整形数据,为什么要使用strcmp比较?

struct abc
{ int a;
char *b; // <<---
int c;
struct abc *d;
};

结构里面的是字符串指针,而后面却直接用scanf("%s",..)获取,~~~

另外:
struct abc *nm(struct abc *p,struct abc *q)
{ struct abc *head;
head=p;
while(strcmp(p->a,q->a)!=0&&p!=NULL) // <<---
{
p=p->d;
}

while条件那么写的话不出问题真的也很奇怪~~很明显应该很检测p是否为空再去判断~~
while(p && strcmp(p->a,q->a)!=0)

# include<stdio.h>
# include<stdlib.h>
struct abc
{ int a;
char b[32]; //分配内存
int c;
struct abc *d;
};
struct abc *abc(int n);
struct abc *nm(struct abc *p, int num);

int main()
{
int n;
int num; // 定义一个变量,用以作查询时存储学号
struct abc *p,*q,*w,*r,a;
system("cls");

printf("请输入班级人数:\n");
scanf("%d&