-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtraction.c
109 lines (99 loc) · 2.64 KB
/
subtraction.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
/*******************************************************************************************************************************************************************
*Title : Subtraction
*Description : This function performs subtraction of two given large numbers and store the result in the resultant list.
*Prototype : int subtraction(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_first(Dlist **head, Dlist **tail)
{
//checking for empty list
if(*head==NULL || *tail==NULL)
return FAILURE;
//checking for single node
if((*head)->next==NULL)
{
free((*head));
*head=*tail=NULL;
return SUCCESS;
}
//logic
*head=(*head)->next;
free((*head)->prev);
(*head)->prev=NULL;
}
int subtraction(Dlist **head1, Dlist **tail1, Dlist **head2, Dlist **tail2, Dlist **headR,Dlist **tailR)
{
/* Definition goes here*/
Dlist *t1=*tail1,*t2=*tail2;
int flag=0,num;
while(t1!=NULL && t2!=NULL)
{
if(flag==1)
t1->data--;
if(t1->data < t2->data)
{
num=10+t1->data-t2->data;
flag=1;
// printf("%d %d\n",t1->data,t2->data,num);
}
else
{
num=t1->data-t2->data;
flag=0;
// printf("%d %d\n",t1->data,t2->data,num);
}
dl_insert_first(headR,tailR,num);
t1=t1->prev;
t2=t2->prev;
}
if(t2==NULL&&t1!=NULL)
{
while(t1!=NULL)
{
if(flag==1)
{
if(t1->data==0)
{
flag=1;
t1->data--;
num=t1->data+10;
dl_insert_first(headR,tailR,num);
}
else
{
flag=0;
t1->data--;
dl_insert_first(headR,tailR,t1->data);
}
}
else
{
dl_insert_first(headR,tailR,t1->data);
}
t1=t1->prev;
}
}
if((*headR)->next!=NULL)
{
while((*headR)->data==0)
{
dl_delete_first(headR,tailR);
if((*headR)==(*tailR))
{
break;
}
}
}
if(*headR==*tailR && (*headR)->data==0)
return 0;
else
return 1;
}