0% found this document useful (0 votes)
76 views4 pages

Polynomial Addition

The document presents a C program for adding two polynomial expressions using linked lists. It defines a structure for polynomial terms and provides functions to create two polynomial lists and add them together. The program includes a menu for user interaction to create lists and perform addition operations.

Uploaded by

rajatmaurya7906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views4 pages

Polynomial Addition

The document presents a C program for adding two polynomial expressions using linked lists. It defines a structure for polynomial terms and provides functions to create two polynomial lists and add them together. The program includes a menu for user interaction to create lists and perform addition operations.

Uploaded by

rajatmaurya7906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ankur Garg LINK LIST OPERATIONS (DATA STRUCTURE)

/* A PROGRAM FOR ADD TWO EXPRESSION */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct poly
{
int coef;
int exp;
struct poly *next;
}*start=NULL,*begin=NULL;

first( )
{
struct poly *node,*temp1;
char exit;
do
{
node=(struct poly*)malloc(sizeof(struct poly));
printf("\n ENTER COFFICIENT: ");
scanf("%d",&node->coef);
printf("\n ENTER EXPONENT: ");
scanf("%d",&node->exp);
node->next=NULL;
temp1=start;
if(start==NULL) start=node;

else
{
while(temp1->next!=NULL)
temp1=temp1->next;

temp1->next=node;
}
printf("\n PRESS 'E' FOR EXIT OR PRESS 'C' FOR CONTINUE\n");
scanf("%s",&exit);
}while(exit=='C' || exit=='c');
}

second( )
{
struct poly *node,*temp2;
char exit;
do
{
node=(struct poly*)malloc(sizeof(struct poly));
printf("\n ENTER COFFICIENT: ");
scanf("%d",&node->coef);
printf("\n ENTER EXPONENT: ");
scanf("%d",&node->exp);
node->next=NULL;
temp2=begin;
…………………………………………………………………………………………. ..……12 of 15
Ankur Garg LINK LIST OPERATIONS (DATA STRUCTURE)
if(begin==NULL) begin=node;

else
{
while(temp2->next!=NULL)
temp2=temp2->next;

temp2->next=node;
}
printf("\n PRESS 'E' FOR EXIT OR PRESS 'C' FOR CONTINUE\n");
scanf("%s",&exit);
}while(exit=='C' || exit=='c');
}

addpoly( )
{
struct poly *node,*temp1,*temp2,*temp3,*add=NULL;

if(start==NULL && begin==NULL)


{
printf("\n NO POLYNOMIAL EXISTS");
getch( ); return 0;
}
else
if(start!=NULL && begin==NULL)
{
temp1=start;

while(temp1!=NULL)
{
printf(" %d",temp1->coef);
printf("X%d ",temp1->exp);
temp1=temp1->next;
}
}
else
if(start==NULL && begin!=NULL)
{
temp2=begin;
while(temp2!=NULL)
{
printf(" %d",temp2->coef);
printf("X%d ",temp2->exp);
temp2=temp2->next;
}
}

else
{
temp1=start; temp2=begin;
while(temp1!=NULL && temp2!=NULL)
{
node=(struct poly*)malloc(sizeof(struct poly));
…………………………………………………………………………………………. ..……13 of 15
Ankur Garg LINK LIST OPERATIONS (DATA STRUCTURE)
if((temp1->exp)>(temp2->exp))
{
node->coef=temp1->coef;
node->exp=temp1->exp;
temp1=temp1->next;
node->next=NULL;
}
else if((temp1->exp)<(temp2->exp))
{
node->coef=temp2->coef;
node->exp=temp2->exp;
temp2=temp2->next;
node->next=NULL;
}
else if(temp1->exp==temp2->exp)
{
node->coef=temp1->coef+temp2->coef;
node->exp=temp1->exp;
node->next=NULL;
temp1=temp1->next;
temp2=temp2->next;
}

temp3=add;
if(add==NULL)
{
add=node;
}
else
{
while(temp3->next!=NULL)
temp3=temp3->next;

temp3->next=node;
}
}
if(temp1==NULL && temp2!=NULL)
{
while(temp2!=NULL)
{
temp3=add;
node=(struct poly*)malloc(sizeof(struct poly));
node->coef=temp2->coef;
node->exp=temp2->exp;
node->next=NULL;
while(temp3->next!=NULL)
temp3=temp3->next;

temp3->next=node;
temp2=temp2->next;
}
}

…………………………………………………………………………………………. ..……14 of 15
Ankur Garg LINK LIST OPERATIONS (DATA STRUCTURE)
else if(temp2==NULL && temp1!=NULL)
{
while(temp1!=NULL)
{
temp3=add;
node=(struct poly*)malloc(sizeof(struct poly));
node->coef=temp1->coef;
node->exp=temp1->exp;
node->next=NULL;
while(temp3->next!=NULL)
temp3=temp3->next;
temp3->next=node;
temp1=temp1->next;
}
}
temp3=add;
while(temp3!=NULL)
{
printf(" %d",temp3->coef);
printf("X%d ",temp3->exp);
temp3=temp3->next;
}
}
getch( );
}
int choice;
void main( )
{
do
{
clrscr( );
printf("\n\t\t A PROGRAMME FOR ADD TWO POLYNOMIALS \n");
printf("\n PLEASE ENTER THE EXPONENTS IN DESCENDING ORDER \n"
" i.e. POWER FUNCTION IN DESCENDING ORDER \n");
printf("\n 1. CREATE FIRST LIST \n");
printf("\n 2. CREATE SECOND LIST \n");
printf("\n 3. ADD TWO LIST \n");
printf("\n 4. EXIT \n");
printf("\n ENTER YOUR CHOICE: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
first( ); break;
case 2:
second( ); break;
case 3:
addpoly( ); break;
default:
printf("ENTER VALID CHOICE");
}
}while(choice<=3&&choice>=1);
}
…………………………………………………………………………………………. ..……15 of 15

You might also like