problem in a program

hey the following c code executes properly except case 4.This is the code for polynomial operations using linked list.So can anyone tell me my mistake😔

#include
#include
#include
#include
int power(int x,int y);
struct pnode
{
int coeff;
int pow;
struct pnode *link;
}*temp1,*temp2,*temp3,*head1,*head2,*head3,*temp,*prev,*newnode;
void main()
{
int sum,a,b,x,y,ch,n,m,i;
do
{
printf("\n linked list operations\n1.creation of the 1st polynomials\n2.creation of the second polynomial\n3.display\n4.addition\n5.multiplication\n6.\evaluation\nenter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the no of terms in the first polynomial: ");
scanf("%d",&n);
for(i=0;i {
printf("\n enter the coeff and the pow: ");
scanf("%d%d",&a,&b);
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=a;
newnode->pow=b;
newnode->link=NULL;
if(head1==NULL)
{
head1=newnode;
prev=newnode;
}
else
{
prev->link=newnode;
prev=newnode;
}
}
break;
case 2:
printf("\n enter the no of terms in the second polynomial: ");
scanf("%d",&m);
for(i=0;i {
printf("\n enter the coeff and the pow: ");
scanf("%d%d",&a,&b);
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=a;
newnode->pow=b;
newnode->link=NULL;
if(head2==NULL)
{
head2=newnode;
prev=newnode;
}
else
{
prev->link=newnode;
prev=newnode;
}
}
break;
case 3:
printf("\n the first polynomial is: ");
temp=head1;
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
temp=temp->link;
}
printf("\n the 2nd polynomial is: ");
temp=head2;
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
temp=temp->link;
}
break;
case 4:
while(head1!=NULL)
{
temp=head2;
while(temp!=NULL)
{
if(head1->pow==temp->pow)
{
x=head1->coeff+temp->coeff;
y=head1->pow;
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=x;
newnode->pow=y;
newnode->link=NULL;
if(head3==NULL)
{
head3=newnode;
prev=newnode;
}
else
{
prev->link=newnode;
prev=newnode;
}
}
else
{
temp=temp->link;
}
}
if(temp==NULL)
{
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->link=NULL;
newnode=head1;
if(head3==NULL)
{
head3=newnode;
prev=newnode;
}
else
{
prev->link=newnode;
prev=newnode;
}
}
head1=head1->link;
}
while(head2!=NULL)
{
temp=head1;
while(temp!=NULL)
{
if(head2->pow==temp->pow)
{
break;
}
else
{
temp=temp->link;
}
}
if(temp==NULL)
{
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->link=NULL;
newnode=head2;
if(head3==NULL)
{
prev=head3=newnode;
}
else
{
prev->link=newnode;
prev=newnode;
}
}
head2=head2->link;
}
printf("\n the addition of polynomials is: ");
temp=head3;
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
temp=temp->link;
}
break;
case 5:
for(temp1=head1;temp1!=NULL;temp1=temp1->link)
{
for(temp2=head2;temp2!=NULL;temp2=temp2->link)
{
x=temp1->coeff*temp2->coeff;
y=temp1->pow+temp2->pow;
newnode=(struct pnode*)malloc(sizeof(struct pnode));
newnode->coeff=x;
newnode->pow=y;
newnode->link=NULL;
if(head3==NULL)
{
head3=newnode;
prev=newnode;
}
else
{
temp3=head3;
while(temp3!=NULL)
{
if(temp3->pow==y)
{
temp3->coeff=temp3->coeff+x;
break;
}
else
{
temp3=temp3->link;
}
}
if(temp3==NULL)
{
prev->link=newnode;
prev=newnode;
}
}
}
}
printf("\n the multiplication of polynomials is: ");
temp=head3;
while(temp!=NULL)
{
printf("%dx^%d+",temp->coeff,temp->pow);
temp=temp->link;
}
break;
case 6:
printf("\n enter the value of x: ");
scanf("%d",&x);
sum=0;
for(temp1=head1;temp1!=NULL;temp1=temp1->link)
{
sum=sum+temp1->coeff*power(x,temp1->pow);
}
printf("\n the result after evaluation is: %d",sum);
break;
}
}while(ch<7);
}
int power(int x,int y)
{
int i,p=1;
for(i=0;i {
p=x*p;
}
return p;
}

Replies

You are reading an archived discussion.

Related Posts

click to view Computer Ads but Old age by Mahesh Dahale
study in abroad Forms section where Student [FONT="]Study[/FONT] at a Foreign Institution give some information about their university programs and where in this section[FONT="][/FONT] will help CEans become familiar with...
How to simulate an Pick and Place Robot in pro-e..... is there an option to simulate by giving the value of degress of freedom.
CEans, Any Bank of India customers here - please read this carefully! I'm suspecting a phishing scam being run online. I received following email that looks very authentic! But it...
hey guys i have started this thread so that people can start reading books other than the fiction,fantasy,and other novels. i am a crazy Manga Fan.😁 For those who don't...