Reversing linked list works.. A followup question will be without using the reversal...For that we should first traverse the longest linked list such a way that it's remaining length is equal to other linked list and multiply using recursion.. After that based on carry.. you should repeat again for leading nodes in larger linked list.. hope i made my point clear..
On Mon, Jul 18, 2011 at 2:39 AM, Piyush Sinha <[email protected]>wrote: > > check my code below...it works for all cases.... > * > **node *MUL(node *h1,node *h2) > { > node *h3,*p,*r; > h1 = reverse(h1); > h2 = reverse(h2); > h3 = multiply(h1,h2->data); > h2 = h2->next; > p = h3; > while(h2) > { > r = multiply(h1,h2->data); > p->next = add(p->next,r); > p = p->next; > h2 = h2->next; > } > h3 = reverse(h3); > return h3; > } > > **node *multiply(node *h,int x) > { > node *head = NULL; > node *p; > int mul,carry=0; > while(h) > { > if(!head) > { > head = (node *)malloc(sizeof(node)); > mul = (x*(h->data)); > carry = mul/10; > mul=mul%10; > head->data=mul; > head->next = NULL; > p = head; > } > else > { > p->next = (node *)malloc(sizeof(node)); > p = p->next; > p->next = NULL; > mul = (x*(h->data)); > p->data = (mul%10)+carry; > carry = mul/10; > } > h = h->next; > } > if(carry) > { > p->next = (node *)malloc(sizeof(node)); > p = p->next; > p->next = NULL; > p->data = carry; > } > return head; > } > > node * add(node *h1,node *h2) > { > node *h3 = NULL; > node *p; > int sum,carry = 0; > while(h1) > { > sum = h1->data+h2->data+carry; > carry = sum/10; > sum = sum%10; > if(h3==NULL) > { > h3 = (node *)malloc(sizeof(node)); > h3->data = sum; > h3->next = NULL; > p =h3; > } > else > { > p->next = (node *)malloc(sizeof(node)); > p = p->next; > p->next = NULL; > p->data = sum; > } > h1 = h1->next; > h2 = h2->next; > } > while(h2) > { > p->next= (node *)malloc(sizeof(node)); > p = p->next; > p->next = NULL; > sum = h2->data + carry; > carry = sum/10; > sum = sum%10; > p->data = sum; > h2 = h2->next; > } > if(carry) > { > p->next = (node *)malloc(sizeof(node)); > p = p->next; > p->next = NULL; > p->data = carry; > } > return h3; > } > > * > > On Mon, Jul 18, 2011 at 2:34 AM, aditi garg <[email protected]>wrote: > >> and 6 is carry forwarded??? >> next node wud be 6*7=42+6=48 >> 8 and 4 carry? >> >> >> On Mon, Jul 18, 2011 at 2:28 AM, hary rathor <[email protected]>wrote: >> >>> sorry 7*9=63 >>> >>> put 3 in list 3 >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >>> >> >> >> -- >> Aditi Garg >> Undergraduate Student >> Electronics & Communication Divison >> NETAJI SUBHAS INSTITUTE OF TECHNOLOGY >> Sector 3, Dwarka >> New Delhi >> >> 9718388816 >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > *Piyush Sinha* > *IIIT, Allahabad* > *+91-7483122727* > * <https://www.facebook.com/profile.php?id=100000655377926> "NEVER SAY > NEVER" > * > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
