void initList(LINKList head)这条语句书上的例子是&first我没有&只有head过了编译程序也能跑样例,加上去反而报错。到底应该怎么写?
题目大意是求t次,每次第一行输入n,m。输入n行学生姓名和成绩,输入m次学生编号(查找m次学生成绩输出)。刻意用链表做的所以有这些问题。
还有本题的head被警告没有初始化,正确的写法应该是怎么样的呢?求解惑,谢谢。
代码见下
#include <stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct StuNode{
char id[11];
int test;
struct StuNode *next;
}Linknode,*LINKList;
void initList(LINKList head){
head =(Linknode*)malloc(sizeof(Linknode));
if(!head){printf("内存分配错误!\n");exit(1);}
head->next=NULL;
}
void Insert (LINKList head,int n){
LINKList p=(Linknode*)malloc(sizeof(Linknode));
head->next=p;
p->next=NULL;
for(int i=1;i<=n;i++){
LINKList t=(Linknode*)malloc(sizeof(Linknode));
scanf("%s %d",t->id,&t->test);//地址
t->next=p->next;
p->next=t;
}
}
LINKList Search_name(char name[], LINKList head)
{
LINKList p = head;
while (p->next != NULL)
{
if (strcmp(name, p->next->id) == 0)
{
return p;
}
p = p->next;
}
return NULL;
}
int main(){
int t,n,m;
LINKList head;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
initList(head);
Insert(head,n);
char name[11];
LINKList temp;
for(int i=1;i<=m;i++){
scanf("%s", name);
temp = Search_name(name, head);
if (temp == NULL)
{
printf("查无此人!\n");
}
else
{ temp = temp->next;
printf("%d\n",temp->test);
}
}
}