-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtraction.c
More file actions
136 lines (122 loc) · 3.29 KB
/
subtraction.c
File metadata and controls
136 lines (122 loc) · 3.29 KB
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
/*******************************************************************************************************************************************************************
*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 subtraction(Dlist **head1, Dlist **tail1, Dlist **head2, Dlist **tail2, Dlist **headR,Dlist **tailR)
{
/* Definition goes here */
//check if list is empty or not
if(*head1==NULL || *head2==NULL)
{
return FAILURE;
}
/*
else if((*head1)->data == 0 && (*head1)->next == NULL)
{
(*headR)=(*head2);
return SUCCESS;
}
else if((*head2)->data == 0 && (*head2)->next == NULL)
{
(*headR) = (*head1);
return SUCCESS;
}*/
//2 pointers
Dlist *temp1 = *head1;
Dlist *temp2 = *head2;
int count1=1,count2=1;
while(temp1->next!=NULL)
{
count1++;
temp1 = temp1->next;
}
while(temp2->next!=NULL)
{
count2++;
temp2 = temp2->next;
}
if(count1 <count2)
{
temp1 = *tail2;
temp2 = *tail1;
}
else
{
temp1 = *tail1;
temp2 = *tail2;
}
while(temp1!=NULL && temp2!=NULL)
{
//create node
Dlist *new = malloc(sizeof(Dlist));
if(new == NULL)
return FAILURE;
new->next = NULL;
new->prev = NULL;
if(temp1->data >=temp2->data)
{
new->data = temp1->data - temp2->data;
}else
{
Dlist *temp_carr = temp1->prev;
while(temp_carr ->data == 0)
{
temp_carr->data = temp_carr->data + 9;
temp_carr = temp_carr->prev;
}
temp1->data = temp1->data + 10;
(temp_carr)->data = (temp_carr)->data - 1;
new->data = temp1->data - temp2->data;
}
if(*headR == NULL)
{
*headR = new;
*tailR = new;
}
else
{
new->next = (*headR);
(*headR)->prev = new;
(*headR)=new;
}
temp1=temp1->prev;
temp2=temp2->prev;
}
if(temp1!=NULL)
{
while(temp1!=NULL)
{
//create node
Dlist *new = malloc(sizeof(Dlist));
if(new == NULL)
return FAILURE;
new->next = NULL;
new->prev = NULL;
new->data = temp1->data;
new->next =(*headR);
(*headR)->prev = new;
(*headR) = new;
temp1 = temp1->prev;
}
}
while((*headR)->data==0)
{
(*headR) = (*headR)->next;
free((*headR)->prev);
}
if(count2>count1)
{
(*headR)->data = (-1)*((*headR)->data);
}
return SUCCESS;
}