#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
char ch;
struct node *ptr;
}*tp,*sp;
int c = 0;
int insert(struct node *list)
{
int n,num,hd;
scanf("%d",&n);
hd = n;
struct node *temp;
n--;
while(n--)
{
scanf("%d",&num);
list->ch = num;
temp = (struct node *)malloc(sizeof(struct node));
list->ptr = temp;
list = list->ptr;
}
scanf("%d",&num);
list->ch = num;
return hd;
}
void add(struct node *list1,struct node *list2)
{
if((list1 && list1->ptr) && (list2 && list2->ptr))
add(list1->ptr,list2->ptr);
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if((c + (list1->ch + list2->ch)) >= 10){
tp->ch = (c + (list1->ch+list2->ch)) -10;
c = 1;
tp ->ptr = temp;
tp = tp->ptr;
}
else{
tp->ch = (c + list1->ch + list2->ch);
c = 0;
tp->ptr = temp;
tp = tp->ptr;
}
}
void print(struct node *list)
{
if(list->ptr)
{
print(list->ptr);
printf("%d ",list->ch);
}
else if(list->ch)
printf("%d ",list->ch);
}
int main()
{
int pos1,pos2,x;
struct node *list1,*list2,*temp;
list1 = (struct node *)malloc(sizeof(struct node));
list2 = (struct node *)malloc(sizeof(struct node));
temp = (struct node *)malloc (sizeof(struct node));
sp = (struct node *)malloc(sizeof(struct node));
sp = temp;
pos1 = insert(list1);
pos2 = insert(list2);
if(pos1>pos2)
x = pos1-pos2-1;
else
x = pos2-pos1-1;
while((x--) > 0){
temp->ch = 0;
tp = (struct node *)malloc(sizeof(struct node));
temp->ptr = tp;
temp = temp->ptr;
}
if(pos2 > pos1){
temp->ch = 0;
temp->ptr = list1;
list1 = sp;
}
else if(pos1 > pos2){
temp->ch =0;
temp->ptr = list2;
list2 = sp;
}
tp = (struct node *)malloc(sizeof(struct node));
sp = tp;
add(list1,list2);
tp->ch = c;
print(sp);
printf("\n");
return 0;
}
On Tue, Jul 19, 2011 at 1:10 AM, SkRiPt KiDdIe <[email protected]>wrote:
> #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.
>
--
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.