Application of Linked List - Polynomial addition and subtraction
#include<stdio.h>
#include<conio.h>
#inlcude<stdlib.h>
struct poly
{
int c,e;
struct poly *next;
};
struct poly * read_poly(struct poly *header)
{
struct poly *p,*temp;
char ch;
printf("\nDo you want to create a node (y/n) : ");
scanf("%c",&ch);
header->c=NULL;
header->e=NULL;
header->next=NULL;
printf("%c",ch);
while(ch!='n')
{
p=(struct poly *)malloc(sizeof(struct poly));
printf("\nEnter the coefficient value : ");
scanf("%d",&p->c);
printf("\nEnter the exponential value : ");
scanf("%d",&p->e);
p->next=NULL;
if(header->next==NULL)
{
header->next=p;
temp=p;
}
else
{
temp->next=p;
temp=p;
}
printf("\nDo you want to create a node (y/n) : ");
getchar();
scanf("%c",&ch);
}
return header;
}
struct poly * poly_add(struct poly *p,struct poly *q,struct poly *r)
{
p=p->next;
q=q->next;
struct poly *newnode,*temp;
r->next=NULL;
while((p!=NULL)&&(q!=NULL))
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
if(p->e==q->e)
{
newnode->c=p->c+q->c;
//printf("newnode c %d",newnode->c);
newnode->e=p->e;
p=p->next;
q=q->next;
}
else if(p->e>q->e)
{
newnode->c=p->c;
newnode->e=p->e;
p=p->next;
}
else
{
newnode->c=q->c;
newnode->e=q->e;
q=q->next;
}
if(r->next==NULL)
{
r->next=newnode;
temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
if(p!=NULL)
{
while(p!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=p->c;
newnode->e=p->e;
temp->next=newnode;
temp=newnode;
p=p->next;
}
}
if(q!=NULL)
{
while(q!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=q->c;
newnode->e=q->e;
temp->next=newnode;
temp=newnode;
q=q->next;
}
}
}
struct poly * poly_sub(struct poly *p,struct poly *q,struct poly *r)
{
p=p->next;
q=q->next;
struct poly *newnode,*temp;
r->next=NULL;
while((p!=NULL)&&(q!=NULL))
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
if(p->e==q->e)
{
newnode->c=p->c-q->c;
newnode->e=p->e;
p=p->next;
q=q->next;
}
else if(p->e>q->e)
{
newnode->c=p->c;
newnode->e=p->e;
p=p->next;
}
else
{
newnode->c=q->c;
newnode->e=q->e;
q=q->next;
}
if(r->next==NULL)
{
r->next=newnode;
temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
if(p!=NULL)
{
while(p!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=p->c;
newnode->e=p->e;
temp->next=newnode;
temp=newnode;
p=p->next;
}
}
if(q!=NULL)
{
while(q!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=q->c;
newnode->e=q->e;
temp->next=newnode;
temp=newnode;
q=q->next;
}
}
}
void traverse(struct poly *header)
{
struct poly *ptr=header->next;
while(ptr!=NULL)
{
printf(" %dX%d",ptr->c,ptr->e);
ptr=ptr->next;
}
}
int main()
{
//struct poly *header=(struct poly *)malloc(sizeof(struct poly));
//header->next=NULL;
int ch;
struct poly *p,*q,*r;
p=(struct poly *)malloc(sizeof(struct poly));
q=(struct poly *)malloc(sizeof(struct poly));
r=(struct poly *)malloc(sizeof(struct poly));
printf("\n Enter polynomial P : ");
p=read_poly(p);
traverse(p);
printf("\n Enter polynomial Q : ");
getchar();
q=read_poly(q);
traverse(q);
do
{
printf("\n MENU ");
printf("\n 1.Polynomial Addition ");
printf("\n 2.Polynomial Subtraction ");
printf("\n 3.Exit ");
printf("\n Enter your choice..... ");
scanf("%d",&ch);
switch(ch)
{
case 1 : poly_add(p,q,r);
traverse(r);
break;
case 2 : poly_sub(p,q,r);
traverse(r);
break;
case 3 : exit(0);
}
}while(ch!=4);
getchar();
}
MENU
#include<conio.h>
#inlcude<stdlib.h>
struct poly
{
int c,e;
struct poly *next;
};
struct poly * read_poly(struct poly *header)
{
struct poly *p,*temp;
char ch;
printf("\nDo you want to create a node (y/n) : ");
scanf("%c",&ch);
header->c=NULL;
header->e=NULL;
header->next=NULL;
printf("%c",ch);
while(ch!='n')
{
p=(struct poly *)malloc(sizeof(struct poly));
printf("\nEnter the coefficient value : ");
scanf("%d",&p->c);
printf("\nEnter the exponential value : ");
scanf("%d",&p->e);
p->next=NULL;
if(header->next==NULL)
{
header->next=p;
temp=p;
}
else
{
temp->next=p;
temp=p;
}
printf("\nDo you want to create a node (y/n) : ");
getchar();
scanf("%c",&ch);
}
return header;
}
struct poly * poly_add(struct poly *p,struct poly *q,struct poly *r)
{
p=p->next;
q=q->next;
struct poly *newnode,*temp;
r->next=NULL;
while((p!=NULL)&&(q!=NULL))
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
if(p->e==q->e)
{
newnode->c=p->c+q->c;
//printf("newnode c %d",newnode->c);
newnode->e=p->e;
p=p->next;
q=q->next;
}
else if(p->e>q->e)
{
newnode->c=p->c;
newnode->e=p->e;
p=p->next;
}
else
{
newnode->c=q->c;
newnode->e=q->e;
q=q->next;
}
if(r->next==NULL)
{
r->next=newnode;
temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
if(p!=NULL)
{
while(p!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=p->c;
newnode->e=p->e;
temp->next=newnode;
temp=newnode;
p=p->next;
}
}
if(q!=NULL)
{
while(q!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=q->c;
newnode->e=q->e;
temp->next=newnode;
temp=newnode;
q=q->next;
}
}
}
struct poly * poly_sub(struct poly *p,struct poly *q,struct poly *r)
{
p=p->next;
q=q->next;
struct poly *newnode,*temp;
r->next=NULL;
while((p!=NULL)&&(q!=NULL))
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
if(p->e==q->e)
{
newnode->c=p->c-q->c;
newnode->e=p->e;
p=p->next;
q=q->next;
}
else if(p->e>q->e)
{
newnode->c=p->c;
newnode->e=p->e;
p=p->next;
}
else
{
newnode->c=q->c;
newnode->e=q->e;
q=q->next;
}
if(r->next==NULL)
{
r->next=newnode;
temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
if(p!=NULL)
{
while(p!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=p->c;
newnode->e=p->e;
temp->next=newnode;
temp=newnode;
p=p->next;
}
}
if(q!=NULL)
{
while(q!=NULL)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
newnode->c=q->c;
newnode->e=q->e;
temp->next=newnode;
temp=newnode;
q=q->next;
}
}
}
void traverse(struct poly *header)
{
struct poly *ptr=header->next;
while(ptr!=NULL)
{
printf(" %dX%d",ptr->c,ptr->e);
ptr=ptr->next;
}
}
int main()
{
//struct poly *header=(struct poly *)malloc(sizeof(struct poly));
//header->next=NULL;
int ch;
struct poly *p,*q,*r;
p=(struct poly *)malloc(sizeof(struct poly));
q=(struct poly *)malloc(sizeof(struct poly));
r=(struct poly *)malloc(sizeof(struct poly));
printf("\n Enter polynomial P : ");
p=read_poly(p);
traverse(p);
printf("\n Enter polynomial Q : ");
getchar();
q=read_poly(q);
traverse(q);
do
{
printf("\n MENU ");
printf("\n 1.Polynomial Addition ");
printf("\n 2.Polynomial Subtraction ");
printf("\n 3.Exit ");
printf("\n Enter your choice..... ");
scanf("%d",&ch);
switch(ch)
{
case 1 : poly_add(p,q,r);
traverse(r);
break;
case 2 : poly_sub(p,q,r);
traverse(r);
break;
case 3 : exit(0);
}
}while(ch!=4);
getchar();
}
OUTPUT :
Enter polynomial P :
Do you want to create a node (y/n) : y
y
Enter the coefficient value : 6
Enter the exponential value : 2
Do you want to create a node (y/n) : y
Enter the coefficient value : 4
Enter the exponential value : 1
Do you want to create a node (y/n) : y
Enter the coefficient value : 1
Enter the exponential value : 0
Do you want to create a node (y/n) : n
6X2 4X1 1X0
Enter polynomial Q :
Do you want to create a node (y/n) : y
y
Enter the coefficient value : 3
Enter the exponential value : 3
Do you want to create a node (y/n) : y
Enter the coefficient value : 4
Enter the exponential value : 2
Do you want to create a node (y/n) : y
Enter the coefficient value : 3
Enter the exponential value : 1
Do you want to create a node (y/n) : n
3X3 4X2 3X1
MENU
1.Polynomial Addition
2.Polynomial Subtraction
3.Exit
Enter your choice..... 1
3X3 10X2 7X1 1X0
MENU
1.Polynomial Addition
2.Polynomial Subtraction
3.Exit
Enter your choice..... 2
3X3 2X2 1X1 1X0
MENU
1.Polynomial Addition
2.Polynomial Subtraction
3.Exit
Enter your choice....
No comments