Untitled
unknown
plain_text
a year ago
2.9 kB
13
Indexable
#include <stdio.h>
#include <stdlib.h>
//creating a structure to define the datatype of the list
struct node
{
int cof;
int exp;
struct node* next ;
}*new_node1,*head1,*temp1,*h1,*h2,*poly1,*poly2,*poly,*trav;
struct node* create()
{
int i1,jcof1,jexp1,n1,hcof1,hexp1;
head1=(struct node*)malloc(sizeof(struct node));
if (head1==NULL)
{
printf("no memory ");
exit(0);
}
printf("enter the cof for the first element of the polynomial:");
scanf("%d",&hcof1);
printf("enter the exponent for the first element of the polynomial:");
scanf("%d",&hexp1);
head1 -> cof = hcof1;
head1 -> exp = hexp1;
head1 -> next = NULL;
temp1=head1;
printf("enter the number of elements after the first element:");
scanf("%d",&n1);
for(i1=0;i1<n1;i1++)
{
new_node1=(struct node*)malloc(sizeof(struct node));
if (new_node1==NULL)
{
printf("no memory");
break;
}
printf("enter the coefficent of the %d element",i1+1);
scanf("%d",&jcof1);
printf("enter the exponent of the %d element",i1+1);
scanf("%d",&jexp1);
new_node1 -> cof = jcof1;
new_node1 -> exp =jexp1;
new_node1 -> next= NULL;
temp1 -> next = new_node1;
temp1 = new_node1;
new_node1=NULL;
}
return head1;
}
struct node* print()
{
printf("The elements of first polynomial:\n");
poly1=h1;
while(poly1!=NULL)
{
printf("%dX^%d \n",poly1->cof,poly1->exp);
poly1=poly1->next;
}
printf("The elements of second polynomial:\n");
poly2=h2;
while(poly2!=NULL)
{
printf("%dX^%d \n",poly2->cof,poly2->exp);
poly2=poly2->next;
}
}
struct node* multiply()
{
poly1=h1;
poly2=h2;
struct node* nh=(struct node*)malloc(sizeof(struct node));
nh=NULL;
while(poly1!=NULL)
{
poly2=h2;
while(poly2!=NULL)
{
struct node* nn=(struct node*)malloc(sizeof(struct node));
nn->cof=poly1->cof * poly2->cof;
nn->exp=poly1->exp + poly2->exp;
nn->next=NULL;
if (nh==NULL)
{
nh=nn;
poly=nh;
}
else
{
poly->next=nn;
poly=poly->next;
}
poly2=poly2->next;
}
poly1=poly1->next;
}
return nh;
}
int main()
{
h1=create();
h2=create();
print();
printf("***********************************************\n");
trav=multiply();
while(trav!=NULL)
{
printf("%dx^%d+",trav->cof,trav->exp);
trav=trav->next;
}
return 0;
}
// print 2x^2+3x^1+4x^0 * 3x^3+2x^0Editor is loading...
Leave a Comment