Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading