Add two Polynomial Equation using Linked List

// Add two  polynomial equation using linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
          int coef;
          int exp;
          struct link *next;
};
struct link *node,*start,*new1,*pre;
int i;
void create_poly(struct link *node);
void display(struct link *node);
void add_poly(struct link *node);
void main()
{
          clrscr();
          printf("\n create polynomial equation");
          node=(struct link *)malloc(sizeof(struct link));
          create_poly(node);
          printf("\n Polynomial equation is as follow\n");
          display(node);
          add_poly(node);
          printf("\n after addition polynomial equation :");
          display(node);
          getch();
}
void create_poly(struct link *node)
{
          char ans;
          i=0;
          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&node->coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&node->exp);
                   node->next=NULL;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}
void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("%dX ^ %d  ",node->coef,node->exp);
                   node=node->next;
                   if(node!=NULL)
                     printf(" + ");
          }
}
void add_poly(struct link *node)
 {
    int t_coef,t_exp;
    char ans;
    struct link *temp,*tp;
    node=start->next;
    pre=start;
    printf("\n Enter 'n' for break:-");
    ans=getchar();
          while(ans!='n')
          {
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&t_coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&t_exp);
                   pre=start;
                   node=start->next;
                   while(node)
                    {
                      if(t_exp > node->exp)
                       {
                         new1=(struct link *)malloc(sizeof(struct link ));
                         new1->next=node;
                         pre-> next=new1;
                         new1->coef=t_coef;
                         new1->exp=t_exp;
                         break;
                       }
                    else
                    {
                      if(t_exp == node->exp)
                             {
                                node->coef=node->coef+t_coef;
                                node->exp=t_exp;
                                break;
                             }
                    }
                    pre=pre->next;
                    node=node->next;
                 }
             fflush(stdin);
             printf("\n Enter 'n' for break:-");
             ans=getchar();
     }
  }

Post a Comment

Previous Post Next Post