-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog5.c
131 lines (116 loc) · 2.79 KB
/
prog5.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node{
int data;
struct node *addr;
};
typedef struct node *NODE;
void display(NODE head){
NODE temp;
if(head->addr == head){
printf("List is empty!!");
return;
}
temp = head->addr;
while(temp != head){
printf("%d", temp->data);
temp = temp->addr;
}
}
NODE insertbegin(NODE head, int item){
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp->data = item;
if(head->addr == head){
head->addr = temp;
temp->addr = head;
return head;
}
temp->addr = head->addr;
head->addr = temp;
return head;
}
NODE insertend(NODE head, int item){
NODE temp, cur;
temp = (NODE)malloc(sizeof(struct node));
temp->data = item;
if(head->addr == head){
head->addr = temp;
temp->addr = head;
return head;
}
cur = head->addr;
while(cur->addr != head){
cur = cur->addr;
}
cur->addr = temp;
temp->addr = head;
return head;
}
void addzero(NODE head1, NODE head2){
int ct1, ct2;
ct1 = head1->data;
ct2 = head2->data;
if(ct1 > ct2){
for(int i=0; i<(ct1 - ct2); i++){
head2 = insertend(head2, 0);
}
}
if(ct1 < ct2){
for(int i=0; i<(ct2 - ct1); i++){
head1 = insertend(head1, 0);
}
}
}
void add(NODE head1, NODE head2){
NODE head, t1, t2;
int sum = 0, carry = 0, x;
head = (NODE)malloc(sizeof(struct node));
head->addr = head;
t1 = head1->addr;
t2 = head2->addr;
while(t1 != head1 && t2 != head2){
x = t1->data + t2->data + carry;
sum = x % 10;
carry = x/10;
head = insertbegin(head, sum);
t1 = t1->addr;
t2 = t2->addr;
}
if(carry > 0){
head = insertbegin(head, carry);
}
printf("\nFinal sum is: ");
display(head);
printf("\n");
}
int main(){
char first[20], second[20];
NODE head1, head2;
head1 = (NODE)malloc(sizeof(struct node));
head2 = (NODE)malloc(sizeof(struct node));
head1->addr = head1;
head2->addr = head2;
printf("Enter the first number: ");
scanf("%s", &first);
printf("\nThe first number is: ");
for(int i=0; first[i]!= '\0'; i++){
head1 = insertbegin(head1, first[i] - '0');
printf("%d", first[i] - '0');
}
printf("\n");
head1->data = strlen(first);
printf("\nEnter the second number: ");
scanf("%s", &second);
printf("\nThe second number is: ");
for(int i=0; second[i]!= '\0'; i++){
head2 = insertbegin(head2, second[i] - '0');
printf("%d", second[i] - '0');
}
printf("\n");
head2->data = strlen(second);
addzero(head1, head2);
add(head1, head2);
return 0;
}