Why does this way of Deleting Repeated Elements in a Doubly Linked List not work?

I tried to delete repeated elements in a Doubly Linked List, using this logic
I’ll create a temporary pointer that will traverse till the end of a linked list under a for loop till it reaches NULL. Under this for loop, I’ll again traverse till the end of linked list, to search for repeated elements. If I find the element repeated I’ll assign it to a variable called temporaryStorer, and then after changing the links, free the temporaryStorer

Why does this cause an error at the line:

(index->leftLink->rightLink)=(index->rightLink);

Here is the code

NODE* deleteDuplicates(NODE* head){
    NODE* current;
    NODE* temporaryStorer;
    NODE* index;
    if (head==NULL) {
        printf("List is empty\n");
        return head;
    } else {
        for (current=head; current!=NULL; current=current->rightLink) {
            for (index=current->rightLink; index!=NULL; index=index->rightLink) {
                if ((index->info)==(current->info)) {
                    //Values are equal
                    //Index is duplicate
                    temporaryStorer=index;
                    (index->leftLink->rightLink)=(index->rightLink);
                    if (index->rightLink!=NULL){
                        index->rightLink->leftLink=index->leftLink;
                        free(temporaryStorer);
                    }
                       
                } else {
                    continue;
                }
            }
        }
    }
    
    return head;
}

My Node is defined in this way

typedef struct NODE {
    struct NODE* leftLink;
    int info;
    struct NODE* rightLink;
    
}NODE;

The error I obtained says Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)

  • To me, this does not appear to be a simple linked list but rather a binary tree structure.

    – 

  • 1

    Run it in gdb, find the point that the error was thrown, and get a backtrace / point out that line. @500-InternalServerError They’re using “leftlink” and “rightlink” as “previous” and “next” values respectively (so a Deque), it’s still being treated as a linkedlist structure (in terms of what is currently given to us).

    – 




  • Oh, sorry, I meant not Linked List, but Doubly Linked List

    – 

  • What is head‘s leftlink‘s value?

    – 

  • When it crashes on this line, then index->leftLink will be NULL, as index cannot be NULL at that moment.

    – 

In the inner for loop you have

for (index=current->rightLink; index!=NULL; index=index->rightLink) {
     if ((index->info)==(current->info)) {
        ...
        temporaryStorer=index;
        ...
        if (index->rightLink!=NULL){
               ...
               free(temporaryStorer);
        }
    }
}

So as temporaryStorer points to the same memory as index (and so doesn’t serve any purpose in your code snippet) in fact you call free(index) and then you access it again by index=index->rightLink. That invokes Undefined behaviour

Leave a Comment