# What's wrong in my function of reverse the doubly linked list with dummy node.

Discussion in 'C Programming' started by Daniel, Oct 25, 2004.

1. ### DanielGuest

I need to reverse the doubly linked list with dummy node. I think the
solution is to exchange each node pointers' next and previous address.
But what's wrong in my function?
Thanks

void reverse_list(NodePtr p)
{ next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}
}

Daniel, Oct 25, 2004

2. ### CBFalconerGuest

Re: What's wrong in my function of reverse the doubly linked list withdummy node.

Daniel wrote:
>
> I need to reverse the doubly linked list with dummy node. I think
> the solution is to exchange each node pointers' next and previous
> address. But what's wrong in my function?

If it is doubly linked there is no need to reverse it. Just follow

--
Chuck F () ()
Available for consulting/temporary embedded and systems.

CBFalconer, Oct 25, 2004

3. ### Nick AustinGuest

On 24 Oct 2004 18:27:46 -0700, (Daniel) wrote:

>I need to reverse the doubly linked list with dummy node. I think the
>solution is to exchange each node pointers' next and previous address.
>But what's wrong in my function?
>Thanks
>
>void reverse_list(NodePtr p)
> { next = q->next;
> q->next = q->prev;
> q->prev = next;
> q = next;
> }
>}

You swap one node too few. Change the while loop to a do-while
loop so that the end condition is at the end of the loop.

Nick.

Nick Austin, Oct 25, 2004
4. ### Sriram RajagopalanGuest

Dan,

As Nick said the unltimate node is not getting swapped.
But, do-while would also repeat the same mistake.

So, handle the ultimate case seperately as below:

I guess this shud work,

void reverse_list(NodePtr p)
{
{
next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}
next = q->next;
q->next = q->prev;
q->prev = next;
}

Cheers,
Sriram.

"Daniel" <> wrote in message
news:...
> I need to reverse the doubly linked list with dummy node. I think the
> solution is to exchange each node pointers' next and previous address.
> But what's wrong in my function?
> Thanks
>
> void reverse_list(NodePtr p)
> { NodePtr next, q=p, head=p->prev;
> { next = q->next;
> q->next = q->prev;
> q->prev = next;
> q = next;
> }
> }

Sriram Rajagopalan, Oct 26, 2004

In article <cll5u9\$2ie\$>,
"Sriram Rajagopalan" <> wrote:

> Dan,
>
> As Nick said the unltimate node is not getting swapped.
> But, do-while would also repeat the same mistake.

Not if you write it correctly.

> So, handle the ultimate case seperately as below:
>
> I guess this shud work,
>
> void reverse_list(NodePtr p)
> {
> {
> next = q->next;
> q->next = q->prev;
> q->prev = next;
> q = next;
> }
> next = q->next;
> q->next = q->prev;
> q->prev = next;
> }

Try:

{

do {
NodePtr next;

next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}

Cheers,
- jonathan

6. ### peteGuest

Daniel wrote:
>
> I need to reverse the doubly linked list with dummy node. I think the
> solution is to exchange each node pointers' next and previous address.
> But what's wrong in my function?
> Thanks
>
> void reverse_list(NodePtr p)
> { NodePtr next, q=p, head=p->prev;
> { next = q->next;
> q->next = q->prev;
> q->prev = next;
> q = next;
> }
> }

{

while(q != NULL) {
q = q -> next;