Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.6 kB
2
Indexable
Never
#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,*new_node,*tempn,*tempd,*newn,*nh;



struct node* createll()
{
    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* add()
{
    poly1=h1;
    poly2=h2;
    poly=(struct node*)malloc(sizeof(struct node));
    nh=NULL; 
    while(poly1->next!=NULL && poly->next!=NULL)
    {
        struct node* newn=(struct node*)malloc(sizeof(struct node));  
        if (poly1->exp > poly2->exp)
        {
            create(poly1);
            poly1=poly1->next;
        }
        else if(poly1->exp < poly2->exp)
        {
            create(poly2);
            poly2=poly2->next;
        }
        else 
        {
            poly->exp=poly1->exp;
            poly->cof=poly1->cof+poly->cof;
            create(poly);
            poly1=poly1->next;
            poly2=poly2->next;
        }
    }
    while (poly1->next || poly2->next)
    {
        if (poly1->next)
        {
            create(poly1);
            poly1=poly1->next;

        }
        if (poly2->next)
        {
            create(poly2);
            poly2=poly2->next;
        }
    }
    return nh;
}
void create(struct node* poly)
{
    struct node* nh=(struct node*)malloc(sizeof(struct node));
    if (nh==NULL)
    {
        nh->cof=poly->cof;
        nh->exp=poly->exp;
        tempn->next=nh;
        tempn = nh;
    }
    newn=(struct node*)malloc(sizeof(struct node));
    newn->cof=poly->cof;
    newn->exp=poly->exp;
    tempn->next=newn;
    tempn = newn;
    newn=NULL;
}
int main()
{
    printf("*****************************************");
    h1=createll();
    h2=createll();
    print();
    tempd=add();
    while(tempd!=NULL)
    {
        printf("%dX^%d \n",tempd->cof,tempd->exp);
        tempd=tempd->next;
    }
    return 0;
}
//PRINT 1x^3+3x^1+2x^0 + 1x^2+2x^1
Leave a Comment