Can someone take a look and let me know if this is right...

I need to add a method/function to output a linked list in reverse order. I need to use either the header and source files for the dynamic list class. I cannot reset links or alter the original linked list. I need some help by 8 so any assistance is greatly appreciated

void linklist::reverse()
{
node *curr = p, *prev = NULL, *nxt_fwd = NULL;
//p is header
while(curr)
{
nxt_fwd = curr->link;
curr->link = prev;
prev =curr;
curr=nxt_fwd;
}
p = prev; /*save the new head */

}

It will depend if it the list is single or double link. for single linked list if the list has a size member than you can create an array of type node and then go through the list and start filling the array from the end. Once you reach the end of the list the you can print out the array. For doubly linked list you can start at the tail node and traverse back to the front node printing the data along the way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.