69,336
社区成员
发帖
与我相关
我的任务
分享
//头插法建立链表
void InsertT(LNode *&C,int arry[],int x)
{
LNode *s,*r;
C=(LNode*)malloc(sizeof(LNode));
C->next=NULL;
r=C;
for(int i=0;i<x;++i)
{
s=(LNode*)malloc(sizeof(LNode));
s->num=arry[i];
s->next=r->next;
r->next=s;
}
}
[/quote]
我的理解:
void InsertT(LNode *&C,int arry[],int x) 中的第一个参数是一个LNode*类型的引用,即一个对指针类型的引用,这是一个pass by reference的方式,目的是改写指针LNode* C的值(函数体内通过将malloc的值赋给C,的确达到了这个目的)
类似于这个:
void test_ref(int *&a)
{
a = new int;
*a = 10;
}
void main()
{
int *p;
test_ref(p);
cout<<*p<<endl;
}
//头插法建立链表
void InsertT(LNode *&C,int arry[],int x)
{
LNode *s,*r;
C=(LNode*)malloc(sizeof(LNode));
C->next=NULL;
r=C;
for(int i=0;i<x;++i)
{
s=(LNode*)malloc(sizeof(LNode));
s->num=arry[i];
s->next=r->next;
r->next=s;
}
}
//头插法建立链表
void InsertT(LNode *&C,int arry[],int x)
{
LNode *s,*r;
C=(LNode*)malloc(sizeof(LNode));
C->next=NULL;
r=C;
for(int i=0;i<x;++i)
{
s=(LNode*)malloc(sizeof(LNode));
s->num=arry[i];
s->next=r->next;
r->next=s;
}
}