简体   繁体   English

单链表-从中间删除

[英]Single Linked List - Delete From middle

I am trying to figure out an algorithm to delete from the middle of a linked list.. 我正在尝试找出从链表中间删除的算法。

My idea is to traverse the list, find the node right before the node I want to delete, call it Nprev, and set Nprev to Nnext where Nnext is after the node to delete Ndelete. 我的想法是遍历列表,在要删除的节点之前找到该节点,将其称为Nprev,并将Nprev设置为Nnext,其中Nnext在要删除Ndelete的节点之后。

So Nprev -> Ndelte -> Nnext . 所以Nprev -> Ndelte -> Nnext

My problem is that I cannot figure out how to traverse this list to find the node before the one I want. 我的问题是我无法弄清楚如何遍历此列表以在所需节点之前找到该节点。

I've been doing this with seg faults because I assign pointers out of range I assume. 我一直在使用seg错误执行此操作,因为我分配的指针超出了我假设的范围。 Its a very messy algorithm that I have, with many if else statements.. 我有一个非常混乱的算法,有很多if语句。

Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

Basically I need to go through the list, apply a function to each node to test if it is true or false. 基本上,我需要遍历该列表,将一个函数应用于每个节点以测试它是对还是错。 If false I delete the node. 如果为假,则删除该节点。 Deleting first and last is not as hard but middle stumped me. 删除第一个和最后一个并不那么困难,但是中间让我感到困惑。

Please let me know if there are some general ways to solve this problem. 请让我知道是否有一些通用方法可以解决此问题。 I've been scouring the internet and found nothing I need. 我一直在搜寻互联网,却找不到我需要的东西。

I used this: http://www.cs.bu.edu/teaching/c/linked-list/delete/ 我用了这个: http : //www.cs.bu.edu/teaching/c/linked-list/delete/

but the algorithm before step 4 only deletes the first node in my list and doesn't do any more. 但是第4步之前的算法仅删除列表中的第一个节点,并且不再执行任何操作。 How can I modify this? 我该如何修改?

They also give a recursive example but I don't understand it and am intimidated by it. 他们还给出了一个递归示例,但我不理解它,并对它感到害怕。

First you need to find the middle node. 首先,您需要找到中间节点。 Well take 3 pointers fast, slow, prev with fast moving with twice the speed of slow and prev storing the address of the node previous of slow. 好吧,以快,慢,以快于速度的两倍的速度快速移动3个指针,并预先存储慢速节点的地址。 ie *slow=&head,*fast=&head,prev=Null traverse the list and when fast=NULL slow will point to the middle node if number of elements are odd and prev will store the address of node previous of the mid node. *slow=&head,*fast=&head,prev=Null遍历列表,当fast=NULL时,如果元素数量为奇数,slow将指向中间节点,并且prev将存储中间节点之前节点的地址。 so simply prev->next=slow->next . 因此只需prev->next=slow->next

Here an example of something I use to search and remove by index: 这是我用来按索引搜索和删除的示例:

Given this struct : (Can also be adapted to other self referencing structs) 给定此结构 :(也可以适用于其他自引用结构)

struct node
{
    S s;
    int num;
    char string[10];
    struct node *ptr;
};
typedef struct node NODE;

Use this to remove an item from somewhere in the "middle" of the list (by index) 使用它从列表的“中间”某处删除某项(按索引)

int remove_by_index(NODE **head, int n) /// tested, works 
{
    int i = 0;
    int retval = -1;
    NODE * current = *head;
    NODE * temp_node = NULL;

    if (n == 0) {
        return pop(head);
    }

    for (int i = 0; i < n-1; i++) {
        if (current->ptr == NULL) {
            return -1;
        }
        current = current->ptr;
    }

    temp_node = current->ptr;
    retval = temp_node->num;
    current->ptr = temp_node->ptr;
    free(temp_node);

    return retval;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM