fork(1) download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // Node structure containing power and coefficient of variable
  6.  
  7. struct node
  8. {
  9. int coeff;
  10. int exp;
  11. struct node *next;
  12. };
  13.  
  14. // Function to create new node
  15.  
  16.  
  17. void create_node(int x, int y, struct node **temp)
  18. {
  19.  
  20. struct node *r, *tmp=NULL ;
  21.  
  22. if(*temp == NULL) // If the list is NULL then add node at First position
  23. {
  24. r =(struct node*)malloc(sizeof(struct node));
  25. r->coeff = x;
  26. r->exp = y;
  27. r->next = NULL;
  28. *temp = r;
  29. /*r->next = (struct node*)malloc(sizeof(struct node));
  30.   r = r->next;
  31.   r->next = NULL;*/ //Not needed
  32. }
  33. else
  34. {
  35. tmp = *temp;
  36. while (tmp->next != NULL) {
  37. tmp = tmp -> next; //Travel to the end of linked list
  38. }
  39. r = (struct node*)malloc(sizeof(struct node));
  40. r->coeff = x;
  41. r->exp = y;
  42. r->next = NULL;
  43. tmp->next = r; //Add new node to the list
  44.  
  45. }
  46. }
  47.  
  48. // Function Adding two polynomial numbers
  49.  
  50. /* adds a term to the polynomial in the descending order of the exponent */
  51.  
  52. void padd ( int coeff, int exp, struct node **s )
  53. {
  54. struct node *r, *temp = *s ;
  55. struct node *tmp = *s;
  56. /* if list is empty or if the node is to be inserted before the first node
  57.   */
  58.  
  59.  
  60.  
  61. if ( temp == NULL || exp > ( temp ) -> exp )
  62.  
  63. {
  64. r = (struct node*)malloc ( sizeof ( struct node ) ) ;
  65. r -> coeff = coeff ;
  66. r -> exp = exp ;
  67. r -> next = temp ;
  68. temp = r;
  69. *s = r;
  70. }
  71. else
  72. {
  73. /* traverse the entire linked list to search the position to insert
  74.   a new node */
  75. while ( temp != NULL )
  76. {
  77. if ( temp -> exp == exp )
  78. {
  79. temp -> coeff += coeff ;
  80. return;
  81. } else if ( temp -> exp > exp && ( temp -> next -> exp < exp ||temp ->next == NULL ) )
  82. {
  83. r = (struct node*)malloc ( sizeof ( struct node ) ) ;
  84. r -> coeff = coeff;
  85. r -> exp = exp ;
  86. r -> next = temp -> next ;
  87. temp -> next = r ;
  88. return;
  89. }
  90.  
  91. temp = temp -> next ; /* go to next node */
  92.  
  93. }
  94.  
  95. r -> next = NULL ;
  96. temp -> next = r ;
  97. }
  98. }
  99.  
  100. struct node * polymul ( struct node *poly1, struct node *poly2, struct node *poly3)
  101. {
  102.  
  103. struct node *y1 ;
  104. int coeff1;
  105. int exp1 ;
  106.  
  107. struct node *poly = poly3;
  108. y1 = poly2 ; /* point to the starting of the second linked list */
  109. if ( poly1 == NULL && poly2 == NULL )
  110. return NULL;
  111.  
  112. /* if one of the list is empty */
  113. if ( poly1 == NULL ) {
  114. poly = poly2 ;
  115. } else
  116. {
  117. if ( poly2 == NULL )
  118. poly = poly1 ;
  119. else /* if both linked lists exist */
  120.  
  121. {
  122. /* for each term of the first list */
  123. while ( poly1 != NULL )
  124. {
  125. /* multiply each term of the second linked list with a
  126.   term of the first linked list */
  127. while ( poly2 != NULL )
  128. {
  129. coeff1 = poly1 -> coeff * poly2 -> coeff ;
  130. exp1 = poly1 -> exp + poly2 -> exp ;
  131. poly2 = poly2 -> next ;
  132.  
  133. /* add the new term to the resultant polynomial */
  134.  
  135. padd( coeff1, exp1, &poly) ;
  136. }
  137.  
  138. poly2 = y1 ; /* reposition the pointer to the starting of
  139.   the second linked list */
  140.  
  141.  
  142. poly1 = poly1 -> next ; /* go to the next node */
  143.  
  144. }
  145. }
  146. }
  147. return poly;
  148. }
  149.  
  150.  
  151.  
  152. // Display Linked list
  153. void show(struct node *node)
  154. {
  155. while(node != NULL) // if we use node->next != NULL while will break at the last node skipping it
  156. {
  157. printf("%dx^%d", node->coeff, node->exp);
  158.  
  159. if(node->next != NULL)
  160. printf(" + ");
  161.  
  162. node = node->next;
  163. }
  164. }
  165.  
  166. // Driver program
  167.  
  168. int main()
  169. {
  170. struct node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
  171.  
  172. // Create first list of 5x^2 + 4x^1 + 2x^0
  173.  
  174. create_node(5,2,&poly1);
  175. create_node(4,1,&poly1);
  176. create_node(2,0,&poly1);
  177.  
  178. // Create second list of 5x^1 + 5x^0
  179.  
  180. create_node(5,1,&poly2);
  181. create_node(5,0,&poly2);
  182.  
  183. printf("1st Number: ");
  184. show(poly1);
  185.  
  186. printf("\n2nd Number: ");
  187. show(poly2);
  188.  
  189. poly = (struct node *)malloc(sizeof(struct node));
  190.  
  191. // Function add two polynomial numbers
  192.  
  193. poly = polymul(poly1, poly2, poly);
  194.  
  195. // Display resultant List
  196.  
  197. printf("\nAdded polynomial: ");
  198. show(poly);
  199.  
  200. return 0;
  201. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
1st Number: 5x^2 + 4x^1 + 2x^0
2nd Number: 5x^1 + 5x^0
Added polynomial: 25x^3 + 45x^2 + 30x^1 + 10x^0