-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddListBasedInteger.cpp
More file actions
114 lines (102 loc) · 1.69 KB
/
Copy pathAddListBasedInteger.cpp
File metadata and controls
114 lines (102 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include <vector>
using namespace std;
struct node{
int data;
node *next;
};
void insertnode(node *head,int val){
while(head->next!=0){
head=head->next;
}
head->next=new node;
head->next->next=0;
head->next->data=val;
//cout<<" Insertion done!";
}
void calc(node *h1,node *h2){
int carry=0;
node *t1=h1;
node *t2=h2;
node *t3=new node;
int flagt1=1,flagt2=1;
t3->next=0;
int count=0;
int sum;
do{
sum=carry;
if(flagt1==1){
sum+=t1->data;
if(t1->next==0)
flagt1=0;
t1=t1->next;
}
if(flagt2==1){
sum+=t2->data;
if(t2->next==0)
flagt2=0;
t2=t2->next;
}
carry=sum/10;
sum=sum%10;
if(count==0){
t3->data=sum;
count=1;
}else{
insertnode(t3,sum);
}}while(flagt1==1 || flagt2==1);
if(carry!=0){
insertnode(t3,carry);
}
//cout<<" Im here";
node *t4=t3;
while(t4->next!=0){
cout<<t4->data<<"->";
t4=t4->next;
}
cout<<t4->data;
cout<<endl;
}
int main(){
node *head=new node;
head->data=3;
head->next=0;
insertnode(head,1);
insertnode(head,4);
insertnode(head,1);
insertnode(head,4);
insertnode(head,3);
insertnode(head,9);
insertnode(head,9);
insertnode(head,5);
insertnode(head,1);
insertnode(head,5);
node *tg=head;
while(tg->next!=0){
cout<<tg->data<<" ";
tg=tg->next;
}
cout<<tg->data<<"\n";
//cout<<endl;
int gh;
/*cin>>gh;*/
node *head1=new node;
head1->data=7;
head1->next=0;
insertnode(head1,0);
insertnode(head1,9);
insertnode(head1,1);
insertnode(head1,5);
insertnode(head1,6);
insertnode(head1,7);
insertnode(head1,8);
node *tg1=head1;
while(tg1->next!=0){
cout<<tg1->data<<" ";
tg1=tg1->next;
}
cout<<tg->data<<"\n";
//cin>>gh;
calc(head,head1);
return 0;
}