Add two link list (don't merge the link list , value should be added)

Question:-You have been given two linked list L1 and L2.
Create another link list L3 adding L1 and L2

Input:
        L1: 1->2->3
        L2: 3->4->5

Output:
        L3: 4->6->8



#include<stdio.h>
#include<stdlib.h>

typedef struct st_tag
{
	int data;
	struct st_tag *next;
}node_type;


node_type *create_link_list(int n);
void print_link_list(node_type *head);
node_type *reverse_link_list(node_type *head);
node_type *add_link_list(node_type *head1 , node_type *head2);

void main()
{
	node_type *head1;
	node_type *head2;
	node_type *head3;
	
	int n1;
	int n2;

	printf("Give the value of n1\n");
	scanf("%d",&n1);
	
	printf("Give the value of n2\n");
	scanf("%d",&n2);

	head1 = create_link_list(n1);
	head2 = create_link_list(n2);

	print_link_list(head1);
        print_link_list(head2);

	printf("The output is\n");
	head3=add_link_list(head1 , head2);
	print_link_list(head3);


}





node_type *add_link_list(node_type *head1 , node_type *head2)
{
	node_type *head3, *temp , *ptr;
	int carry=0;
	int first=0;
	head3=NULL;
	
	head1=reverse_link_list(head1);
	head2=reverse_link_list(head2);

	while(head1 && head2)
	{
		temp=(node_type *)malloc(sizeof(node_type));
		temp->next = NULL;

		if((head1->data + head2->data + carry) <= 9)
		{
			temp->data=head1->data + head2->data + carry;
			carry=0;
		}
		else
		{
			temp->data=(head1->data + head2->data + carry)%10;
			carry=(head1->data + head2->data + carry)/10;
		}
		if(first == 0)
		{
			head3=temp;
			ptr=temp;
			first=1;
		}
		else
		{
			ptr->next = temp;
			ptr=temp;
		}
		head1 = head1->next;
		head2 = head2->next;
	}

	while(head1)
	{
		temp=(node_type *)malloc(sizeof(node_type));
		temp->next = NULL;

		if((head1->data + carry) <= 9)
		{
			temp->data=head1->data + carry;
			carry=0;
		}
		else
		{
			temp->data=(head1->data + carry)%10;
			carry=(head1->data + carry)/10;
		}
		if(first == 0)
		{
			head3=temp;
			ptr=temp;
			first=1;
		}
		else
		{
			ptr->next = temp;
			ptr=temp;
		}
		head1 = head1->next;
	}


	while(head2)
	{
		temp=(node_type *)malloc(sizeof(node_type));
		temp->next = NULL;

		if((head2->data + carry) <= 9)
		{
			temp->data=head2->data + carry;
			carry=0;
		}
		else
		{
			temp->data=(head2->data + carry)%10;
			carry=(head2->data + carry)/10;
		}
		if(first == 0)
		{
			head3=temp;
			ptr=temp;
			first=1;
		}
		else
		{
			ptr->next = temp;
			ptr=temp;
		}
		head2 = head2->next;
	}

	if(carry>0)
	{

		temp=(node_type *)malloc(sizeof(node_type));
		temp->data = carry;
		temp->next = NULL;
		ptr->next = temp;
	}

	head3=reverse_link_list(head3);

	return head3;
}


node_type *reverse_link_list(node_type *head)
{
	node_type *prev , *save;
	prev=NULL;
	if(head==NULL)
		return NULL;
	while(head)
	{
		save=head->next;
		head->next=prev;
		prev=head;
		head=save;
	}
	return prev;
}




void print_link_list(node_type *head)
{
	while(head!=NULL)
	{
		printf("%d-->",head->data);
		head=head->next;
	}
	printf("\n");
}

node_type *create_link_list(int n)
{
	node_type *temp, *head , *ptr;
	int i;
	int a[100];
	if(n==0)
	{
		return (node_type *)NULL;
	}
	else
	{
		printf("Give the element\n");
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}

		for(i=0;i<n;i++)
		{
			temp=(node_type *)malloc(sizeof(node_type));
			temp->data = a[i];
			temp->next = NULL;

			if(i==0)
			{
				head=temp;
				ptr=temp;
			}
			else
			{
				ptr->next=temp;
				ptr=temp;
			}

		}
	}
	return head;
}


Author: Gour Ch. Saha
Contact for any query:: gour_ch_saha@yahoo.co.in
Free Web Hosting