void add(Node *head1,Node *head2,Node **result){ if(NULL==head1){ *result=head2; return; } if(NULL==head2){ *result=head1; return; } int l1=size(head1); int l2=size(head2); Node *curr; int carry=0,i; if(l1==l2){ *result=addSameSize(head1,head2,&carry); }else{ int diff=abs(l1-l2); if(l1<l2) swap(&head1,&head2); for(curr=head1;diff--;curr=curr->next); *result=addSameSize(curr,head2,&carry); addCarryToRemaining(head1,curr,&carry,result); } if(carry) push(result,carry); }
void addCarryToRemaining(Node *head1,Node *curr,int *carry,Node **result){ int sum; if(head1!=curr){ addCarryToRemaining(head1->next,curr,carry,result); sum=head1->data+*carry; *carry=sum/10; sum%=10; push(result,sum); } }
// The main function that adds two linked lists represented by head1 and head2. // The sum of two lists is stored in a list referred by result void addList(node* head1, node* head2, node** result) { node *cur; // first list is empty if (head1 == NULL) { *result = head2; return; } // second list is empty else if (head2 == NULL) { *result = head1; return; } int size1 = getSize(head1); int size2 = getSize(head2) ; int carry = 0; // Add same size lists if (size1 == size2) *result = addSameSize(head1, head2, &carry); else { int diff = abs(size1 - size2); // First list should always be larger than second list. // If not, swap pointers if (size1 < size2) swapPointer(&head1, &head2); // move diff. number of nodes in first list for (cur = head1; diff--; cur = cur->next); // get addition of same size lists *result = addSameSize(cur, head2, &carry); // get addition of remaining first list and carry addCarryToRemaining(head1, cur, &carry, result); } // if some carry is still there, add a new node to the fron of // the result list. e.g. 999 and 87 if (carry) push(result, carry); }
node_t add_two_lists(node_t list1,node_t list2,node_t list3) { node_t cur; if(list1 == NULL) { return list2; } else if(list2 == NULL) { return list1; } int length1 = getLength(list1); int length2 = getLength(list2); int carry = 0; if(length1 == length2) { list3 = addSameSize(list1,list2,&carry); } else { int difference = abs(length1 - length2); if(length1 < length2) { swapPointers(&list1,&list2); } for(cur = list1; difference--; cur = cur -> link); list3 = addSameSize(cur,list2,&carry); addCarryToRemaining(list1,cur,&carry,&list3); } if(carry != 0) { node_t temp = (node_t)malloc(sizeof(struct node)); temp -> key = carry; temp -> link = list3; list3 = temp; } return list3; }
// This function is called after the smaller list is added to the bigger // lists's sublist of same size. Once the right sublist is added, the carry // must be added toe left side of larger list to get the final result. void addCarryToRemaining(node* head1, node* cur, int* carry, node** result) { int sum; // If diff. number of nodes are not traversed, add carry if (head1 != cur) { addCarryToRemaining(head1->next, cur, carry, result); sum = head1->data + *carry; *carry = sum/10; sum %= 10; // add this node to the front of the result push(result, sum); } }
void addCarryToRemaining(node_t list1,node_t cur,int* carry,node_t* list3) { int sum; if(list1 != cur) { addCarryToRemaining(list1->link,cur,carry,list3); sum = list1->key + *carry; *carry = sum / 10; sum = sum % 10; node_t temp = (node_t)malloc(sizeof(struct node)); temp -> key = sum; temp -> link = *list3; *list3 = temp; } }