Slip 27_2 : Write a program that adds two single variable polynomials. Each polynomial should be represented as a list with linked list implementation.

 Solution :

#include<stdio.h>
#include<stdlib.h>
struct node
{ int coeff,exp;
 struct node *next;
};
struct node* create(struct node *f)
{
 int i,n;
 struct node *s;
 printf("\nEnter no of terms ");
 scanf("%d",&n);
 printf("Enter term in descending order of power ");
 f=(struct node *)malloc(sizeof(struct node));
 printf("\n Enter coeff");
 scanf("%d",&f->coeff);
 printf("\n Enter power ");
 scanf("%d",&f->exp);
 s=f;
 for(i=1;i<n;i++)
 {
 s->next=(struct node *)malloc(sizeof(struct node));
 s=s->next;
 printf("\n Enter coeff");
 scanf("%d",&s->coeff);
 printf("\n Enter power ");
 scanf("%d",&s->exp);
 }
 s->next=NULL;
 return f;
}
void display(struct node *f)
{ struct node *s;
 for(s=f;s!=NULL;s=s->next)
 { printf("%dx^%d ->",s->coeff,s->exp);
 }
}
struct node *Add(struct node *p1,struct node *p2)
{
 struct node *t1,*t2,*t3=NULL,*nw;
 struct node *p3;
 t1=p1;t2=p2;
 printf("\n%d %d",t1->exp,t2->exp);
 while(t1!=NULL && t2!=NULL)
 { nw=(struct node*)malloc(sizeof(struct node));
 nw->next=NULL;
 if(t1->exp > t2->exp)
 {
 nw->exp=t1->exp;
 nw->coeff=t1->coeff;
 t1=t1->next;
 }
 else if(t2->exp > t1->exp)
 {
 nw->exp=t2->exp;
 nw->coeff=t2->coeff;
 t2=t2->next;
 }
 else
 { nw->exp=t1->exp;
 nw->coeff=t1->coeff+t2->coeff;
 t1=t1->next;
 t2=t2->next;
 }
if(t3==NULL)
 { p3=nw;
 t3=nw;
 }
 else
 { t3->next=nw;
 t3=t3->next;
 }
 }
 while(t1!=NULL)
 {
 nw=(struct node*)malloc(sizeof(struct node));
 nw->next=NULL;
 nw->exp=t1->exp;
 nw->coeff=t1->coeff;
 t1=t1->next;
 t3->next=nw;;
 t3=t3->next;
 }
 while(t2!=NULL)
 {
 nw=(struct node*)malloc(sizeof(struct node));
 nw->next=NULL;
 nw->exp=t2->exp;
 nw->coeff=t2->coeff;
 t2=t2->next;
 t3->next=nw;
 t3=t3->next;
 }
 return p3;
}
main()
{ struct node *p1=NULL,*p2=NULL,*p3=NULL;
 p1=create(p1);
 p2=create(p2);
 printf("\n 1st Polynomial is : ");
 display(p1);
 printf("\n 2nd Polynomial is : ");
 display(p2);
 p3=Add(p1,p2);
 printf("\n Addition of 2 Polynomial is ");
 display(p3);
}

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 10_1 ,22_1: Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes queue library and calls different queue operations