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);
}
Exemple #2
0
void addList(struct node *head1,struct node *head2,struct node **result)
{
  struct node *curr;
  if(head1 == NULL)
  {
    *result = head2;
  }
  if(head2 == NULL)
  {
    *result = head1;
  }
  int size1 =getCount(head1);
  int size2 = getCount(head2);
  int carry = 0;
  if(size1 == size2)
  {
    *result = addSameSize(head1,head2,&carry);
  }
  else
  {
    int diff = abs(size1 - size2);
    if(size1 < size2)
    {
      swap(head1,head2,struct node*);
    }
    for(curr = head1;diff--;curr=curr->next);
    *result = addSameSize(curr,head2,&carry);
    addCarryToRemaing(head1,curr,&carry,result);
  }
  if(carry)
  {
    push(result,carry);
  }
}
Exemple #3
0
// 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);
}
Exemple #4
0
// Adds two linked lists of same size represented by head1 and head2 and returns
// head of the resultant linked list. Carry is propagated while returning from
// the recursion
node* addSameSize(node* head1, node* head2, int* carry)
{
    // Since the function assumes linked lists are of same size,
    // check any of the two head pointers
    if (head1 == NULL)
        return NULL;
 
    int sum;
 
    // Allocate memory for sum node of current two nodes
    node* result = (node *)malloc(sizeof(node));
 
    // Recursively add remaining nodes and get the carry
    result->next = addSameSize(head1->next, head2->next, carry);
 
    // add digits of current nodes and propagated carry
    sum = head1->data + head2->data + *carry;
    *carry = sum / 10;
    sum = sum % 10;
 
    // Assigne the sum to current node of resultant list
    result->data = sum;
 
    return result;
}
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;

}
Node *addSameSize(Node *head1,Node *head2,int *carry){
	if(NULL==head1)
		return NULL;
	int sum;
	Node *result=createNode(TEMP_DATA);
	result->next=addSameSize(head1->next,head2->next,carry);
	sum=head1->data+head2->data+*carry;
	*carry=sum/10;
	sum=sum%10;
	result->data=sum;
	return result;
}
Exemple #7
0
struct node* addSameSize(struct node *head1,struct node *head2,int *carry)
{
  if(head1 == NULL)
  {
    return NULL;
  }
  int sum;
  struct node *result = (struct node*)malloc(sizeof(struct node));
  result->next = addSameSize(head1->next,head2->next,carry);
  sum = head1->data + head2->data + *carry;
  *carry = sum/10;
  sum = sum%10;
  result->data = sum;
  return result;
}
node_t addSameSize(node_t list1,node_t list2,int* carry)
{
        if(list1 == NULL)
        {
                return NULL;
        }

        int sum;

        node_t list3 = (node_t) malloc(sizeof(struct node));

        list3 -> link = addSameSize(list1->link,list2->link,carry);
        
        sum = list1->key + list2->key + *carry;
        *carry = sum / 10;

        sum = sum % 10;

        list3->key = sum;

        return list3;
}