/
29 Polynomial of Linkedlist.c
68 lines (57 loc) · 1.25 KB
/
29 Polynomial of Linkedlist.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
// representation of polynomial
struct Node {
int coeff; // coefficient of poly
int exp; // exponential of poly
struct Node* next; // pointer to next node
}*poly=NULL;
void Create ()
{
struct Node* t, * last = NULL; // temp node to new node ; and ptr to last nnode
int num, i;
printf("Enter number of terms ");
scanf_s("%d", &num);
printf("Enter each terms with there coeff and exp\n");
for (i = 0; i < num; i++)
{
t = (struct Node*)malloc(sizeof(struct Node)); // for each term we need to create node
scanf_s("%d %d", &t->coeff, &t->exp); // eading two value
t->next = NULL; // last node will be null
if (poly == NULL) // if it is 1st node
{
poly = last = t; // all sud point on 1st
}
else {
last->next = t; // moving upto last and inserion
last = t;
}
}
}
void Display(struct Node* p)
{
while (p)
{
printf("%dx%d +", p->coeff, p->exp);
p = p->next;
}
printf("\n");
}
long eval(struct Node *p, int x)
{
double sum = 0;
while (p)
{
sum += p->coeff * pow(x, p->exp);
p = p->next;
}
return sum;
}
int main()
{
Create();
Display(poly);
printf("%ld\n", eval(poly, 2));
return 0;
}