473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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;
while (q != head)
{ next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}
}
Nov 14 '05 #1
5 5484
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
the other links.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #2
On 24 Oct 2004 18:27:46 -0700, hk******@gmail.com (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;
while (q != head)
{ 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.

Nov 14 '05 #3
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)
{
NodePtr next, q=p, head=p->prev;
while (q != head)
{
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" <hk******@gmail.com> wrote in message
news:2f**************************@posting.google.c om...
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;
while (q != head)
{ next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}
}

Nov 14 '05 #4
In article <cl**********@newshost.mot.com>,
"Sriram Rajagopalan" <sr****@mot.com> 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)
{
NodePtr next, q=p, head=p->prev;
while (q != head)
{
next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}
next = q->next;
q->next = q->prev;
q->prev = next;
}


Try:

void reverse_list(NodePtr head)
{
NodePtr q = head;

do {
NodePtr next;

next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
} while (q != head);
}

Cheers,
- jonathan
Nov 14 '05 #5
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;
while (q != head)
{ next = q->next;
q->next = q->prev;
q->prev = next;
q = next;
}
}


NodePtr reverse_list(NodePtr head)
{
NodePtr q = head;

while(q != NULL) {
head = q;
q = q -> next;
head -> next = head -> prev;
head -> prev = q;
}
return head;
}

--
pete
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: D. Beckham | last post by:
I wrote the following code to create a short doubly-link list of three strings: "one", "two", and "three". I would like to know if I set them up correctly, so that they point to one another. ...
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
9
by: Perpetual Snow | last post by:
How can I reverse a linked list with no memory allocation? I'm searching for an algorithm which is constant in runtime and space. Thanks
3
by: Sathyaish | last post by:
I wanted to practice some Linked List stuff, so I set out to create a linked list. The plan was to create the following: (1) A linked list class in Visual Basic (2) A non-class based linked list...
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
11
by: Neo | last post by:
Hi Frns, Could U plz. suggest me how can i reverse a link list (without recursion)??? here is my code (incomplete): #include<stdio.h>
5
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
11
by: sam_cit | last post by:
Hi Everyone, I want to actually reverse a single linked list without using many variables, i have a recurssive solution, but i wanted an iterative one. can anyone help me on this?
1
by: Mahesh | last post by:
Hi Coders, I was asked to write a program to interchange numbers using doubly linked list @ Amazon. Here is the details with Code that i wrote. i/p: 1 2 3 4 5 6 7 8 .....n,n-1. o/p: 2 1 4...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.