Code.GeekInterview.com
 
Code Samples C
 

Polynomial Addition using Linked List


Code ResourceAuthor: mano.mithun  

Difficulty Level: Intermediate

Published: 3rd Aug 2010   Read: 21384 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

This is about polynomial addition using linked list.

 


Sample Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. typedef struct poly
  4. {
  5. int coeff;
  6. int expo;
  7. }p;
  8. p p1[10],p2[10],p3[10];
  9. void main()
  10. {
  11. int t1,t2,t3,k;
  12. int read(p p1[10]);
  13. int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);
  14. void print(p p2[10],int t2);
  15. void printo(p pp[10],int t2);
  16. clrscr();
  17. t1=read(p1);
  18. print(p1,t1);
  19. t2=read(p2);
  20. print(p2,t2);
  21. t3=add(p1,p2,t1,t2,p3);
  22. printo(p3,t3);
  23. getch();
  24. }
  25. int read(p p[10])
  26. {
  27. int t1,i;
  28. printf("n Enter the total no of terms");
  29. scanf("%d",&t1);
  30. printf("n Enter the coeff and expo in descending order");
  31. for(i=0;i<t1;i++)
  32. scanf("%d%d",&p[i].coeff,&p[i].expo);
  33. return(t1);
  34. }
  35. int add(p p1[10],p p2[10],int t1,int t2,p p3[10])
  36. {
  37. int i,j,k;
  38. int t3;
  39. i=0,j=0,k=0;
  40. while(i<t1 && j<t2)
  41. {
  42. if(p1[i].expo==p2[j].expo)
  43.  
  44. p3[k].coeff=p1[i].coeff+p2[j].coeff;
  45. p3[k].expo=p1[i].expo;
  46. i++;j++;k++;
  47. }
  48. else if(p1[1].expo>p2[j].expo)
  49. {
  50. p3[k].coeff=p1[i].coeff;
  51. p3[k].expo=p1[i].expo;
  52. i++;k++;
  53. }
  54. else
  55. {
  56. p3[k].coeff=p2[j].coeff;
  57. p3[k].expo=p2[j].expo;
  58. j++;k++;
  59. }
  60. }
  61. while(i<t1)
  62. {
  63. p3[k].coeff=p1[i].coeff;
  64. p3[k].expo=p1[i].expo;
  65. i++;k++;
  66. }
  67. while(j<t2)
  68. {
  69. p3[k].coeff=p2[j].coeff;
  70. p3[k].expo=p2[j].expo;
  71. j++;k++;
  72. }
  73. t3=k;
  74. return(t3);
  75. }
  76. void print(p pp[10],int term)
  77. {
  78. int k;
  79. printf("nn Given Polynomial:");
  80. for(k=0;k<term-1;k++)
  81. printf("%dx^%d+",pp[k].coeff,pp[k].expo);
  82. printf("%dx^%d",pp[k].coeff,pp[k].expo);
  83. }
  84. void printo(p pp[10],int term)
  85. {
  86. int k;
  87. printf("nn The addition of polynomial:");
  88. for(k=0;k<term-1;k++)
  89. printf("%dx^%d+",pp[k].coeff,pp[k].expo);
  90. printf("%dx^%d",pp[k].coeff,pp[k].expo);
  91. }
Copyright GeekInterview.com


Next Article: Program to find out an adam number using arrays


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

dear sir,
i am ME first student i want this program using 'Cpp' language, The objective of this program is to swap two variables without using 3rd Variable.
Comment posted by: p.rathika on 2010-09-27T08:42:55
This is very good examples of c program specially linked list
Comment posted by: RahulRaj on 2010-10-02T09:43:08

Popular Coders

# Coder NameHits
1. kaivalya198947000
2. mano.mithun37618
3. deepu0821380
4. sadasivathavamani14006
5. Jimmy Zorald12317
6. venkatakrishnansvpr12235
7. meefriend4ever10314
8. shashivaishnav10039
9. chandrikakr9999
10. sripri8362

Active Coders

Refined Tags