1.Polynomial addition using linked list
#include
#include
#include
struct Node
{
int coef,exp;
struct Node *next;
}*head1=NULL,*head2=NULL,*head3=NULL;
void read1();
void read2();
void add();
void display();
void read1()
{
struct Node *p;
int y,i,n;
printf("enter the
highest exponent of polynomial1\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
printf("enter the coeffient of x%d\n",i);
scanf("%d",&y);
p=(struct Node
*)malloc(sizeof(struct Node));
p->coef=y;
p->exp=i;
p->next=NULL;
if(head1==NULL)
{
head1=p;
}
else
{
struct
Node *temp;
temp=head1;
while(temp->next!=NULL)
temp=temp->next;
temp->next=p;
}
}
}
void read2()
{
struct Node *p;
int y,i,n;
printf("enter the
highest exponent of polynomial2\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
printf("enter the coeffient of x%d\n",i);
scanf("%d",&y);
p=(struct
Node *)malloc(sizeof(struct Node));
p->coef=y;
p->exp=i;
p->next=NULL;
if(head2==NULL)
{
head2=p;
}
else
{
struct
Node *temp;
temp=head2;
while(temp->next!=NULL)
temp=temp->next;
temp->next=p;
}
}
}
void add()
{
struct Node *p1,*p2,*temp,*p;
p1=head1;
p2=head2;
while(p1!=NULL &&
p2!=NULL)
{
if(p1->exp==p2->exp)
{
p=(struct Node
*)malloc(sizeof(struct Node));
p->coef=p1->coef+p2->coef;
p->exp=p1->exp;
p->next=NULL;
if(head3==NULL)
head3=p;
else
{
temp=head3;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p1=p1->next;
p2=p2->next;
}
}
else
if(p1->exp>p2->exp)
{
p=(struct
Node *)malloc(sizeof(struct Node));
p->coef=p1->coef;
p->exp=p1->exp;
p->next=NULL;
if(head3==NULL)
head3=p;
else
{
temp=head3;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p1=p1->next;
}
}
else
if(p2->exp>p1->exp)
{
p=(struct Node
*)malloc(sizeof(struct Node));
p->coef=p2->coef;
p->exp=p2->exp;
p->next=NULL;
if(head3==NULL)
head3=p;
else
{
temp=head3;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p2=p2->next;
}
}
}
while(p1!=NULL)
{
p=(struct
Node *)malloc(sizeof(struct Node));
p->coef=p1->coef;
p->exp=p1->exp;
p->next=NULL;
if(head3==NULL)
head3=p;
else
{
temp=head3;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p1=p1->next;
}
}
while(p2!=NULL)
{
p=(struct
Node *)malloc(sizeof(struct Node));
p->coef=p2->coef;
p->exp=p2->exp;
p->next=NULL;
if(head3==NULL)
head3=p;
else
{
temp=head3;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p2=p2->next;
}
}
}
void display()
{
struct Node *temp;
temp=head3;
while(temp!=NULL){
printf("%dx%d+",temp->coef,temp->exp);
temp=temp->next;
}
}
int main()
{
int opt;
do
{
printf("\n enter
the choice\n");
printf("1.read firdt
poli\n");
printf("2.read
second poli\n");
printf("3.addition\n");
printf("4.display\n");
printf("5.exit\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
read1();
break;
case 2:
read2();
break;
case 3:
add();
break;
case 4:
display();
break;
case 5:
break;
default:
break;
}
}while(opt!=5);
}
Output
enter the choice
1.read1
2.read2
3.addition
4.display
5.exit
1
enter the highest exponent of
polynomial1
5
enter the coeffient of x5
5
enter the coeffient of x4
5
enter the coeffient of x3
5
enter the coeffient of x2
5
enter the coeffient of x1
5
enter the coeffient of x0
5
enter the choice
1.read1
2.read2
3.addition
4.display
5.exit
2
enter the highest exponent of
polynomial2
3
enter the coeffient of x3
3
enter the coeffient of x2
3
enter the coeffient of x1
3
enter the coeffient of x0
3
enter the choice
1.read1
2.read2
3.addition
4.display
5.exit
3
enter the choice
1.read1
2.read2
3.addition
4.display
5.exit
4
5x5+5x5+5x4+8x3+8x2+8x1+8x0+
enter the choice
1.read1
2.read2
3.addition
4.display
5.exit
0 comments:
Post a Comment