Skip to content

Commit d3f4015

Browse files
authored
Update Polynomial_linklist.c
1 parent 06f5ecb commit d3f4015

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

Polynomial_linklist.c

+49-39
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,57 @@
11
#include<stdio.h>
22
#include<stdlib.h>
33
#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+
}
2426
}
25-
}
2627
}
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");
3336
}
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;
4146
}
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;
4757
}

0 commit comments

Comments
 (0)