-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial_representaion.c
65 lines (63 loc) · 1.23 KB
/
polynomial_representaion.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
//polynomial representation
#include<stdio.h>
#include<stdlib.h>
struct node{
float coeff;
int expo;
struct node *next;
};
typedef struct node Node;
Node *insert_a_term(Node *head,float co,int ex){
Node *NN;
NN=(Node *)malloc(sizeof(Node));
NN->coeff=co;
NN->expo=ex;
NN->next=NULL;
if(head==NULL || NN->expo > head->expo){
NN->next=head;
head=NN;
}
else{
Node *temp=head;
while(temp->next!=NULL &&temp->next->expo >ex){
temp=temp->next;
}
NN->next=temp->next;
temp->next=NN;
}
return head;
}
Node *create_polynomial(){
Node *head=NULL;
int n,i;
printf("enter the no of terms:\n");
scanf("%d",&n);
for(i=1;i<=n;i++){
float co;
int ex;
printf("enter the coeff of term %d:\n",i);
scanf("%f",&co);
printf("enter expo of the term %d:\n",i);
scanf("%d",&ex);
head=insert_a_term(head,co,ex);
}
return head;
}
void display_polynomial(Node *head){
Node *temp=head;
while(temp){
printf("%0.1fx^%d",temp->coeff,temp->expo);
if(temp->next!=NULL){
printf(" + ");
}
else{
printf("\n");
}
temp=temp->next;
}
}
int main(){
printf("Polynomial:\n");
Node *poly=create_polynomial();
display_polynomial(poly);
}