0

I am learning linked lists in C and I am having a problem with my delete function. A segmentation fault occurs at the line:

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

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; 
}
2
  • I fixed the problem with the NULL bu now I am getting a fault at the last line current1 -> next = curren2 -> next Commented Jul 9, 2013 at 22:13
  • I edited my answer to explain with you are getting this error now. Commented Jul 10, 2013 at 18:57

3 Answers 3

1

You are making sure there's at least one element at current1 with your test current1 != NULL so current1->next is guaranteed to work, but it might return NULL itself causing current1->next->data to crash as it tries to get data from the next element.

Sign up to request clarification or add additional context in comments.

Comments

1

At this line you don't know if current1->next is NULL or not. If it is NULL and you try to access current1->next->data you will end with a segmentation fault.

You have two solution to fix your loop :

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

Or

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

But in the second case you have to be sure that at the first loop current1 != NULL.

To answer to the comment :

It is the same error, you don't know if current1 is NULL or not and you try to access to the next element, you have two things to do :

  • First you have to be sure that current1 is not NULL before trying to reach the next
  • And just after you have to be sure that current2 is not NULL too.

You have two possibilies (easy to do) :

if ( NULL != current1 )
{
    current2 = current1->next;

    if ( NULL != current2 )
        current1->next = current2->next;
}

Or

if ( NULL != current1 && NULL != current1->next )
{
    current2 = current1->next;
    current1->next = current2->next;
}

Both will work well. Choose the one you prefer.

Comments

0

In the line that you told us had the error, you're never checking whether or not current1->next is NULL or not. At some point in your program, current1->next is NULL, and so you are trying to dereference the address 0. At that point, the code is equivalent to ((struct list *)0)->data. Thus the segmentation fault.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.