【发布时间】:2015-03-30 15:39:52
【问题描述】:
在链表中使用双指针或仅全局声明头指针哪个更好的编程实践
//这里head双指针作为参数传递
Void insertatend(node **head, int item)
{
node *ptr, *loc;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
ptr->next=NULL;
if(*head==NULL)
*head=ptr;
else
{
loc=*head;
while (loc->next!=NULL)
{
loc=loc->next;
loc->next=ptr;
}
}
}
或者这个
//这里我已经将头指针声明为全局的了
void insert(int x)
{
node *ptr,*ptr1;
ptr=(node*)malloc(sizeof(node));
ptr->info=x;
if(head==NULL)
{
ptr->next=head;
head=ptr;
}
else
{
ptr1=head;
while(ptr1->next!=NULL)
{
ptr1=ptr1->next;
}
ptr1->next=ptr;
ptr->next=NULL;
}
}
【问题讨论】:
-
顺便说一句
loc->next=ptr;insertatend移动到while循环之后。
标签: c data-structures computer-science