c - 根据您的索引从 C 中的链表中删除

标签 c indexing linked-list

到目前为止,我已经设法删除了第 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;
}

最佳答案

要链接索引前后的节点,将list_pointer->next指向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;

关于c - 根据您的索引从 C 中的链表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34120349/

相关文章:

c - 编写一个重新排列链表的函数,将偶数位置的节点放在列表中奇数位置的节点之后

c - 打乱结构体

indexing - DynamoDB 中本地索引和全局索引之间的差异

Mongodb低基数索引

c++ - 做关于兔子群的练习,撞墙了

c++ - 段错误发生在某些编译器中,但其他编译器没有

c - 数据类型问题

c - 在我的 printf 中打印一个 = 符号

c++ - 嵌入式系统时钟

mysql - 复合索引中的 OR 问题