简体   繁体   English

链表分割错误C

[英]Linked List Segmentation Fault C

I am learning linked lists in C and I am having a problem with my delete function keeps giving me a segmentation fault. 我正在学习C语言中的链表,而删除功能却一直给我分段错误。 I do not know what is wrong with the code. 我不知道代码有什么问题。

void delete(int d)
{
    struct list * current1 = head; 
    struct list * current2;

    if (len() == 0)
    { //prtError("empty");
        exit(0);
    }
    if (head -> data == d)
    { 
        head = head -> next;
    }

    //Check if last node contains element
    while (current1->next->next != NULL)
        current1 = current1->next;
    if(current1->next->data == d)
            current1->next == NULL; 


    current1 = head; //move current1 back to front */

    while(current1 != NULL && (current1->next->data != d))
        current1 = current1 -> next; 


    current2 = current1 -> next;
    current1 -> next = current2 -> next; 
}

From a quick glimpse: 快速浏览:

Lets say there are 100 structs ranging from 1~99. 可以说有100个结构,范围是1〜99。
The 100th would be (probably) NULL. 第100个(可能)为NULL。


while(current1 != NULL && (current1->next->data != d))

When the above code reaches the 99th struct. 当上面的代码到达第99个结构时。 You execute 2 checks. 您执行2个检查。

1) Check if the 99th is not NULL .. returns true 1)检查第99位是否不为NULL ..返回true
2) Check if the 100ths data is different than d 2)检查百分位数的数据是否与d不同

But there's no 100th struct. 但是没有第100个结构。
This results to undefined behaviour which COULD and probably WOULD lead to a segfault. 这会导致不确定的行为可能和可能导致段错误。

This is wrong in so many ways: 这在很多方面都是错误的:

1) 1)

while (current1->next->next != NULL)

If the list has only one element: 如果列表中只有一个元素:

current1 = head;
current1->next = NULL; 
current1->next->next = Seg Fault

2) 2)
If you're going to look if the last element has the provided data make sure you return from the function once you have found it and also free the memory for it: 如果要查看最后一个元素是否具有提供的数据,请确保在找到该元素后从该函数返回,并为其释放内存:

while(current1->next->next != NULL)
    current1 = current1->next;
if(current1->next->data == d){
        free(current->next);
        current1->next == NULL;
        return; 
}


3) 3)
If you search as above if the last element has your data (although a pointless search; no need to do it separately) you eliminate an error case from the code bellow. 如果按上述方式搜索最后一个元素是否包含您的数据(尽管无意义的搜索;无需单独进行搜索),则可以从下面的代码中消除错误情况。 But you will still have the situation when your data cannot be found in the list and current1 is positioned on the last element (so != NULL ) but current1->next->data != d with crash your program. 但是您仍然会遇到以下情况:无法在列表中找到您的数据,并且current1位于最后一个元素上(因此!= NULL ),但是current1->next->data != d导致程序崩溃。 This will happen if you don't return from the function at 2). 如果您没有从2)中的函数返回,则会发生这种情况。

current1 = head; //move current1 back to front */
while(current1 != NULL && (current1->next->data != d))
    current1 = current1 -> next; 


4) 4)
Free memory for the deleted node: 删除节点的可用内存:

current2 = current1 -> next;
current1 -> next = current2 -> next; 
free(current2);

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

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