[text] hi

Viewer

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct Node
  5. {
  6. int coeff;
  7. int pow;
  8. struct Node *next;
  9. };
  10. // Function to create new node
  11. void create_node(int x, int y, struct Node **temp)
  12. {
  13. struct Node *r, *z;
  14. z = *temp;
  15. if(z == NULL)
  16. {
  17. r =(struct Node*)malloc(sizeof(struct Node));
  18. r->coeff = x;
  19. r->pow = y;
  20. *temp = r;
  21. r->next = (struct Node*)malloc(sizeof(struct Node));
  22. r = r->next;
  23. r->next = NULL;
  24. }
  25. else
  26. {
  27. r->coeff = x;
  28. r->pow = y;
  29. r->next = (struct Node*)malloc(sizeof(struct Node));
  30. r = r->next;
  31. r->next = NULL;
  32. }
  33. }
  34. // Function Adding two polynomial numbers
  35. void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly)
  36. {
  37. while(poly1->next && poly2->next)
  38. {
  39. // If power of 1st polynomial is greater then 2nd, then store 1st as it is
  40. // and move its pointer
  41. if(poly1->pow > poly2->pow)
  42. {
  43. poly->pow = poly1->pow;
  44. poly->coeff = poly1->coeff;
  45. poly1 = poly1->next;
  46. }
  47. // If power of 2nd polynomial is greater then 1st, then store 2nd as it is
  48. // and move its pointer
  49. else if(poly1->pow < poly2->pow)
  50. {
  51. poly->pow = poly2->pow;
  52. poly->coeff = poly2->coeff;
  53. poly2 = poly2->next;
  54. }
  55. // If power of both polynomial numbers is same then add their coefficients
  56. else
  57. {
  58. poly->pow = poly1->pow;
  59. poly->coeff = poly1->coeff+poly2->coeff;
  60. poly1 = poly1->next;
  61. poly2 = poly2->next;
  62. }
  63. // Dynamically create new node
  64. poly->next = (struct Node *)malloc(sizeof(struct Node));
  65. poly = poly->next;
  66. poly->next = NULL;
  67. }
  68. while(poly1->next || poly2->next)
  69. {
  70. if(poly1->next)
  71. {
  72. poly->pow = poly1->pow;
  73. poly->coeff = poly1->coeff;
  74. poly1 = poly1->next;
  75. }
  76. if(poly2->next)
  77. {
  78. poly->pow = poly2->pow;
  79. poly->coeff = poly2->coeff;
  80. poly2 = poly2->next;
  81. }
  82. poly->next = (struct Node *)malloc(sizeof(struct Node));
  83. poly = poly->next;
  84. poly->next = NULL;
  85. }
  86. }
  87. // Display Linked list
  88. void show(struct Node *node)
  89. {
  90. while(node->next != NULL)
  91. {
  92. printf("%dx^%d", node->coeff, node->pow);
  93. node = node->next;
  94. if(node->next != NULL)
  95. printf(" + ");
  96. }
  97. }
  98. // Driver program
  99. int main()
  100. {
  101. struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
  102. // Create first list of 5x^2 + 4x^1 + 2x^0
  103. create_node(5,2,&poly1);
  104. create_node(4,1,&poly1);
  105. create_node(2,0,&poly1);
  106. // Create second list of 5x^1 + 5x^0
  107. create_node(5,1,&poly2);
  108. create_node(5,0,&poly2);
  109. printf("1st Polynomial Equation: ");
  110. show(poly1);
  111. printf("\n2nd Polynomial Equation: ");
  112. show(poly2);
  113. poly = (struct Node *)malloc(sizeof(struct Node));
  114. // Function add two polynomial numbers
  115. polyadd(poly1, poly2, poly);
  116. // Display resultant List
  117. printf("\nAdded polynomial: ");
  118. show(poly);
  119. return 0;
  120. }

Editor

You can edit this paste and save as new: