1

I had just tried out a program to reverse print a singular linked list.

Assume that the linked list is ready with 5 elements: 1->2->3->4->5

I had written a program to print in reverse order like: 5 4 3 2 1
But my program just prints as 5 4 3 2; the 1 is not printing. Why?

int Reverse_List(abc_t *pNode) {
    abc_t *pTemp;
    int count = 5;

    if (pNode->pNext != NULL) {
        pNode = pNode->pNext;
        Reverse_List(pNode);
        printf("The node is %d\n", pNode->a);
    }
}
2
  • 2
    the 1 is not printing. Why? printf("The node is %d\n",pNode->a); pNode was pNode->pNext Commented Jun 6, 2013 at 11:45
  • 1
    Suggestion: No need of int count and abc_t *pTemp Commented Jun 6, 2013 at 11:45

2 Answers 2

13

Maybe like this

void Reverse_List(abc_t *pNode){
    if(pNode==NULL)
        return;

    Reverse_List(pNode->pNext);
    printf("The node is %d\n", pNode->a);
}
Sign up to request clarification or add additional context in comments.

Comments

3

see how call works

you have list 1->2->3->4->5

now recursion start as

pnode = 1;    pnode->next=2; (!NULL)      (print 2)   
  pnode=2;    pnode->next=3; (!NULL)      (print 3)   
    pnode=3;    pnode->next=4 (!NULL)     (print 4)   
       pnode=4;   pnode->next=5 (!NULL)   (print 5)   
          pnode=5;   pnode->next==NULL

so here as you can see when you have one it will print two as you advance your pointer

for correct code see @BLUEPIXY's answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.