-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
247 lines (206 loc) · 4.8 KB
/
functions.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include<stdio.h>
#include<stdlib.h>
#include "header.h"
//function to get the number and store them minto a linked list
int getnumber(struct node **head)
{
char ch;
int digit;
int count = 0;
struct node *temp, *rear = NULL;
ch = getchar();
while (ch != '\n')
{
digit = ch-'0';
temp = (struct node *)malloc(sizeof(struct node));
temp->num = digit;
temp->next = NULL;
temp->prev=NULL;
count++;
if ((*head) == NULL)
{
*head = temp;
rear = temp;
}
else
{
rear->next = temp;
temp->prev=rear;
rear = rear->next;
}
ch = getchar();
}
return count;
}
//Function to display the numbers stored in the linked list
void display (struct node *head)
{
while (head != NULL)
{
printf("%d", head->num);
head = head->next;
}
printf("\n");
}
//Function to free the linked list
void release (struct node **head)
{
struct node *temp = *head;
while ((*head) != NULL)
{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}
//Function to insert a digit to the front of a linked list
void insert_front(struct node **p, int x)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->num=x;
temp->prev=temp->next=NULL;
if(*p==NULL)
{
*p=temp;
}
else
{
temp->next=*p;
(*p)->prev=temp;
*p=temp;
}
}
//Funciton to add 2 numebrs stored in p and q, and store the result in r
struct node* add(struct node **phead,struct node **qhead,struct node *r)
{
//struct node *r = NULL;
//printf("inside add");
struct node *ptemp; struct node *qtemp;
ptemp=*phead;
qtemp=*qhead;
//Go to the last digits, i.e. units place
while(ptemp->next!=NULL)
{
ptemp=ptemp->next;
}
while(qtemp->next!=NULL)
{
qtemp=qtemp->next;
}
//Initialize the carry over to zero. Add the digits from right to lift, updating the carry-overs on the way
int car=0;
int sum,sum1;
while(ptemp!=NULL && qtemp!=NULL)
{
sum=ptemp->num + qtemp->num + car;
sum1=sum%10;
insert_front(&r,sum1);
car=sum/10;
ptemp=ptemp->prev;
qtemp=qtemp->prev;
}
//Take care of boundary cases where either p or q is a shorter number than the other
if(ptemp!=NULL && qtemp==NULL)
{
while(ptemp!=NULL)
{
sum=ptemp->num + car;
sum1=sum%10;
insert_front(&r,sum1);
car=sum/10;
ptemp=ptemp->prev;
}
}
else if(ptemp==NULL && qtemp!=NULL)
{
while(qtemp!=NULL)
{
sum=qtemp->num + car;
sum1=sum%10;
insert_front(&r,sum1);
car=sum/10;
qtemp=qtemp->prev;
}
}
if(car>0)
{
insert_front(&r,car);
}
return r;
}
//Function to multiply 2 numbers stored in p and q, and store the result in s
struct node* mul(struct node **phead,struct node **qhead,struct node *s)
{
struct node *r1 = NULL;
struct node *r2 = NULL;
struct node *ptemp;
struct node *qtemp;
struct node *prear;
// struct node *qrear;
int prod,car,prod1,i;
int count=0;
ptemp=*phead;
qtemp=*qhead;
while(ptemp->next!=NULL)
{
ptemp=ptemp->next;
}
prear=ptemp;
while(qtemp->next!=NULL)
{
qtemp=qtemp->next;
}
// qrear=qtemp;
insert_front(&r1,0);
while(qtemp!=NULL)
{
car=0;
r2=NULL;
ptemp=prear;
for(i=0;i<count;i++)
{
insert_front(&r2,0);
}
while (ptemp!=NULL)
{
//printf("inside 2nd while");
prod=((qtemp->num)*(ptemp->num))+car;
prod1=prod%10;
car=prod/10;
insert_front(&r2,prod1);
ptemp=ptemp->prev;
}
if(car>0)
{
insert_front(&r2,car);
}
r1=add(&r1,&r2,s);
// display(r1);
//display(r2);
qtemp=qtemp->prev;
count++;
}
s=r1;
return s;
#if 0
if(qtemp->prev!=NULL)
{
qtemp=qtemp->prev;
insert_front(&s,0);
ptemp=prear;
car=0;
while (ptemp!=NULL)
{
prod=((qtemp->num)*(ptemp->num))+car;
prod1=prod%10;
car=prod/10;
insert_front(&s,prod1);
ptemp=ptemp->prev;
}
}
display(r1);
display(r2);
//return 0;
#endif
}