varungurnaney

DSA: Addn of Polynomial using LL

Aug 5th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include<stdio..h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4.  
  5. struct node
  6. {
  7.     int coef;
  8.     int expo;
  9.     struct node *link;
  10. };
  11. struct node *poly_add(struct node *,struct node *);
  12. //struct node *poly_sub(struct node *,struct node *);
  13.  
  14. struct node *enter(struct node *);
  15. struct node *insert(struct node *,float,int);
  16. void display(struct node *);
  17. void main()
  18. {
  19.     struct node *p1_start;
  20.     struct node *p2_start;
  21.     struct node *p3_start;
  22.     struct node *p4_start;
  23.  
  24.     clrscr();
  25.     p1_start=NULL;
  26.     p2_start=NULL;
  27.     p3_start=NULL;
  28.     //p4_start=NULL;
  29.  
  30.     printf("Polynomial 1:\n");
  31.     p1_start=enter(p1_start);
  32.     printf("Polynomial 2:\n");
  33.     p2_start=enter(p2_start);
  34.     p3_start=poly_add(p1_start,p2_start);
  35.     //p4_start=poly_sub(p1_start,p2_start);
  36.     printf("Polynomial 1 : ");
  37.     display(p1_start);
  38.     printf("Polynomial 2 : ");
  39.     display(p2_start);
  40.     printf("Added Polynomial : ");
  41.     display(p3_start);
  42.     /*printf("Subtracted Polynomial : ");
  43.     diplay(p4_start);*/
  44.     getch();
  45. }
  46. struct node *enter(struct node *start)
  47. {
  48.     int i,n,ex,co;
  49.     printf("How many Time You Want To Enter\n");
  50.     scanf("%d",&n);
  51.     for(i=1;i<=n;i++)
  52.     {
  53.         printf("Co-efficient of Term %d : ",i);
  54.         scanf("%d",&co);
  55.         printf("Expo of Term %d : ",i);
  56.         scanf("%d",&ex);
  57.         start=insert(start,co,ex);
  58.     }
  59.     return start;
  60. }
  61.  
  62. struct node *insert(struct node *start,float co,int ex)
  63. {
  64.     struct node *ptr,*t;
  65.     t=(struct node *)malloc(sizeof(struct node));
  66.     t->coef=co;
  67.     t->expo=ex;
  68.  
  69.     if(start==NULL||ex>start->expo)
  70.     {
  71.         t->link=start;
  72.         start=t;
  73.     }
  74.     else
  75.     {
  76.         ptr=start;
  77.         while(ptr->link!=NULL &&ptr->link->expo>ex)
  78.         {
  79.             ptr=ptr->link;
  80.         }
  81.         t->link=ptr->link;
  82.         ptr->link=t;
  83.         if(ptr->link==NULL)
  84.         {
  85.             t->link=NULL;
  86.         }
  87.     }
  88.     return start;
  89. }
  90. struct node *poly_add(struct node *p1,struct node *p2)
  91. {
  92.     struct node *p3_start,*p3,*t;
  93.     p3_start=NULL;
  94.     if(p1==NULL&&p2!=NULL)
  95.         return p3_start;
  96.     while(p1!=NULL&&p2!=NULL)
  97.     {
  98.         t=malloc(sizeof(struct node));
  99.         if(p3_start==NULL)
  100.         {
  101.             p3_start=t;
  102.             p3=p3_start;
  103.         }
  104.         else
  105.         {
  106.             p3->link=t;
  107.             p3=p3->link;
  108.         }
  109.         if(p1->expo>p2->expo)
  110.         {
  111.             t->coef=p1->coef;
  112.             t->expo=p1->expo;
  113.             p1=p1->link;
  114.         }
  115.         else
  116.         {
  117.             if(p2->expo>p1->expo)
  118.             {
  119.                 t->coef=p2->coef;
  120.                 t->expo=p2->expo;
  121.                 p2=p2->link;
  122.             }
  123.             else
  124.             {
  125.                 if(p1->expo==p2->expo)
  126.                 {
  127.                     t->coef=p1->coef+p2->coef;
  128.                     t->expo=p1->expo;
  129.                     p1=p1->link;
  130.                     p2=p2->link;
  131.                 }
  132.             }
  133.         }
  134.     }//end of while
  135.  
  136.     while(p1!=NULL)
  137.     {
  138.         t=malloc(sizeof(struct node));
  139.         t->coef=p1->coef;
  140.         t->expo=p1->expo;
  141.         if(p3_start==NULL)
  142.         {
  143.             p3_start=t;
  144.             p3=p3_start;
  145.         }
  146.         else
  147.         {
  148.             p3->link=t;
  149.             p3=p3->link;
  150.         }
  151.         p1=p1->link;
  152.     }//end of while
  153.     while(p2!=NULL)
  154.     {
  155.         t=malloc(sizeof(struct node));
  156.         t->coef=p2->coef;
  157.         t->expo=p2->expo;
  158.         if(p3_start==NULL)//poly 1 is empty
  159.         {
  160.             p3_start=t;
  161.             p3=p3_start;
  162.         }
  163.         else
  164.         {
  165.             p3->link=t;
  166.             p3=p3->link;
  167.         }
  168.         p2=p2->link;
  169.     }//end of while
  170.     p3->link=NULL;
  171.     return p3_start;
  172. }
  173. void display(struct node *ptr)
  174. {
  175.     if(ptr==NULL)
  176.     {
  177.         printf("Empty\n");
  178.         return ;
  179.     }
  180.     while(ptr!=NULL)
  181.     {
  182.         printf("(%dx^%d)",ptr->coef,ptr->expo);
  183.         ptr=ptr->link;
  184.     }
  185.     printf("\b\b\n");/*\b\bto erase the last+sign*/
  186. }
Advertisement
Add Comment
Please, Sign In to add comment