-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddition.c
More file actions
113 lines (94 loc) · 2.88 KB
/
addition.c
File metadata and controls
113 lines (94 loc) · 2.88 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
/*******************************************************************************************************************************************************************
*Title : Addition
*Description : This function performs addition of two given large numbers and store the result in the resultant list.
*Prototype : int addition(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 addition(Dlist **head1, Dlist **tail1, Dlist **head2, Dlist **tail2, Dlist **headR,Dlist **tailR)
{
/* Definition goes here */
//check if list 1 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;
}
int data;
Dlist *temp1= *tail1;
Dlist *temp2= *tail2;
while(temp1 != NULL && temp2 != NULL)
{
//create new node
Dlist *new = malloc(sizeof(Dlist));
if(new == NULL)
{
return FAILURE;
}
new->data = (temp1->data + temp2->data);
new->next = NULL;
new->prev = NULL;
if((*headR) == NULL)
{
*headR = new;
*tailR = new;
}
else
{
(*headR)->prev = new;
new->next = *headR;
*headR = new;
new->data = new->data + ((new->next)->data / 10);
(new->next)->data = (new->next)->data % 10;
}
temp1 = temp1->prev;
temp2 = temp2->prev;
}
if(temp1!=NULL)
{
while(temp1!=NULL)
{
//create new node
Dlist *new = malloc(sizeof(Dlist));
new->next=NULL;
new->prev=NULL;
new->data = temp1->data+((*headR)->data/10);
(*headR)->data = (*headR)->data % 10;
(*headR)->prev = new;
new->next = (*headR);
(*headR) = new;
temp1 = temp1->prev;
}
}
else if(temp2!=NULL)
{
while(temp2!=NULL)
{
//create new node
Dlist *new = malloc(sizeof(Dlist));
new->next=NULL;
new->prev=NULL;
new->data = temp2->data+((*headR)->data/10);
(*headR)->data = (*headR)->data % 10;
(*headR)->prev = new;
new->next = (*headR);
(*headR)=new;
temp2 = temp2->prev;
}
}
return SUCCESS;
}