删除链表中的节点不';我不能在c工作
我想从链表中删除一个节点,但它不起作用。这是我的密码:删除链表中的节点不';我不能在c工作,c,data-structures,linked-list,C,Data Structures,Linked List,我想从链表中删除一个节点,但它不起作用。这是我的密码: jL Delete(jL* node,int n) { jL first1, n_th, save; int count = 0; save = first1 = n_th = (*node); while(first1->next) { first1 = first1->next; count++; if(count == (n-1)) break; } while ( first1->
jL Delete(jL* node,int n)
{
jL first1, n_th, save;
int count = 0;
save = first1 = n_th = (*node);
while(first1->next)
{
first1 = first1->next;
count++;
if(count == (n-1))
break;
}
while ( first1->next != NULL )
{
first1 = first1->next;
save = n_th;
n_th = n_th->next;
}
save->next = n_th->next;
free(n_th);
return (&node);}
我的错在哪里?你能帮我一下吗?看来你想要以下的东西。我假设列表中元素的索引从0开始
jL Delete( jL* node, int n )
{
jL current = *node;
jL prev = NULL;
while ( current != NULL && n != 0 )
{
prev = current;
current = current->next;
--n;
}
if ( n == 0 && current != NULL )
{
if ( prev == NULL ) *node = current->next;
else prev->next = current->next;
free( current );
}
return *node;
}
由于列表指针已更新,所以不需要返回值,或者函数可以返回通过/失败指示器(如果列表中没有n,则返回失败)
不显示数据声明,修改本地副本,返回参数的地址。这应该足够了。哦,调试器……….DCVI已经回滚了您的编辑-如果您想删除您的问题,请这样做,但不要只是删除代码-这会使您的问题无法理解,而且会使现有的答案变得无用。
void remove_node(jL* node, int n)
{
jL *ppNode = node;
jL pNode;
if(node == NULL || *node == NULL)
return;
while(n--){
ppNode = &((*ppNode)->next);
if(*ppNode == NULL) /* if n not in list */
return; /* just return */
}
pNode = *ppNode; /* delete node */
*ppNode = (*ppNode)->next;
free(pNode);
}