forked from abbh07/DataStructure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
List-AdditionLongNumbers.cpp
149 lines (143 loc) · 2.48 KB
/
List-AdditionLongNumbers.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
using namespace std;
class node
{
private:
int data;
node* next;
public:
node()
{
next = NULL;
}
node* insert(node*,int);
void display(node*);
node* addList(node*,node*);
};
node* node::insert(node* head,int item)
{
node* temp = new node();
temp->data = item;
temp->next=NULL;
node* h=head;
if(head == NULL)
{
h = head = temp;
return h;
}
else
{
while(head->next!=NULL)
{
head=head->next;
}
head->next = temp;
return h;
}
}
void node::display(node* root)
{
if(root == NULL)
cout<<"Empty List!\n";
else
{
while(root!=NULL)
{
cout<<root->data<<"\t";
root=root->next;
}
cout<<endl;
}
}
node* node::addList(node* root1,node* root2)
{
if(root1 == NULL && root2 == NULL)
return NULL;
if(root1 == NULL)
return root2;
if(root2 == NULL)
return root1;
node* r1 = root1;
node* r2 = root2;
node* temp;
int c=0;
node* root3 = NULL;
node* head3;
while(r1!=NULL && r2!=NULL)
{
temp = new node();
temp->next = NULL;
int val = r1->data+r2->data+c;
if(val<10)
{
temp->data = val;
c=0;
}
else
{
temp->data = val%10;
c=val/10;
}
if(root3 == NULL)
{
head3 = root3 = temp;
}
else
{
root3->next = temp;
root3 = root3->next;
}
r1=r1->next;
r2=r2->next;
}
while(r1!=NULL)
{
temp = new node();
temp->data = r1->data;
temp->next = NULL;
root3->next = temp;
root3 = root3->next;
r1=r1->next;
}
while(r2!=NULL)
{
temp=new node();
temp->data = r2->data;
temp->next =NULL;
root3->next = temp;
root3 = root3->next;
r2=r2->next;
}
return head3;
}
int main()
{
node n;
cout<<"Enter the number of digits in the first number\n";
int s;
cin>>s;
int num;
node *head1 = NULL,*head2 = NULL;
for(int i=0;i<s;i++)
{
cout<<"Enter the first number from lsb to msb\n";
cin>>num;
head1 = n.insert(head1,num);
}
cout<<"Enter the number of digits in the second number\n";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"Enter the second number from lsb to msb\n";
cin>>num;
head2 = n.insert(head2,num);
}
cout<<"First list:\n";
n.display(head1);
cout<<"Second list:\n";
n.display(head2);
cout<<"Addition of the two lists:\n";
node *head3 = n.addList(head1,head2);
n.display(head3);
return 0;
}