forked from tirthasheshpatel/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_addition.c
166 lines (158 loc) · 4.15 KB
/
polynomial_addition.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
typedef struct node* iterator;
typedef struct node
{
int coef;
int expo;
struct node* next;
}Node;
iterator create_singly_linked_list(int coef, int expo)
{
iterator head = (iterator)malloc(sizeof(Node));
head->coef = coef;
head->expo = expo;
head->next = 0;
return head;
}
iterator insert_inorder(int coef, int expo, iterator head)
{
iterator it = head;
iterator prev = 0;
if(head->expo < expo)
{
iterator new = (iterator)malloc(sizeof(Node));
new->coef = coef;
new->expo = expo;
new->next = head;
head = new;
return head;
}
while(it->expo > expo)
{
if(it->next == 0)
{
if(it->coef == coef)
{
it->coef += coef;
return head;
}
iterator new = (iterator)malloc(sizeof(Node));
new->coef = coef;
new->expo = expo;
new->next = 0;
it->next = new;
return head;
}
prev = it;
it = it->next;
}
if(it->expo == expo)
{
it->coef += coef;
return head;
}
iterator new = (iterator)malloc(sizeof(Node));
new->coef = coef;
new->expo = expo;
new->next = it;
prev->next = new;
return head;
}
iterator insert_at_end(int coef, int expo, iterator head)
{
iterator it = head;
while(it->next != 0) it = it->next;
iterator new = (iterator)malloc(sizeof(Node));
new->coef = coef;
new->expo = expo;
new->next = 0;
it->next = new;
return head;
}
iterator add_polynomials(iterator head1, iterator head2)
{
iterator temp1 = head1;
iterator temp2 = head2;
iterator head3 = 0;
while(temp1 != 0 && temp2 != 0)
{
if(temp1->expo < temp2->expo)
{
if(head3 == 0){
head3 = create_singly_linked_list(temp2->coef, temp2->expo);
}
else{
head3 = insert_at_end(temp2->coef, temp2->expo, head3);
}
temp2 = temp2->next;
}
else if(temp1->expo > temp2->expo)
{
if(head3 == 0){
head3 = create_singly_linked_list(temp1->coef, temp1->expo);
}
else{
head3 = insert_at_end(temp1->coef, temp1->expo, head3);
}
temp1 = temp1->next;
}
else
{
if(head3 == 0){
head3 = create_singly_linked_list(temp1->coef + temp2->coef, temp1->expo);
}
else{
head3 = insert_at_end(temp1->coef + temp2->coef, temp1->expo, head3);
}
temp1 = temp1->next;
temp2 = temp2->next;
}
}
iterator it = head3;
while(it->next != 0) it = it->next;
if(temp1 == 0 && temp2 != 0) it->next = temp2;
else if(temp2 == 0 && temp1 != 0) it->next = temp1;
return head3;
}
void display_sll(iterator head)
{
iterator it = head;
while(it->next != 0)
{
printf("%d * x^%d + ", it->coef, it->expo);
it = it->next;
}
printf("%d * x^%d", it->coef, it->expo);
printf("\n");
}
int main()
{
iterator head1 = 0;
iterator head2 = 0;
int terms1, terms2;
printf("Enter the number of terms in first polynomial: ");
scanf("%d", &terms1);
printf("Enter the coeficient and exponent for all terms of first polynomial: ");
for(int i=0;i<terms1;i++)
{
int coef, expo;
scanf("%d%d", &coef, &expo);
if(head1 == 0) head1 = create_singly_linked_list(coef, expo);
else head1 = insert_inorder(coef, expo, head1);
}
display_sll(head1);
printf("Enter the number of terms in second polynomial: ");
scanf("%d", &terms2);
printf("Enter the coeficient and exponent for all terms of second polynomial: ");
for(int i=0;i<terms2;i++)
{
int coef, expo;
scanf("%d%d", &coef, &expo);
if(head2 == 0) head2 = create_singly_linked_list(coef, expo);
else head2 = insert_inorder(coef, expo, head2);
}
display_sll(head2);
iterator head3 = add_polynomials(head1, head2);
display_sll(head3);
}