Advertisement
sanpai

Polynomial addition through linked list

Aug 25th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct poly
  5.  
  6. {
  7.  
  8. int co;
  9. int exp;
  10. struct poly *ptr;
  11. };
  12.  
  13. struct poly * Addelement(struct poly *,int,int);
  14. struct poly * add(struct poly *,struct poly *);
  15. void display(struct poly *);
  16.  
  17. int main()
  18.  
  19. {
  20.  
  21. int num1,co,exp,num2,i;
  22. struct poly *p1,*p2,*p3;
  23. p1=NULL;
  24. p2=NULL;
  25. p3=NULL;
  26.  
  27. printf(" \n The number of elements to be added to the 1st list ");
  28. scanf("%d",&num1);
  29.  
  30. for(i=1;i<=num1;i++)
  31. {
  32. printf("\n Enter the Elements of 1st polynomial and coeff \n ");
  33. scanf("%d %d",&co,&exp);
  34. p1=Addelement(p1,co,exp);
  35. }
  36.  
  37. printf(" \n The number of elements to be added to the 2nd list ");
  38. scanf("%d",&num2);
  39.  
  40. for(i=1;i<=num2;i++)
  41. {
  42. printf("\n Enter the Elements of 2nd polynomial and coeff \n ");
  43. scanf("%d %d",&co,&exp);
  44. p2=Addelement(p2,co,exp);
  45. }
  46. printf("\n The elements of first polynomial are ");
  47. display(p1);
  48. printf("\n The elements of second polynomial are ");
  49. display(p2);
  50. p3=add(p1,p2);
  51. printf("\n The Added polynomial is \n");
  52. display(p3);
  53. }
  54.  
  55. struct poly * Addelement(struct poly *head,int co,int exp)
  56. {
  57. struct poly *newnode;
  58. newnode=(struct poly *)malloc(sizeof(struct poly));
  59. newnode->co=co;
  60. newnode->exp=exp;
  61. if(head==NULL)
  62. {
  63. newnode->ptr=NULL;
  64. return newnode;
  65. }
  66.  
  67. else
  68. {
  69. newnode->ptr=head;
  70. return newnode;
  71. }
  72. }
  73.  
  74. void display(struct poly *p3)
  75.  
  76. {
  77. if(p3==NULL)
  78. {
  79.  
  80. printf("\n The list is empty \n");
  81. }
  82.  
  83. while(p3!=NULL)
  84. {
  85. printf(" %dx^%d+ ",p3->co,p3->exp);
  86. p3=p3->ptr;
  87. }
  88. }
  89.  
  90. struct poly * add(struct poly *p1,struct poly *p2)
  91. {
  92.  
  93. struct poly *p3,*temp;
  94. p3=NULL;
  95. if(p1==NULL&&p2==NULL)
  96. {
  97. printf("\n The list is empty \n");
  98. }
  99.  
  100.  
  101. while(p1!=NULL&&p2!=NULL)
  102. {
  103. temp=(struct poly *)malloc(sizeof(struct poly));
  104. if(p3==NULL)
  105. {
  106.  
  107. temp->ptr=NULL;
  108. p3=temp;
  109. }
  110.  
  111. else
  112. {
  113. temp->ptr=p3;
  114. p3=temp;
  115. }
  116.  
  117. if(p1->exp>p2->exp)
  118. {
  119. temp->co=p1->co;
  120. temp->exp=p1->exp;
  121. p1=p1->ptr;
  122. }
  123.  
  124. else if(p2->exp>p1->exp)
  125. {
  126. temp->co=p2->co;
  127. temp->exp=p2->exp;
  128. p2=p2->ptr;
  129. }
  130. else if(p1->exp==p2->exp)
  131. {
  132. temp->co=p1->co+p2->co;
  133. temp->exp=p1->exp;
  134. p1=p1->ptr;
  135. p2=p2->ptr;
  136. }
  137. }
  138. while(p1!=NULL)
  139.  
  140. {
  141. temp=(struct poly *)malloc(sizeof(struct poly));
  142. temp->co=p1->co;
  143. temp->exp=p1->exp;
  144. temp->ptr=p3;
  145. p3=temp;
  146. p1=p1->ptr;
  147. }
  148.  
  149. while(p2!=NULL)
  150.  
  151. {
  152. temp=(struct poly *)malloc(sizeof(struct poly));
  153. temp->co=p2->co;
  154. temp->exp=p2->exp;
  155. temp->ptr=p3;
  156. p3=temp;
  157. p2=p2->ptr;
  158. }
  159.  
  160. return p3;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement