根据您的at索引从C中的链接列表中删除

时间:2021-09-09 07:18:16

So far I have managed to delete everything from the ith index but I need to be able to connect the node from before the ith to the node after the ith index this is what I have so far.

到目前为止,我已经设法删除了第i个索引中的所有内容,但是我需要能够将第i个索引之前的节点连接到第i个索引之后的节点,这是我到目前为止所拥有的。

LinklistNode* remove_node(LinklistNode* list_head, int index){
if(list_head != NULL){
    LinklistNode* temp;
    if(list_head-> next ==NULL){
        temp = list_head;
        list_head = NULL;
    }
    else{
        LinklistNode* list_pointer = list_head;
        LinklistNode* next_list_pointer = list_pointer->next;
        while(next_list_pointer->next != NULL && index > 0){
            index--;
            list_pointer = next_list_pointer;
            next_list_pointer = next_list_pointer->next;
        }
        temp = next_list_pointer;
        list_pointer->next = NULL;
    }
    free(temp);
   }
   return list_head;
}

3 个解决方案

#1


1  

To link the nodes before and after the index, point list_pointer->next to next_list_pointer->next

要链接索引之前和之后的节点,请指向list_pointer-> next_list_pointer-> next旁边

while(next_list_pointer->next != NULL){
        index--;
        if (index == 0) break;
        list_pointer = next_list_pointer;
        next_list_pointer = next_list_pointer->next;
}
temp = next_list_pointer;
list_pointer->next = temp->next;

#2


1  

In order to connect the node before the ith element use your list_pointer. Have something like ...

为了在第i个元素之前连接节点,请使用list_pointer。有类似......

while(next_list_pointer != NULL || index != 0){
    list_pointer = list_pointer->next;
    next_list_pointer = list_pointer->next;
}

if(next_list_pointer == NULL){
   printf("Invalid index given. Nothing deleted.\n");
   //Do not delete otherwise an error will be given
   return list_head;
} else {
   //Valid index was given 
   list_pointer->next = next_list_pointer->next;
   free(next_list_pointer);
   return list_head;
}

... This should pretty much cover the deletion portion just be sure to include your base cases.

...这应该涵盖删除部分,只需确保包含您的基本案例。

#3


0  

Alternate method using pointer to pointer to node:

使用指针指向节点的指针的替代方法:

LinklistNode* remove_node(LinklistNode* list_head, int index)
{
LinklistNode **ppNode = &list_head;
    if(list_head == NULL)
        return NULL;
    while(index--){
        ppNode = &((*ppNode)->next);
        if(*ppNode == NULL)             /* if index not in list */
            return list_head;           /*   just return */
    }
    *ppNode = (*ppNode)->next;          /* delete node */
    return list_head;
}

#1


1  

To link the nodes before and after the index, point list_pointer->next to next_list_pointer->next

要链接索引之前和之后的节点,请指向list_pointer-> next_list_pointer-> next旁边

while(next_list_pointer->next != NULL){
        index--;
        if (index == 0) break;
        list_pointer = next_list_pointer;
        next_list_pointer = next_list_pointer->next;
}
temp = next_list_pointer;
list_pointer->next = temp->next;

#2


1  

In order to connect the node before the ith element use your list_pointer. Have something like ...

为了在第i个元素之前连接节点,请使用list_pointer。有类似......

while(next_list_pointer != NULL || index != 0){
    list_pointer = list_pointer->next;
    next_list_pointer = list_pointer->next;
}

if(next_list_pointer == NULL){
   printf("Invalid index given. Nothing deleted.\n");
   //Do not delete otherwise an error will be given
   return list_head;
} else {
   //Valid index was given 
   list_pointer->next = next_list_pointer->next;
   free(next_list_pointer);
   return list_head;
}

... This should pretty much cover the deletion portion just be sure to include your base cases.

...这应该涵盖删除部分,只需确保包含您的基本案例。

#3


0  

Alternate method using pointer to pointer to node:

使用指针指向节点的指针的替代方法:

LinklistNode* remove_node(LinklistNode* list_head, int index)
{
LinklistNode **ppNode = &list_head;
    if(list_head == NULL)
        return NULL;
    while(index--){
        ppNode = &((*ppNode)->next);
        if(*ppNode == NULL)             /* if index not in list */
            return list_head;           /*   just return */
    }
    *ppNode = (*ppNode)->next;          /* delete node */
    return list_head;
}