Various operations on Polynomials using linked List

#include<math.h>
#include<stdio.h>
#include<conio.h>
typedef struct node
{
    int xpower;
    int ypower;
    float coeff;
    struct node *next;
}node;
void init(node *head);
node * read();
void print(node *head);
node * add(node  *head1,node *head2);
node * insert(node *head,node t1);
void main()
{
    node *head1=NULL,*head2=NULL,*head3=NULL;
    int option;
    float x,value;
    do
    {
        printf("\n1 : create 1'st polynomial");
        printf("\n2 : create 2'nd polynomial");
        printf("\n3 : Add polynomials");
        printf("\n4 : Quit");
        printf("\nEnter your choice :");
        scanf("%d",&option);
        switch(option)
        {
            case 1:head1=read();print(head1);break;
            case 2:head2=read();print(head2);break;
            case 3:head3=add(head1,head2);
                   printf("\n1'st polynomial -> ");
                   print(head1);
                   printf("\n2'nd polynomial -> ");
                   print(head2);
                   printf("\n Sum = ");
                   print(head3);
                   break;
        }
    }while(option!=4);
}
node * read()
{
    int n, i, xpower,ypower;
    float coeff;
    node t;
    node *head=NULL,*p;
    printf("\n Enter number of terms :");
    scanf("%d",&n);
    /* read n terms */
    for (i=0;i<n;i++)
    {

        printf("\nenter a term(powerof x,power of y,  coeff.)");
        scanf("%d%d%f",&xpower,&ypower,&coeff);
        t.xpower=xpower;
        t.ypower=ypower;
        t.coeff=coeff;
        head=insert(head,t);
    }
  return(head);
}
void print(node  *head)
{
    if(head==NULL)
        return;
    printf("%5.2fX^%dY^%d ",head->coeff,head->xpower,head->ypower);
    for(head=head->next;head!=NULL;head=head->next)
       printf(" + %5.2fX^%dY^%d ",head->coeff,head->xpower,head->ypower);
}

node * add(node *head1, node *head2)
{
    node *head3=NULL;
    node t;
    while(head1!=NULL  && head2!=NULL)
    {
        if(head1->xpower==head2->xpower && head1->ypower==head2->ypower)
        {
            t.xpower=head1->xpower;
            t.ypower=head1->ypower;
            t.coeff=head1->coeff+head2->coeff;
            head3=insert(head3,t);
            head1=head1->next;head2=head2->next;
        }
        else
            if(head1->xpower < head2->xpower  ||
               head1->xpower == head2->xpower &&
               head1->ypower < head2->ypower      )
            {
                head3=insert(head3,*head1);
                head1=head1->next;
            }
            else
            {
                head3=insert(head3,*head2);
                head2=head2->next;
            }
    }
    while(head1!=NULL)
    {
         head3=insert(head3,*head1);
         head1=head1->next;
    }
    while(head2!=NULL)
    {
         head3=insert(head3,*head2);
         head2=head2->next;
    }
    return(head3);
}
node * insert(node *head,node t)
{
    int i;
    node *p,*q;
    p=(node*)malloc(sizeof(node));
    *p=t;//t is copied to newly acquired node
    p->next=NULL;
    if(head==NULL || p->xpower < head->xpower ||
             p->xpower == head->xpower && p->ypower<head->ypower)
         {  //insert at the beginning
       p->next=head;
        head=p;
        return(head);
         }

    /*locate the point of insertion*/
    q=head;
    while(q->next!=NULL && ( p->xpower > q->next->xpower ||
                      p->xpower ==q->next->xpower &&
                      p->ypower > q->next->ypower))
          q=q->next;
    //insert
    p->next=q->next;
    q->next=p;
    return(head);
}

Post a Comment

0 Comments