|
1 | 1 | #include<stdio.h>
|
2 | 2 | #include<stdlib.h>
|
3 | 3 | #include<math.h>
|
4 |
| -struct Node { //node structure for polynomial |
5 |
| - int coeff; |
6 |
| - int exp; //exponent |
7 |
| - struct Node * next; //pointing to next node |
8 |
| -}* poly = NULL; //type pointer polynomial |
9 |
| -void create() { //creating polyno mial |
10 |
| - struct Node * t, * last = NULL; //temporary pointer, last pointer |
11 |
| - int num, i; |
12 |
| - printf("Enter number of terms"); |
13 |
| - scanf("%d", & num); |
14 |
| - printf("Enter each term with coeff and exp\n"); |
15 |
| - for (i = 0; i < num; i++) { //loop |
16 |
| - t = (struct Node * ) malloc(sizeof(struct Node)); //create new node |
17 |
| - scanf("%d%d", &t->coeff, &t->exp); //reading 2 data |
18 |
| - t-> next = NULL; //linking each node into linklist |
19 |
| - if (poly == NULL) { //first node check |
20 |
| - poly = last = t; |
21 |
| - } else { |
22 |
| - last -> next = t; |
23 |
| - last = t; |
| 4 | +struct Node { //node structure for polynomial |
| 5 | + int coeff; |
| 6 | + int exp; //exponent |
| 7 | + struct Node *next; //pointing to next node |
| 8 | +} *poly = NULL; //type pointer polynomial |
| 9 | +void create(void) |
| 10 | +{ //creating polyno mial |
| 11 | + struct Node *t, *last = NULL; //temporary pointer, last pointer |
| 12 | + int num, i; |
| 13 | + printf("Enter number of terms"); |
| 14 | + scanf("%d", &num); |
| 15 | + printf("Enter each term with coeff and exp\n"); |
| 16 | + for (i = 0; i < num; i++) { //loop |
| 17 | + t = (struct Node *) malloc(sizeof(struct Node)); //create new node |
| 18 | + scanf("%d%d", &t->coeff, &t->exp); //reading 2 data |
| 19 | + t->next = NULL; //linking each node into linklist |
| 20 | + if (poly == NULL) { //first node check |
| 21 | + poly = last = t; |
| 22 | + } else { |
| 23 | + last->next = t; |
| 24 | + last = t; |
| 25 | + } |
24 | 26 | }
|
25 |
| - } |
26 | 27 | }
|
27 |
| -void Display(struct Node * p) { |
28 |
| - while (p) { |
29 |
| - printf("%dx%d +", p -> coeff, p -> exp); //printing node |
30 |
| - p = p -> next; //shifting node |
31 |
| - } |
32 |
| - printf("\n"); |
| 28 | + |
| 29 | +void Display(struct Node *p) |
| 30 | +{ |
| 31 | + while (p) { |
| 32 | + printf("%dx%d +", p->coeff, p->exp); //printing node |
| 33 | + p = p->next; //shifting node |
| 34 | + } |
| 35 | + printf("\n"); |
33 | 36 | }
|
34 |
| -long Eval(struct Node * p, int x) { //evalution |
35 |
| - long val = 0; |
36 |
| - while (p) { //scanning through polynomial |
37 |
| - val += p -> coeff * pow(x, p -> exp); |
38 |
| - p = p -> next; |
39 |
| - } |
40 |
| - return val; |
| 37 | + |
| 38 | +long Eval(struct Node *p, int x) |
| 39 | +{ //evalution |
| 40 | + long val = 0; |
| 41 | + while (p) { //scanning through polynomial |
| 42 | + val += p->coeff * pow(x, p->exp); |
| 43 | + p = p->next; |
| 44 | + } |
| 45 | + return val; |
41 | 46 | }
|
42 |
| -int main() { |
43 |
| - create(); |
44 |
| - Display(poly); |
45 |
| - printf("%ld\n", Eval(poly, 1)); |
46 |
| - return 0; |
| 47 | + |
| 48 | +// TODO insert |
| 49 | +// TODO delete |
| 50 | + |
| 51 | +int main(void) |
| 52 | +{ |
| 53 | + create(); |
| 54 | + Display(poly); |
| 55 | + printf("%ld\n", Eval(poly, 1)); |
| 56 | + return 0; |
47 | 57 | }
|
0 commit comments