Advertisement
Naimul_X

Assignment_DataLab_F

Feb 23rd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct Node
  6. {
  7. int data;
  8. int pow;
  9. struct Node *next;
  10. };
  11. typedef struct Node node;
  12.  
  13. void create_node(int a, int b, node **temp)
  14. {
  15. struct Node *r, *x;
  16. x = *temp;
  17. if(x == NULL)
  18. {
  19. r =new node();
  20. r->data = a;
  21. r->pow = b;
  22. *temp = r;
  23. r->next = new node();
  24. r = r->next;
  25. r->next = NULL;
  26. }
  27. else
  28. {
  29. r->data = a;
  30. r->pow = b;
  31. r->next = new node();
  32. r = r->next;
  33. r->next = NULL;
  34. }
  35. }
  36.  
  37.  
  38. void polyadd(node *poly1, node *poly2,node *poly)
  39. {
  40. while(poly1->next && poly2->next)
  41. {
  42.  
  43. if(poly1->pow > poly2->pow)
  44. {
  45. poly->pow = poly1->pow;
  46. poly->data = poly1->data;
  47. poly1 = poly1->next;
  48. }
  49.  
  50.  
  51. else if(poly1->pow < poly2->pow)
  52. {
  53. poly->pow = poly2->pow;
  54. poly->data = poly2->data;
  55. poly2 = poly2->next;
  56. }
  57.  
  58.  
  59. else
  60. {
  61. poly->pow = poly1->pow;
  62. poly->data = poly1->data+poly2->data;
  63. poly1 = poly1->next;
  64. poly2 = poly2->next;
  65. }
  66.  
  67. poly->next = new node();
  68. poly = poly->next;
  69. poly->next = NULL;
  70. }
  71. while(poly1->next || poly2->next)
  72. {
  73. if(poly1->next)
  74. {
  75. poly->pow = poly1->pow;
  76. poly->data = poly1->data;
  77. poly1 = poly1->next;
  78. }
  79. if(poly2->next)
  80. {
  81. poly->pow = poly2->pow;
  82. poly->data = poly2->data;
  83. poly2 = poly2->next;
  84. }
  85. poly->next = new node();
  86. poly = poly->next;
  87. poly->next = NULL;
  88. }
  89. }
  90.  
  91.  
  92. void show(struct Node *node)
  93. {
  94. while(node->next != NULL)
  95. {
  96. if(node->pow==1)
  97. {
  98. printf("%dx", node->data);
  99. }
  100. else if(node->pow==0)
  101. {
  102. printf("%d", node->data);
  103. }
  104. else
  105. {
  106. printf("%dx^%d", node->data, node->pow);
  107. }
  108. node = node->next;
  109. if(node->next != NULL)
  110. printf(" + ");
  111. }
  112. }
  113.  
  114.  
  115. int main()
  116. {
  117. struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
  118.  
  119.  
  120. create_node(5,3,&poly1);
  121. create_node(2,2,&poly1);
  122. create_node(3,1,&poly1);
  123. create_node(4,0,&poly1);
  124.  
  125.  
  126. create_node(4,2,&poly2);
  127. create_node(3,0,&poly2);
  128.  
  129. printf("1st Number: ");
  130. show(poly1);
  131.  
  132. printf("\n2nd Number: ");
  133. show(poly2);
  134.  
  135. poly = new node();
  136.  
  137.  
  138. polyadd(poly1, poly2, poly);
  139.  
  140. printf("\nAdded NUMBER: ");
  141. show(poly);
  142.  
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement