返回语句 C 上的隔离错误



我使用此代码遇到的问题是当我尝试在第一次迭代时在addTrash函数中返回结构节点*头时。我怀疑这可能是堆栈损坏,但我不确定,我也不确切知道我将如何弄清楚我给你的代码中的错误是什么。

这是一个双向链表,其价值是它所持有的唯一数据。

struct node * modifyMainList( struct node *head, int link2Delete){
    struct node * curr = head;
    struct node * temp;
    int i = 0;  
    //traverse list link2Delete amount of times
    while(i != link2Delete){
        curr = curr -> next;
    }

    //head case
    if(curr -> previous == NULL){
        curr = curr -> next;            
        head = curr;
        return head;
    }
    //tail case 
    if(curr -> next == NULL){
        temp = curr;    
        curr = curr -> previous;
        curr -> next = NULL;
        temp -> previous = NULL;
        temp -> next = NULL;
        free(temp);
        return head;
    }
    curr -> previous -> next = curr -> next;
    curr -> next -> previous = curr -> previous;            

    curr -> previous = NULL;
    curr -> next = NULL;
    free(curr);
    return head;
}

struct node * addTrash(struct node *mainHead, int link2Delete){
    struct node * head = NULL;
    struct node * curr = mainHead;
    struct node * trashCurr = NULL;;
    struct node * temp = NULL;
    int i = 0;  
    printf("im in trash before loopnn");
    for(i = 0; i < link2Delete; i++){
        curr = curr -> next;
    }
    printf("im in trash before head size checknn");
    if(head == NULL){
        printf("im in trash with head == nullnn");
        //head of main list
        if(link2Delete == 0){
            printf("im in trash link2delete == nullnn");
            curr = curr -> previous;
            head = curr;
            curr = curr -> next;
            curr -> previous = NULL;
            curr -> next = NULL;
            return head;
        }
        printf("im in trash before tail casenn");
        //tail of main list
        if(curr -> next == NULL){

            printf("im in trash with tail casenn");
            head = curr;
            head -> previous = NULL;
            return head;
        }
        printf("im in trash before after tail casenn");
        //every other case
        //printf("this is the head value: %dnn", head -> value);
        head = curr;
        //printf("im in trash after head = currnn");
        head -> previous = NULL;
        //printf("im in trash after head -> previousnn");
        head -> next = NULL;
        printf("im in trash after head -> nextnn");
        printf("this is the head value: %dnn", head -> value);
        return head;
    }else{
        printf("im in trash inside elsenn");
        trashCurr = head;
        while(trashCurr -> next != NULL){
            trashCurr = trashCurr -> next;
        }
        if(link2Delete == 0){
            temp = curr; 
            trashCurr -> next = temp;
            temp -> previous = trashCurr;
            trashCurr = temp;
            trashCurr -> next = NULL;
            return head;
        }
        //tail of main list
        if(curr -> next == NULL){
            temp = curr;
            trashCurr = temp;
            temp -> previous = trashCurr;           
            temp -> next = NULL;
            trashCurr -> next = temp;
            return head;
        }
        //every other case
        temp = curr;    
        temp -> previous = trashCurr;
        trashCurr -> next = temp;
        trashCurr = temp;
        trashCurr -> next = NULL;
        return head;
    }

}

void generateRandom(struct node *mainHead, int size){
    int i = 0;
    int link2Delete = 0;
    struct node *head = NULL;
    srand ( time(NULL) );
    int number2Delete = rand() % size + 1;

    printf("this is the rand number: %dn", rand());    
    printf("this is the number of nodes to be deleted: %dn", number2Delete);
    for (i = 0; i < number2Delete; i++) {
        // Pick a random node (payload) to delete.  
        link2Delete = (rand() % size);
        printf("this is the number of nodes in the list: %dn", size);
        printf("this is the node to be deleted: %dn", link2Delete);
        size--;
        if(link2Delete == 0){
            mainHead = modifyMainList(mainHead, link2Delete);
            //printf("this is the call return: %dnn",  addTrash(mainHead, link2Delete) -> value);
            head = addTrash (mainHead, link2Delete);
        }else{
            head = addTrash (mainHead, link2Delete);
            mainHead = modifyMainList(mainHead, link2Delete);
        }
    }
    return;
}

在你的代码中,

while(i != link2Delete){
    curr = curr -> next;
}

是一个无限循环,如果link2Delete!=0 .

而且,在head case中不自由.

由于循环是无限的,curr = curr->next将继续重复,它将指向一些垃圾指针(如果循环条件为真(。然后,它具有未定义的行为。你可能会得到SEGFAULT。

最新更新