-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial_Differentiation_singly_linked_list.c
80 lines (69 loc) · 1.43 KB
/
Polynomial_Differentiation_singly_linked_list.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
// Find the differentiation of the polynomial equation using Singly Linked List.
// Input Format :
// First line contains the highest degree of the polynomial
// Second line contains the Coefficient of polynomial
// Sample Input :
// 5
// 3 0 -2 0 1 5
// Sample Output:
// 15x^4 - 6x^2 + 1x^0
// Explanation:
// Differentiation (3x^5 + 0x^4 - 2x^3 + 0x^2 + 1x^1 + 5x^0) = 15x^4 - 6x^2 + 1x^0
#include<stdio.h>
#include<stdlib.h>
struct node
{
int pow;
int co;
struct node *next;
};
typedef struct node node;
node *position;
void diff(node *ans,int c,int p)
{
node *newnode=malloc(sizeof(node));
newnode->next=NULL;
if(p>0)
{
newnode->pow=p-1;
newnode->co=p*c;
if(ans->next==NULL)
{
ans->next=newnode;
position=newnode;
}
else
{
position->next=newnode;
position=newnode;
}
}
}
void display(node *ans)
{
node *position=ans->next;
while(position!=NULL)
{
if(position->co!=0)
{
printf("%dx^%d ",position->co,position->pow);
if(position->pow!=0 && position->co>0)
printf("+ ");
}
position=position->next;
}
}
int main()
{
int n,m;
node *ans=malloc(sizeof(node));
ans->next=NULL;
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
scanf("%d ",&m);
diff(ans,m,n-i);
}
display(ans);
return 0;
}