#include<cstdlib>
#include<iostream>
using namespace std;

struct node{
        int val;
        node *next;
    };

node * Input(int n)
{
    node *temp,*list,*end;

    int x;
    for(int i=0;i<n;i++)
        {
                cin>>x;

temp=(node*)malloc(sizeof(node));temp->val=x;temp->next=NULL;

                if(!i)list=end=temp;
                else end=end->next=temp;
        }
    return list;
}
void Rev(node *&head,node *add)
{
    if(!head){head=add;return;}
    node *t=head;head=head->next;
    Rev(head,t);
    t->next=add;
}
void Show(node *head)
{
    while(head){cout<<head->val<<" ";head=head->next;}
    cout<<"\n\n";
}
node * Add(node* a, node *b,node *c,int carry)
{
    node *temp;

    if(!a)
    {
        while(carry)
        {
                    temp=(node*)malloc(sizeof(node));
                    temp->val=carry%10;temp->next=NULL;
                    carry/=10;
                    c=c->next=temp;
        }return c;
    }

    temp=(node*)malloc(sizeof(node));
        temp->val=(a->val+b->val+carry)%10;

    if(!c)c=temp;
    else c=c->next=temp;

    Add(a->next,b->next,c,(a->val+b->val+carry)/10);
    return c;
}
int main()
{

    node *list1,*list2,*list3=NULL,*temp;

    int n1,n2;

    cin>>n1;
    list1=Input(n1);//Input list 1
    cin>>n2;
    list2=Input(n2);//Input list 2


if(n2>n1)while((n2--)-n1){temp=(node*)malloc(sizeof(node));temp->val=0;temp->next=list1;list1=temp;}
    else
while((n1--)-n2){temp=(node*)malloc(sizeof(node));temp->val=0;temp->next=list2;list2=temp;}


    Rev(list1,NULL);
    Rev(list2,NULL);
    list3=Add(list1,list2,list3,0);
    Rev(list3,NULL);
    Show(list3);
    return 0;
}

-- 
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.

Reply via email to