(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-2960223314593660", enable_page_level_ads: true }); C program for Application of Linked List: Polynomial Multiplication - TecGlance

Header Ads

C program for Application of Linked List: Polynomial Multiplication

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct poly
{
       int c,e;
       struct poly *next;
};
void read_poly(struct poly *header)
{
       struct poly *newnode,*temp;
       char ch;
       printf("\nCreate a node (Y/N) : ");
       getchar();
       scanf("%c",&ch);
       header->next=NULL;
       while(ch!='n')
       {
           newnode=(struct poly *)malloc(sizeof(struct poly));
           printf("\nEnter the coefficient value : ");
           scanf("%d",&newnode->c);
           printf("\nEnter the exponential value : ");
           scanf("%d",&newnode->e);
           newnode->next=NULL;
           if(header->next==NULL)
           {
                header->next=newnode;
                temp=newnode;
           }
           else
           {
               temp->next=newnode;
               temp=newnode;
           }
           printf("\nCreate a node (Y/N) : ");
           getchar();
           scanf("%c",&ch);
       }
}
void mult_poly(struct poly *p,struct poly *q,struct poly *r)
{
       int c,e;
       r->next=NULL;
       p=p->next;
       q=q->next;
       struct poly *node,*temp,*last,*ptr;
       temp=q;
       while(p!=NULL)
       {
          q=temp;
          while(q!=NULL)
          {
             c=p->c*q->c;
             e=p->e+q->e;
             if(r->next==NULL)
             {
                node=(struct poly *)malloc(sizeof(struct poly));
                node->c=c;
                node->e=e;
                node->next=NULL;
                r->next=node;
                last=node;
             }
             else
             {
                ptr=r->next;
                while(ptr!=NULL)
                {
                   if(ptr->e!=e)
                      ptr=ptr->next;             
                   else
                      break;    
                }
                if(ptr!=NULL)
                {
                   ptr->c+=c;             
                }
                else
                {
                   node=(struct poly *)malloc(sizeof(struct poly));
                   node->c=c;
                   node->e=e;
                   node->next=NULL;
                   last->next=node;
                   last=node;    
                }  
             }
          q=q->next;
          }
       p=p->next;
       }
}
void delt(struct poly *header)
{
   struct poly *ptr,*ptr1;
   ptr=header->next;
   while(ptr!=NULL)
     {
          ptr1=ptr;
          ptr=ptr->next;
          free(ptr1);
     }
}
void traverse(struct poly *header)
{
     printf("\n");
     struct poly *ptr=header->next;
     while(ptr!=NULL)
     {
          printf(" %dX%d+",ptr->c,ptr->e);
          ptr=ptr->next;
     }
     printf("\n");
}
int main()
{
    int ch;
    struct poly *p,*q,*r;
    p=(struct poly *)malloc(sizeof(struct poly));
    q=(struct poly *)malloc(sizeof(struct poly));
    r=(struct poly *)malloc(sizeof(struct poly));
    do
    {
        printf("\n..MENU..\n1. Enter Polynomial\n2. Polynomial Multiplication\n3. Exit\n\nEnter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
                  case 1: printf("\nEnter polynomial P : ");
                          read_poly(p);
                          traverse(p);
                          printf("\n\nEnter polynomial Q : ");
                          getchar();
                          read_poly(q);
                          traverse(q);
                          break;
                  case 2: mult_poly(p,q,r);
                          printf("\nResultant polynomial is: ");
                          traverse(r);
                          break;
                  case 3: exit(0);
        }
    }while(1);
    delt(p);
    delt(q);
    delt(r);
    getchar();
}

No comments

Powered by Blogger.