【发布时间】:2013-12-31 17:39:27
【问题描述】:
我一直在研究 C 中的链表,关于 append 函数,我遇到了以下代码:
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL){
right=right->next;
}
right->next =temp;
right=temp;
right->next=NULL;
}
为了节省一行代码,是不是可以在temp 的下一个 属性?像这样:
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
temp -> next = NULL;
right=(struct node *)head;
while(right->next != NULL){
right=right->next;
}
right->next =temp;
}
【问题讨论】:
-
这没什么问题。
-
不能保证
head指向正确的列表。
标签: c null linked-list append