Saturday, November 14, 2009

Linked-list?

What is the best way to print the values in a single linked-list in reverse?

Linked-list?
Easiest way is to do with a recursive function.


printrev(node* head)


{


if(head==NULL)


return;


printrev(head-%26gt;next);


print(head);


}





It is never the best way as you are using recursive function, rather if you need to print in reverse order it is better to use doubly linked lit.
Reply:doesnt c++ suck?
Reply:The best way, according to me would be to traverse the linked-list and push all the values of the node onto a stack. Then you can simply pop from the stack and print the values.

shoe uppers

No comments:

Post a Comment