The Gateway to Computer Science Excellence
0 votes
114 views

Function to reverse the circular doubly linked list.
Why is this function showing an error?

head is a global variable.

void reverse()
{
    struct node *z;
    struct node *t = head -> forw;
    while(t != head)
    {
        z=t->back;
        t->back=t->forw;
        t->forw=z;
        t = t -> back;
    }
  z=t->back;
  t->back=t->forw;
  t->forw=z;
  head=t->forw;
}

 

in Programming by (57 points)
edited by | 114 views
0
what error it is showing?
0
Segmentation fault
+1
The code seems to be all right but have you made sure that the head is not null?
0
there is no issue with the code... i hope you didn't make your list is circular ( means somewhere you have NULL ) before calling this reverse() function
0
Yes, you are correct

Thanks for the help.
0
@Shaik @MiNiPanda

is there will be issue of dangling pointer or memory leakage there?

Here only head need to change it's position

right?
0
If at some point the situation becomes like "NULL->back" or "NULL->forw" then it will give segmentation fault.

No memory is freed in this code so dangling pointer won't be there.

The node linkings are implemented properly so no memory leakage here.
0

 in that question, before calling the function

1) head variable may corrupted ( ie., it may be dangling or wild pointer)

2) passed list have some where NULL Link.

 

Here only head need to change it's position

It is not sufficient for making linked lists reverse. 

0
By making the head point to the last node and treating the "back" link as the "forw" link we can access the linked list in reverse order but we are not physically changing the orientation which is actually needed.
0
yes, that I told, head can just moved to last node and doubly circular LL get reversed

but here code definitely have some error.

it is not able to forward
0

but here code definitely have some error.

where you are facing it. 

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,492 answers
195,439 comments
100,695 users