The following code works fine when head is sent as a parameter to it. As I am new to C, I couldn\'t understand how it works. Help me out please.
struct node
Let the linked list be 1-> 2 -> 3 ->4
the function in c is--
struct linked_node * reverse_recursive(struct linked_node * head)
{
struct linked_node * first;/*stores the address of first node of the linked
list passed to function*/
struct linked_node * second;/* stores the address of second node of the
linked list passed to function*/
struct linked_node * rev_head;/*stores the address of last node of initial
linked list. It also becomes the head of the reversed linked list.*/
//initalizing first and second
first=head;
second=head->next;
//if the linked list is empty then returns null
if(first=NULL)
return(NULL);
/* if the linked list passed to function contains just 1 element, then pass
address of that element*/
if(second==NULL)
return(first);
/*In the linked list passed to function, make the next of first element
NULL. It will eventually (after all the recursive calls ) make the
next of first element of the initial linked list NULL.*/
first->next=NULL;
/* storing the address of the reverse head which will be passed to it by the
condition if(second==NULL) hence it will store the address of last element
when this statement is executed for the last time. Also here we assume that
the reverse function will yield the reverse of the rest of the linked
list.*/
rev_head=reverse(second);
/*making the rest of the linked list point to the first element. i.e.
reversing the list.*/
second->next=first;
/*returning the reverse head (address of last element of initial linked
list) . This condition executes only if the initial list is 1- not empty
2- contains more than one element. So it just relays the value of last
element to higher recursive calls. */
return(rev_head);
}
now running the function for the linked list 1-> 2-> 3 -> 4
list of function is
1(first)->2(second) -> 3 -> 4
inside reverse(&2)
code runs until rev_head=reverse(&3);
list of function
2(first)->3 (second)-> 4
inside reverse(&3)
code runs until rev_head=reverse (&4);
list if function
3(first)-> 4 (second)
inside reverse(&4) terminating condition second==NULL is true so return is executed and address of 4 is returned.
list of function
4(first)-> NULL(second)
after executing second->next=first; list becomes
NULL<- 3(first) <-4 (second)
return (rev_head ); is executed which passes &4 because rev_head=&4
list in function is
NULL<-2(first) 3(second)<-4
and rev_head is &4 which was returned by rev(&3)
after executing second->next=first , list becomes
NULL<-2(first)<-3(second)<-4
return(rev_head); is executed which returns &4 to rev(&1);
list in function is
NULL<-1(first) 2(second)<-3<-4
and value of rev_head is &4 which was passed by reverse(&3)
now second->next =first is executed and list becomes
NULL<-1(first) <- 2(second)<-3<-4
return(rev_head); is executed // rev_head=&4 which was returned by reverse(&2) and the value of rev_head is passesd to the main function.
hope this helps. It took me quite a lot of time to understand this and also to write this answer.