468,253 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

doubly linked list

Hi, I'm wondering how you can recursively reverse a doubly linked list with C?
May 13 '06 #1
4 6886
Banfa
9,033 Expert Mod 8TB
The easiest way to to set up temporary new start and end pointers then remove items from the current list and added the to the new list in the reverse order (i.e. remove from and add to end or vis-versa), when you have completed the operation copy you temperary start and end pointers to your permanent start and end pointers.
May 13 '06 #2
Okay, I tried to come up with this code, but I don't think it's quite right, can someone please check it over? Thanks.

struct node *reverse(struct node *head)
{
if (head==NULL) return NULL;
if (head-->next==NULL) {
head-->prev=NULL;
return head;
}

rest=head-->next;
rest=reverse(rest);
head-->next-->next=head;
head-->prev=head-->next;
head-->next=NULL;
return rest;
}
May 16 '06 #3
Banfa
9,033 Expert Mod 8TB
Why don't you think it's right? Have you tried running it?

Looks ok and works fine for me.
May 16 '06 #4
What is the program suppost to output?

Okay, I tried to come up with this code, but I don't think it's quite right, can someone please check it over? Thanks.

struct node *reverse(struct node *head)
{
if (head==NULL) return NULL;
if (head-->next==NULL) {
head-->prev=NULL;
return head;
}

rest=head-->next;
rest=reverse(rest);
head-->next-->next=head;
head-->prev=head-->next;
head-->next=NULL;
return rest;
}
Nov 9 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by surrealtrauma | last post: by
4 posts views Thread by dssuresh6 | last post: by
5 posts views Thread by free2cric | last post: by
1 post views Thread by drewy2k12 | last post: by
2 posts views Thread by murali | last post: by
3 posts views Thread by maruf.syfullah | last post: by
5 posts views Thread by adam.kleinbaum | last post: by
kim6987
4 posts views Thread by kim6987 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.