-
Notifications
You must be signed in to change notification settings - Fork 0
/
division.c
139 lines (120 loc) · 2.94 KB
/
division.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
/*******************************************************************************************************************************************************************
*Title : Division
*Description : This function performs division of two given large numbers and store the result in the resultant list.
*Prototype : int division(Dlist **head1, Dlist **tail1, Dlist **head2, Dlist **tail2, Dlist **headR);
*Input Parameters : head1: Pointer to the first node of the first double linked list.
: tail1: Pointer to the last node of the first double linked list.
: head2: Pointer to the first node of the second double linked list.
: tail2: Pointer to the last node of the second double linked list.
: headR: Pointer to the first node of the resultant double linked list.
*Output : Status (SUCCESS / FAILURE)
*******************************************************************************************************************************************************************/
#include "apc.h"
#include <stdio.h>
#include <stdlib.h>
int dl_delete_list(Dlist **head, Dlist **tail)
{
if(*head==NULL || *tail==NULL)
return FAILURE;
while((*head)->next!=NULL)
{
*head=(*head)->next;
free((*head)->prev);
}
free((*head));
*head=*tail=NULL;
}
int division(Dlist **head1, Dlist **tail1, Dlist **head2, Dlist **tail2, Dlist **headR,Dlist **tailR, char *argv[])
{
/* Definition goes here */
int signvar=0;
if(argv[1][0]=='-')
signvar++;
if(argv[3][0]=='-')
signvar++;
if((*head2)->data==0 && (*head2)->prev==NULL && (*head2)->prev==NULL)
{
printf("NOT DEFINED / INFINITY\n");
return 0;
}
int c1=0,c2=0,q=1;
Dlist *t1=*head1;
Dlist *t2=*head2;
while(t1 != NULL)
{
t1=t1->next;
c1++;
}
while(t2 != NULL)
{
t2=t2->next;
c2++;
}
t1=*head1;
t2=*head2;
if(c1 == c2)
{
while(t1!=NULL && t2!= NULL && t1->data==t2->data){
t1=t1->next;
t2=t2->next;
}
if(t1!=NULL && t2!=NULL){
if((t2)->data > (t1)->data)
{
printf("0\n");
return FAILURE;
}
}
}
else if(c2 > c1)
{
printf("0\n");
return FAILURE;
}
while(1)
{
c1=0,c2=0;
subtraction(head1,tail1,head2,tail2,headR,tailR);
dl_delete_list(head1,tail1);
*head1=*headR;
*tail1=*tailR;
*headR=NULL;
*tailR=NULL;
t1=*head1;
t2=*head2;
while(t1!=NULL)
{
t1=t1->next;
c1++;
}
while(t2!=NULL)
{
t2=t2->next;
c2++;
}
t1=*head1;
t2=*head2;
if(c1==c2)
{
while(t1!=NULL && t2!=NULL && t1->data==t2->data){
t1=t1->next;
t2=t2->next;
}
if(t1!=NULL&&t2!=NULL){
if((t1)->data < (t2)->data)
{
break;
}
}
}
else if(c1 < c2)
{
break;
}
q++;
}
if(signvar==1)
printf("-");
printf("%d\n",q);
return 0;
}