C 在末尾插入链表
这是我的linkedlistcode,用于在末尾插入节点。我正在将错误代码转储。所以,请帮助我代码有什么问题 如果我继续回来;在末尾和*headRef=newnode之后的行;它起作用了。那么为什么要返回一个void函数呢C 在末尾插入链表,c,C,这是我的linkedlistcode,用于在末尾插入节点。我正在将错误代码转储。所以,请帮助我代码有什么问题 如果我继续回来;在末尾和*headRef=newnode之后的行;它起作用了。那么为什么要返回一个void函数呢 struct node { int data; // node format struct node* next; }; void insertAtEnd(struct node** headRef, int newData) { struct no
struct node
{
int data; // node format
struct node* next;
};
void insertAtEnd(struct node** headRef, int newData)
{
struct node* ptr;
struct node* newnode;
ptr = (*headRef);
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = newData;
newnode->next = NULL;
if (*headRef == NULL)
{
*headRef = newnode;
}
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newnode;
}
您应该返回if内部,因为在if语句之后,它再次进入while循环,并在末尾添加额外的节点
struct node
{
int data; // node format
struct node* next;
};
void insertAtEnd(struct node** headRef, int newData)
{
struct node* ptr;
struct node* newnode;
ptr = (*headRef);
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = newData;
newnode->next = NULL;
if (*headRef == NULL)
{
*headRef = newnode;
return;
}
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newnode;
return;
}
python与此有何关系?选择一种语言来诊断核心转储我们需要查看完整的程序。在进入
while
循环之前,应该设置ptr=*headRef
。此外,如果*hreadRef==NULL
则应返回
,这样就不会进入以下while循环。1)检查malloc
是否成功,2)如果head为NULL,则不要执行while循环,其余代码则在insertAtEnd()中执行