-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
multiplyll.c
123 lines (122 loc) · 2.62 KB
/
multiplyll.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node{
int data;
struct Node*next;
};
void swapPointer(struct Node**a,struct Node**b)
{
struct Node*t=*a;
*a=*b;
*b=t;
}
void push(struct Node**head,int new_data)
{
struct Node* new_node=(struct Node*)malloc(sizeof(struct Node));
new_node->data=new_data;
new_node->next=*head;
*head=new_node;
}
void printList(struct Node*node)
{
while(node!=NULL)
{
printf("%d",node->data);
node=node->next;
}
printf("\n");
}
int getSize(struct Node*node)
{
int size=0;
while(node!=NULL)
{
node=node->next;
size++;
}
return size;
}
void reverse(struct Node**head)
{
struct Node*pre=NULL;
struct Node*next=NULL;
struct Node*current=*head;
while(current!=NULL)
{
next=current->next;
current->next=pre;
pre=current;
current=next;
}
*head=pre;
}
struct Node*make_empty_list(int size)
{
struct Node*head=NULL;
while(size--)
{
push(&head,0);
}
return head;
}
struct Node* multiply(struct Node*head1,struct Node*head2)
{
int size1=getSize(head1);
int size2=getSize(head2);
if(size1<size2)
{
swapPointer(&head1,&head2);
}
reverse(&head1);
reverse(&head2);
struct Node*result=make_empty_list(size1+size2+1);
struct Node*head2_ptr=head2,*result_ptr1=result,*result_ptr2,*head1_ptr;
while(head2_ptr)
{
int carry=0;
result_ptr2=result_ptr1;
head1_ptr=head1;
while(head1_ptr)
{
int mul=head1_ptr->data*head2_ptr->data+carry;
result_ptr2->data+=mul%10;
carry=mul/10+result_ptr2->data/10;
result_ptr2->data=result_ptr2->data%10;
head1_ptr=head1_ptr->next;
result_ptr2=result_ptr2->next;
}
if(carry)
{
result_ptr2->data+=carry;
}
result_ptr1=result_ptr1->next;
head2_ptr=head2_ptr->next;
}
reverse(&result);
reverse(&head1);
reverse(&head2);
while(result->data==0)
{
struct Node*temp=result;
result=result->next;
free(temp);
}
return result;
}
int main(void)
{
char*num1=malloc(255*sizeof(char));
char*num2=malloc(255*sizeof(char));
scanf("%s %s",num1,num2);
struct Node*head1=NULL;
struct Node*head2=NULL;
int size1=strlen(num1),size2=strlen(num2);
for(int i=size1-1;i>=0;i--)
push(&head1,num1[i]-'0');
for(int i=size2-1;i>=0;i--)
push(&head2,num2[i]-'0');
struct Node*result=multiply(head1,head2);
printList(result);
return 0;
}