-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddition of two linklist.cpp
More file actions
155 lines (136 loc) · 2.36 KB
/
Addition of two linklist.cpp
File metadata and controls
155 lines (136 loc) · 2.36 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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int data;
struct node*next;
struct node*prev;
}*start1=NULL, *start2=NULL,*start3=NULL;
insert1(int num ) //insert function
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
printf("\nyou node :%d",temp->data);
temp->next=NULL;
temp->prev=NULL;
if(start1== NULL)
{
start1=temp;
}
else
{
struct node *curr=start1;
while (curr->next != NULL)
{
curr=curr->next;
}
curr->next=temp;
temp->prev=curr;
}
}
void insert2 (int num ) //insert function
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
printf("\nyou node :%d",temp->data);
temp->next=NULL;
temp->prev=NULL;
if(start2== NULL)
{
start2=temp;
}
else
{
struct node *curr=start2;
while (curr->next != NULL)
{
curr=curr->next;
}
curr->next=temp;
temp->prev=curr;
}
}
insert3(int num,int carry ) //insert function
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->next=NULL;
temp->prev=NULL;
if(start3== NULL)
{
start3=temp;
}
else
{
struct node *curr=start3;
while (curr->next != NULL)
{
curr=curr->next;
}
curr->next=temp;
temp->prev=curr;
curr->data=curr->data+carry;
}
}
addition()
{
struct node *curr1=start1;
struct node *curr2=start2;
struct node *curr3=start3;
int sum=0,carry=0;
{ if(curr1==start1 && curr2==start2)
{
sum=curr1->data + curr2->data;
insert3(sum,carry);
curr1=curr1->next;
curr2=curr2->next;
}
while(curr1!=NULL && curr2 !=NULL)
{
sum=curr1->data + curr2->data;
carry=sum/10;
if(carry!= 0)
sum=sum%10;
insert3(sum,carry);
curr1=curr1->next;
curr2=curr2->next;
}
}
}
printlist3()
{ struct node*curr=start3;
while (curr->next != NULL)
{
printf("\nYour data is :%d",curr->data);
curr=curr->next;
}
printf("\nYour data is :%d",curr->data);
}
int main()
{
int digit,a;
for(int i=1;i<=2;i++)
{
int x=10000;
printf("\nEnter the value of num:");
scanf("%d",&a);
for(int j=1;j<=5;j++)
{
digit = a / x;
a = a % x;
if(i==1)
insert1(digit);
else
insert2(digit);
x=x/10;
}
}
addition();
printlist3();
return 0;
}