-
Notifications
You must be signed in to change notification settings - Fork 0
/
question2.c
110 lines (86 loc) · 1.71 KB
/
question2.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
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
void push(struct Node** head,int val){
struct Node *ptr=(struct Node *)malloc(sizeof(struct Node));
ptr->next = *head;
ptr->data=val;
*head=ptr;
}
void lltrav(struct Node *head){
struct Node * p=head;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int length_ll(struct Node *head){
struct Node *ptr=head;
int c = 0;
while(ptr!=NULL){
c++;
ptr=ptr->next;
}
return c;
}
int sumofll(struct Node *h1,struct Node *h2){
int n1 = length_ll(h1);
int n2 = length_ll(h2);
int arr1[n1];
int arr2[n2];
struct Node *ptr1=h1;
struct Node *ptr2=h2;
int i = 0;
while(ptr1!=NULL){
arr1[i]=ptr1->data;
ptr1=ptr1->next;
i++;
}
int j = 0;
while(ptr2!=NULL){
arr2[j]=ptr2->data;
ptr2=ptr2->next;
j++;
}
int k1=0;
for(int i = 0;i<n1;i++){
k1+=arr1[i]*pow(10,n1-i-1);
}
int k2=0;
for(int i = 0;i<n2;i++){
k2+=arr2[i]*pow(10,n2-i-1);
}
int sum=k1+k2;
return sum;
}
int main(){
struct Node *head1=NULL;
struct Node *head2=NULL;
struct Node *head3=NULL;
push(&head1,3);
push(&head1,6);
push(&head1,5);
push(&head2,2);
push(&head2,4);
push(&head2,8);
lltrav(head1);
lltrav(head2);
int s = sumofll(head1,head2);
int dupli=s;
int numdigits=0;
while(dupli!=0){
dupli=dupli/10;
numdigits++;
}
int arrdigits[numdigits];
for(int i = 0;i<numdigits;i++){
push(&head3,s%10);
s=s/10;
}
lltrav(head3);
return 0;
}