#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=NULL,*end,*temp; int carry=0; while(a) { temp=(node*)malloc(sizeof(node)); temp->val=(a->val+b->val+carry)%10; carry=(a->val+b->val+carry)/10; a=a->next;b=b->next; if(!c)c=end=temp; else end=end->next=temp; } while(carry) { temp=(node*)malloc(sizeof(node)); temp->val=carry%10;temp->next=NULL; carry/=10; end=end->next=temp; } return c; } int main() { node *list1,*list2,*list3,*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); 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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.