-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddLargeNumber.cpp
More file actions
230 lines (185 loc) · 3.79 KB
/
Copy pathAddLargeNumber.cpp
File metadata and controls
230 lines (185 loc) · 3.79 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Name : Annie Bhalla
Roll No : 19HCS4009
Course : Bsc (H) Computer Science
Semester : 3
Title : Implementation of Addition of large numbers using Doubly Linked List
*/
#include<iostream>
#include<string>
using namespace std;
class node
{
private:
node *next;
node *prev;
int elem;
public:
friend class DLinkedList;
};
class DLinkedList
{
private:
node* header;
node* trailer;
public:
DLinkedList(); //1
~DLinkedList(); //2
bool empty(); //3
void addFront(const int& e); //4
void addBack(const int& e); //5
void display(); //6
void add(DLinkedList& obj); //7
void insert(string S); //8
void delFront(); //9
};
// (1) Constructor
DLinkedList::DLinkedList()
{
header= new node;
trailer= new node;
header->next=trailer;
trailer->next= header;
}
// (2) Destructor
DLinkedList::~DLinkedList()
{
while( !empty()) delFront();
delete header;
delete trailer;
}
// (3) To check whether linked list is empty
bool DLinkedList::empty()
{
if(header->next==trailer)
return 1;
else
return 0;
}
// (4) to add an element in the front
void DLinkedList::addFront(const int& e)
{
node *p= new node;
p->elem=e;
p->prev= header;
p->next= header->next;
header->next->prev= p;
header->next=p;
}
// (5) to add an element at the back
void DLinkedList::addBack(const int& e)
{
node *p = new node;
p->elem= e;
p->next = trailer;
p->prev= trailer->prev;
trailer->prev->next =p;
trailer->prev=p;
}
// (6) display function
void DLinkedList::display()
{
if(empty())
throw " empty list ";
{
node *n=header->next;
int a;
while(n!=trailer){
a=n->elem;
cout<<a<<" -> ";
n=n->next;
}cout<<endl;
}
}
// (7) adding two large numbers
void DLinkedList::add(DLinkedList& obj)
{
DLinkedList D;
int carry=0,sum=0;
if(empty() && obj.empty())
{
cout<<"\n OOPS ! Missing Operands ";
return;
}
if(empty())
obj.display();
else
if(obj.empty())
display();
else
{
node* a= (*this).trailer->prev;
node* b= obj.trailer->prev;
while( a!= header && b!= obj.header)
{
sum = (a->elem) + (b->elem) + carry;
carry= sum/10000;
D.addFront(sum%10000);
a=a->prev;
b=b->prev;
sum=0;
}
if(carry!=0)
{
while( a!= header)
{
int sum1 = a->elem +carry;
carry=sum1/10000;
D.addFront(sum1%10000);
a=a->prev;
}
while( b!=header)
{
int sum1 = b->elem +carry;
carry=sum1/10000;
D.addFront(sum1%10000);
b=b->prev;
}
D.addFront(carry);
}
D.display();
}
}
// (8) to convert the string into a DLL
void DLinkedList::insert(string S)
{
int k=S.length()-1;
while(k>=0)
{
int j=1,t=0, val=0;
while(k>=0 && t<4)
{
val= val + (S[k]- 48) * j;
j= j*10;
t++;
k--;
}
addFront(val);
}
display();
}
// (9 ) delete the elements from the fron
void DLinkedList::delFront()
{
if( empty() ) throw " Empty list ";
node *p= header->next;
header->next = p->next;
p->next->prev = p->prev;
delete p;
}
// driver code
int main()
{
string s1;
string s2;
DLinkedList D1,D2,D3,D4;
cout<<"\n Enter the first number : "<<endl;
cin>>s1;
D1.insert(s1);
cout<<"\n Enter the second number : "<<endl;
cin>>s2;
D2.insert(s2);
cout<<endl<<" ************ SUM ************* "<<endl;
D1.add(D2);
D3.add(D4);
return 0;
}