-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolyAddUsingLinkedList5.c
152 lines (140 loc) · 3.77 KB
/
PolyAddUsingLinkedList5.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
/*Exp 5: Write a Program to add Two Polynomials Using Linked List */
#include <stdio.h>
#include<stdlib.h>
struct node{
int coef,expo;
struct node *link;
};
struct node *Phead,*Qhead,*Rhead;
//read poly input
struct node * ReadPoly(){
struct node *new,*ptr,*head=NULL;
int n,i;
printf("Enter the total number of terms in the polynomial : ");
scanf("%d",&n);
printf("Enter the COEFFICIENTS and EXPONENTS of the polynomial in DESCENDING ORDER \n");
for(int i=1;i<=n;i++){
printf("Enter(coef%d,expo%d) : ",i,i);
new=(struct node *)malloc(sizeof(struct node));
scanf("%d",&new->coef);
scanf("%d",&new->expo);
new->link=NULL;
if(head==NULL){
head=new;
ptr=head;
}else{
ptr->link=new;
ptr=new;
}
}
return(head);
}
//display polynomial
void DisplayPoly(struct node *head){
struct node *ptr;
if(head==NULL){
printf("Polynomail is Empty");
}else{
ptr=head;
while(ptr->link!=NULL){
printf("%dx^%d + ",ptr->coef,ptr->expo);
ptr=ptr->link;
}
printf("%dX%d\t",ptr->coef,ptr->expo);
}
}
//Add Polynomail
struct node * AddPoly(){
struct node *new,*P,*Q,*R,*head=NULL;
P=Phead;
Q=Qhead;
while(P!=NULL && Q!=NULL){
if(P->expo==Q->expo){
new=(struct node *)malloc(sizeof(struct node));
new->coef=P->coef+Q->coef;
new->expo=P->expo;
new->link=NULL;
P=P->link;
Q=Q->link;
}else if(P->expo>Q->expo){
new=(struct node *)malloc(sizeof(struct node));
new->coef=P->coef;
new->expo=P->expo;
new->link=NULL;
P=P->link;
}else{
new=(struct node *)malloc(sizeof(struct node));
new->coef=Q->coef;
new->expo=Q->expo;
new->link=NULL;
Q=Q->link;
}if(head==NULL){
head=new;
R=head;
}else{
R->link=new;
R=new;
}
}
while(P!=NULL){
new=(struct node *)malloc(sizeof(struct node));
new->coef=P->coef;
new->expo=P->expo;
new->link=NULL;
if(head==NULL){
head=new;
R=head;
}else{
R->link=new;
R=new;
}
P=P->link;
}
while(Q!=NULL){
new=(struct node *)malloc(sizeof(struct node));
new->coef=Q->coef;
new->expo=Q->expo;
new->link=NULL;
if(head==NULL){
head=new;
R=head;
}else{
R->link=new;
R=new;
}
Q=Q->link;
}
return(head);
}
void main(){
printf("Enter the details of First Polynomial\n");
Phead=ReadPoly();
printf("\nEnter the details of Second Polynomial\n");
Qhead=ReadPoly();
printf("\nFirst Polynomial : ");
DisplayPoly(Phead);
printf("\nSecond Polynomial : ");
DisplayPoly(Qhead);
Rhead=AddPoly();
printf("\nResultant polynomail(SUM) : ");
DisplayPoly(Rhead);
}
//SAMPLE OUTPUT
/*
Enter the details of First Polynomial
Enter the total number of terms in the polynomial : 3
Enter the COEFFICIENTS and EXPONENTS of the polynomial in DESCENDING ORDER
Enter(coef1,expo1) : 5 4
Enter(coef2,expo2) : 4 3
Enter(coef3,expo3) : 2 1
Enter the details of Second Polynomial
Enter the total number of terms in the polynomial : 3
Enter the COEFFICIENTS and EXPONENTS of the polynomial in DESCENDING ORDER
Enter(coef1,expo1) : 5 3
Enter(coef2,expo2) : 4 2
Enter(coef3,expo3) : 3 1
First Polynomial : 5X^4 + 4X^3 + 2X1
Second Polynomial : 5X^3 + 4X^2 + 3X1
Resultant polynomail(SUM) : 5X^4 + 9X^3 + 4X^2 + 5X1
=== Code Exited With Errors ===
*/