Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Polynomial Addition and Subtraction Batch-A

#include<stdio.h>
#include<memory.h>
#include<stdlib.h>

struct Element
{
int coeff;
int pwvalue;
struct Element *next;
};

struct Element *fp=NULL,*sp=NULL,*tp=NULL;
struct Element * addition(struct Element *,struct Element *);
struct Element * subtraction(struct Element *,struct Element *);
struct Element * createLL(int );
void printLL(struct Element *);

int main()
{
int hp1,hp2;
struct Element *fhead=NULL,*shead=NULL, *thead=NULL;
printf("\n Welcome to Polynomial Addition & Subtraction Program : \n");
printf("\n You are about to insert a Values of polynomial  : \n");
printf("\n Enter Highest Power of First Polynomial : ");
scanf("%d",&hp1);
fp=createLL(hp1);

printf("\n Enter Highest Power of Second Polynomial : ");
scanf("%d",&hp2);
sp=createLL(hp2);

fhead=fp;
shead=sp;

printLL(fhead);
printLL(shead);

printf("\n================================");
printf("\n Polynomial Addition \n");

fhead=fp;
shead=sp;

tp=addition(fhead,shead);

thead=tp;
printLL(thead);

printf("\n================================");
printf("\n Polynomial Subtraction \n");

fhead=fp;
shead=sp;
tp=subtraction(fhead,shead);

thead=tp;
printLL(thead);

return 0;
}

struct Element * addition(struct Element *fhead,struct Element *shead)
{
int hp3;
struct Element *head=NULL,*temp=NULL;
struct Element *tp=NULL;

hp3=(fhead->pwvalue > shead->pwvalue ? fhead->pwvalue : shead->pwvalue);

while(hp3>-1)
{
temp=(struct Element *) malloc (sizeof(struct Element) * 1);
temp->pwvalue=hp3;
temp->next=NULL;
if(fhead->pwvalue==shead->pwvalue)
{
temp->coeff = fhead->coeff + shead->coeff;
fhead=fhead->next;
shead=shead->next;

}
else if(fhead->pwvalue > shead->pwvalue)
{
temp->coeff = fhead->coeff;
fhead=fhead->next;

}
else
{
temp->coeff = shead->coeff;
shead=shead->next;

}

if(tp==NULL)
{
tp=temp;
}
else
{
head=tp;
while(head->next!=NULL)
{
head=head->next;
}
head->next=temp;
}
hp3--;
}
return tp;
}

struct Element * subtraction(struct Element *fhead,struct Element *shead)
{
int hp3;
struct Element *head=NULL,*temp=NULL;

struct Element *tp=NULL;

hp3=(fhead->pwvalue > shead->pwvalue ? fhead->pwvalue : shead->pwvalue);

while(hp3>-1)
{
temp=(struct Element *) malloc (sizeof(struct Element) * 1);
temp->pwvalue=hp3;
temp->next=NULL;
if(fhead->pwvalue==shead->pwvalue)
{
temp->coeff = fhead->coeff - shead->coeff;
fhead=fhead->next;
shead=shead->next;

}
else if(fhead->pwvalue > shead->pwvalue)
{
temp->coeff = fhead->coeff;
fhead=fhead->next;

}
else
{
temp->coeff = -(shead->coeff);
shead=shead->next;

}

if(tp==NULL)
{
tp=temp;
}
else
{
head=tp;
while(head->next!=NULL)
{
head=head->next;
}
head->next=temp;
}
hp3--;
}
return tp;
}

struct Element * createLL(int noOfNode)
{
struct Element *first=NULL,*temp=NULL, *head=NULL;
while(noOfNode>-1)
{
//Create new Node
temp=(struct Element *) malloc (sizeof(struct Element) * 1);
temp->pwvalue=noOfNode;
printf("\n Enter Coefficient of = __ X%d : ",noOfNode);
scanf("%d",&temp->coeff);
temp->next=NULL;

//To Attach Node in LL
if(first==NULL)
{
first=temp;
}
else
{
head=first;
while(head->next!=NULL)
{
head=head->next;
}
head->next=temp;
}
noOfNode--;
}
return first;
}

void printLL(struct Element *head)
{
printf("\n");
while(head!=NULL)
{
printf("%d X%d -> ",head->coeff,head->pwvalue);
head=head->next;
}
}

Comments