File tree 1 file changed +80
-0
lines changed
1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Find the differentiation of the polynomial equation using Singly Linked List.
2
+
3
+ // Input Format :
4
+
5
+ // First line contains the highest degree of the polynomial
6
+
7
+ // Second line contains the Coefficient of polynomial
8
+
9
+ // Sample Input :
10
+
11
+ // 5
12
+
13
+ // 3 0 -2 0 1 5
14
+
15
+ // Sample Output:
16
+
17
+ // 15x^4 - 6x^2 + 1x^0
18
+
19
+ // Explanation:
20
+
21
+ // Differentiation (3x^5 + 0x^4 - 2x^3 + 0x^2 + 1x^1 + 5x^0) = 15x^4 - 6x^2 + 1x^0
22
+
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+ struct node
26
+ {
27
+ int pow ;
28
+ int co ;
29
+ struct node * next ;
30
+ };
31
+ typedef struct node node ;
32
+ node * position ;
33
+ void diff (node * ans ,int c ,int p )
34
+ {
35
+ node * newnode = malloc (sizeof (node ));
36
+ newnode -> next = NULL ;
37
+ if (p > 0 )
38
+ {
39
+ newnode -> pow = p - 1 ;
40
+ newnode -> co = p * c ;
41
+ if (ans -> next == NULL )
42
+ {
43
+ ans -> next = newnode ;
44
+ position = newnode ;
45
+ }
46
+ else
47
+ {
48
+ position -> next = newnode ;
49
+ position = newnode ;
50
+ }
51
+ }
52
+ }
53
+ void display (node * ans )
54
+ {
55
+ node * position = ans -> next ;
56
+ while (position != NULL )
57
+ {
58
+ if (position -> co != 0 )
59
+ {
60
+ printf ("%dx^%d " ,position -> co ,position -> pow );
61
+ if (position -> pow != 0 && position -> co > 0 )
62
+ printf ("+ " );
63
+ }
64
+ position = position -> next ;
65
+ }
66
+ }
67
+ int main ()
68
+ {
69
+ int n ,m ;
70
+ node * ans = malloc (sizeof (node ));
71
+ ans -> next = NULL ;
72
+ scanf ("%d" ,& n );
73
+ for (int i = 0 ;i <=n ;i ++ )
74
+ {
75
+ scanf ("%d " ,& m );
76
+ diff (ans ,m ,n - i );
77
+ }
78
+ display (ans );
79
+ return 0 ;
80
+ }
You can’t perform that action at this time.
0 commit comments