can somebody help me how to print in reverse the data in a structure. i tried to pass the 1st, 2nd and so on node on tempPtr to reach the end (NULL) but when i was there i dont know how to get the data in the previous node so that it will print at the back.
You will get better help when you post code that shows that you have tried. You don't have to post all of your code, perhaps just the reverse function.
write a function that gets the node as a parameter.
call that function with the first node.
within the function:
if you have a next node call this function with the next node as parameter, otherwise print out the desired value.
As you're using a linked list, think of that: there's no reason why linked lists have to be unidirectional. You could make each node a chunk of data and two pointers, one to the next and one to the previous element.
If you have a single linked list, you use recursion as people sugested.
the function wil check if the "Next" pointer of the node is null, if it's not it will call the same function again with the "Next" pointer.
Hope it helps.
void reverse(ListNodePtr currentPtr)
{
if(currentPtr==NULL) {
puts("List is empty\n");
return;
}
elseif ( currentPtr->nextPtr != null ) {
reverse(currentPtr->nextPtr);
}
else puts("End of list reached, now printing from last element to first\n")
printf("%c -->", currentPtr->data);
}