-
Notifications
You must be signed in to change notification settings - Fork 0
/
mul_2ll.c
87 lines (87 loc) · 1.47 KB
/
mul_2ll.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
#include<stdio.h>
#include<malloc.h>
typedef struct ll
{
int data;
struct ll *next;
}node;
node* gn(int data)
{
node *x=(node*)malloc(sizeof(node));
x->data=data;
return x;
}
void append(node **h,int data)
{
node *x=gn(data);
x->next=NULL;
if(*h==NULL)
{
*h=x;
}
else
{
node *t=*h;
while(t->next!=NULL)
t=t->next;
t->next=x;
}
}
int count(node *h)
{
int c=0;
while(h!=NULL)
{
c++;
h=h->next;
}
return c;
}
long mul(node *h,long c1)
{
long z;
if(h==NULL)
return 0;
z=mul(h->next,c1/10);
return (z+(h->data)*c1);
}
long multiplyTwoLists (node* l1, node* l2)
{
long c2=1,c1=1,m1,m2,mult;
int c=count(l1);
while(--c)
{
c1*=10;
}
m1=mul(l1,c1);
c=count(l2);
while(--c)
{
c2*=10;
}
m2=mul(l2,c2);
printf("m1=%ld m2=%ld\n",m1,m2);
mult=m1*m2;
return mult;
}
int main()
{
int data,n;
node *h1=NULL,*h2=NULL;
printf("Enter the size of 1st linked list : ");
scanf("%d",&n);
while(n--)
{
scanf("%d",&data);
append(&h1,data);
}
printf("Enter the size of 2nd linked list : ");
scanf("%d",&n);
while(n--)
{
scanf("%d",&data);
append(&h2,data);
}
printf("multiplied value : %ld",multiplyTwoLists (h1,h2));
return 0;
}