-1
bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{

    while(temp->next!=current)
    {
        temp=temp->next;
        current=temp;
    }
    temp=head;

    return current; 
}

I have linked list and 'head' pointer hold first node , 'current' pointer hold last node,I want to bring 'current' to head one by one ,so I write this function but it gives segmentation fault when I debug the program

karabugra05
  • 55
  • 1
  • 1
  • 7
  • 1
    `while(temp->next!=current)` should be `while(temp && temp->next!=current)`, otherwise you would dereference a NULL pointer, which is undefined behaviour. – mch Oct 14 '15 at 13:48
  • 1
    There isn't a really convenient way to traverse a singly linked list backward... you can use a double linked list instead, or reverse the list first and traverse the reversed list forward. – Dmitri Oct 14 '15 at 13:51
  • mch I tried your answer but it is not solved ,I dont want to reverse list thank you for answers – karabugra05 Oct 14 '15 at 13:55
  • what is the point of having 'temp' as a parameter? – Selçuk Cihan Oct 14 '15 at 14:04
  • I learned it new ,temp was not necessarily , thank yo @SelçukCihan – karabugra05 Oct 14 '15 at 14:17

1 Answers1

0

temp->next!=current will never be true unless temp==temp->next.

Try this:

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{

    while(temp->next!=current)
    {
        temp=temp->next;
    }
    current=temp; /* get this out of the loop */
    temp=head;

    return current; 
}

or more simplified this:

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{
    (void)head; /* head isn't used, so put this to avoid warning */
    while(temp->next!=current)
    {
        temp=temp->next;
    }
    /* current and temp will be lost, so assigning to them here is useless */

    return temp; 
}

To make it safer, make sure that temp isn't NULL to avoid runtime error in case of current is invalid.

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{
    (void)head; /* head isn't used, so put this to avoid warning */
    while(temp != NULL && temp->next!=current)
    {
        temp=temp->next;
    }

    /* temp will be the previous node of current or NULL */
    return temp; 
}

Maybe you want this:

bn_ptr drive_temp(bn_ptr head,bn_ptr current) /* remove temp from the arguments */
{
    bn_ptr temp = head;
    while(temp != NULL && temp->next!=current)
    {
        temp=temp->next;
    }

    /* temp will be the previous node of current or NULL */
    return temp; 
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    @ameyCU It shouldn't be. Casting to `void` game me [no errors nor warnings](http://melpon.org/wandbox/permlink/sh88B0VLjn5jwXbV), and casting to `void*` gave me [a warning](http://melpon.org/wandbox/permlink/nBrMF9ayGbUDCjae). – MikeCAT Oct 14 '15 at 13:57
  • That is just a warning of unused variable. But I am surprised it didn't gave anything with `void` . – ameyCU Oct 14 '15 at 14:02
  • @ameyCU Why the cast **should be** `void *` despite of the warning? Why do you **want** the warning? – MikeCAT Oct 14 '15 at 14:04
  • @MikeCAT i am grateful to you ,your codes worked a lot ,and thank you ameyCU – karabugra05 Oct 14 '15 at 14:14