链表中,关于函数参数的传递(为什么必须要用&了?)
我的链表的源程序如下,在creat_list()函数中,参数HEAD必须要加上引用,但我一直想找一下"官方解释",谢谢!!
#include <iostream.h>
struct student
{
int number;
float score;
struct student *next;
};
void creat_list(student * &HEAD) //此处的引用符号&必须要加上!
{
student *pS, *pEnd;
int number;
float score;
HEAD = new student;
pEnd = HEAD;
cin >>number >>score;
while(number != 0)
{
pS = new student;
pS->number = number;
pS->score = score;
pEnd->next = pS;
pEnd = pS;
cin >>number >>score;
}
pEnd->next = NULL;
}
void show_linkedlist(student *head)
{
cout <<"Output the items of the linkedlist:" <<endl;
student *p_show = head->next;
while(p_show != NULL)
{
cout <<p_show->number <<" " <<p_show->score <<endl;
p_show = p_show->next;
}
}
void main()
{
student * head = NULL;
creat_list(head);
show_linkedlist(head);
}