sir i am not able to understand the working of this code please help
Explain how this code is working
actually sir last time i was not able to drag the image so i posted it empty and thank you for responding to this post this is the code in the image
its just reversing the doubly linked list.
focus on these lines->
while(current!=NULL){
temp=current->prev;
current->prev=current->next;
current->next=temp;
current=current->prev;
}
here they are swapping previous node with next node.
......
temp=current->prev;//storing previous node address in temp
current->prev=current->next //putting next node in previous
current->next=temp;// since temp has address of previous node, this line will put previous node in next.
current=current->prev ; //going to next node, since address of next node is now in prev , we are doing current->prev
they are just repeating the same on evry node and assining the lead node as head,as a result u will get reversed linked list.
for clarity run this code ->
sir what does this line exactly means :
node* current = *head_ref ;
what is the compiler actually doing while compiling this statement of code?
in function they have sent pointer of pointer so to get the head node we need to dereference the pointer they have sent in function.
so in this line
node *current=*head_ref ; // we are putting address of head node to current pointer.
look at the code that i have shared that will avoid such confusion, as i have used familar syntaxes
yes sir i saw the code
it means that
node* current = head;
is same as
node current =head;?
no they are not same ,
infact this is not possible at all. how u can store head (a pointer) to current (an object).
sir i dont know today my pc is not working properly so typing errors are occuring i am sorry for that, i was asking that
node* current =head ;
is same as
node* current = *head; ?
no bro. . . .they are not, first look at the argumetnts of the function.
in quiz they are giving pointer of a pointer of list head (**head)
whereas in the code(that i shared) has a pointer of list head(*head).
they are differnet right?
in first case ,to get the head pointer u need to derefernce the given pointer.
whereas in second pointer u already have head pointer so no needof dereference
ok sir, i got it
thanks for explaining this so smoothly : )
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.