DSA Program 9
DSA Program 9
Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes.
a) Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3.
b) Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations.
C Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int m,n;
struct node
{
int coef;
int ex1,ex2,ex3;
struct node *link;
};
ppointer a,b,c,temp;
case 1: headc=attach(starta->coef,starta->ex1,starta->ex2,starta->ex3,headc);
starta = starta->link;
i++;
if(starta==heada)
done=1;
break;
case -1: headc=attach(startb->coef,startb->ex1,startb->ex2,startb->ex3,headc);
startb = startb->link;
j++;
if(startb==headb)
done=1;
break;
}
}while(!done);
while(i<m)
{
headc=attach(starta->coef,starta->ex1,starta->ex2,starta->ex3,headc);
starta=starta->link;
i++;
}
while(j<n)
{
headc=attach(startb->coef,startb->ex1,startb->ex2,startb->ex3,headc);
startb=startb->link;
j++;
}
return headc;
}
int main()
{
int ch,num,co,e1,e2,e3,i,j,result;
clrscr();
while(1)
{
ppointer heada,headb,headc,heade;
heada=getnode();
headb=getnode();
heade=getnode();
heada->coef=heada->ex1=heada->ex2=heada->ex3=-1;
headb->coef=headb->ex1=headb->ex2=headb->ex3=-1;
heade->coef=heade->ex1=heade->ex2=heade->ex3=-1;
heada->link=heada;
headb->link=headb;
heade->link=heade;
printf("\nPolynomial Operations\n1.Evaluation of polynomial\n
2.addition of two polynomials\n3.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number of terms in the polynomial\n");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("Enter the coefficient and three exponents of x,y,z\n");
scanf("%d%d%d%d",&co,&e1,&e2,&e3);
heade=attach(co,e1,e2,e3,heade);
}
display(heade);
result=eval(heade);
printf("The value of polynomial is %d\n",result);
break;
case 2: printf("Enter the number of terms in polynomial a\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the coefficient and three exponents of x,y,z\n");
scanf("%d%d%d%d",&co,&e1,&e2,&e3);
heada=attach(co,e1,e2,e3,heada);
}
printf("Enter the number of terms in polynomial b\n");
scanf("%d",&n);
for(j=0;j<n;j++)
{
printf("Enter the coefficient and three exponents of x,y and z\n");
scanf("%d%d%d%d",&co,&e1,&e2,&e3);
headb=attach(co,e1,e2,e3,headb);
}
printf("\n\npolynomial a is\n");
display(heada);
printf("\n\npolynomial b is\n");
display(headb);
headc=add(heada,headb);
printf("\n\nThe resulting polynomial c is\n");
display(headc);
break;
case 3: exit(0);
default: printf("Invalid choice\n");
}
}
return 0;
}
OUTPUT 1:
Polynomial Operations
1. Evaluation of polynomial
2. Addition of two polynomials
3. Exit
Enter your choice
1
Enter the number of terms in the polynomial
2
Enter the coefficient and three exponents of x,y,z
6 3 2 1
Enter the coefficient and three exponents of x,y,z
5 2 1 0
6x^3y^2z^1 + 5x^2y^1z^0
Enter the value of x,y,z
1 2 1
The value of polynomial is 34
OUTPUT 2:
Polynomial Operations
1. Evaluation of polynomial
2. Addition of two polynomials
3. Exit
Enter your choice
1
Enter the number of terms in the polynomial
3
Enter the coefficient and three exponents of x,y,z
2 3 2 1
Enter the coefficient and three exponents of x,y,z
3 2 1 0
Enter the coefficient and three exponents of x,y,z
4 1 0 0
2x^3y^2z^1 + 3x^2y^1z^0 + 4x^1y^0z^0
Enter the value of x,y,z
1 2 1
The value of polynomial is 18
OUTPUT 3:
Polynomial Operations
1. Evaluation of polynomial
2. Addition of two polynomials
3. Exit
Enter your choice
2
Enter the number of terms in polynomial a
3
Enter the coefficient and three exponents of x,y,z
5 3 2 1
Enter the coefficient and three exponents of x,y,z
6 2 1 0
Enter the coefficient and three exponents of x,y,z
7 1 0 0
Enter the number of terms in polynomial b
3
Enter the coefficient and three exponents of x,y,z
7 4 3 2
Enter the coefficient and three exponents of x,y,z
5 2 1 0
Enter the coefficient and three exponents of x,y,z
8 0 1 0
Polynomial a is
5x^3y^2z^1 + 6x^2y^1z^0 + 7x^1y^0z^0
Polynomial b is
7x^4y^3z^2 + 5x^2y^1z^0 + 8x^0y^1z^0
The resulting polynomial c is
7x^4y^3z^2 + 5x^3y^2z^1 + 11x^2y^1z^0 + 7x^1y^0z^0 + 8x^0y^1z^0
OUTPUT 4:
Polynomial Operations
1. Evaluation of polynomial
2. Addition of two polynomials
3. Exit
Enter your choice
2
Enter the number of terms in polynomial a
2
Enter the coefficient and three exponents of x,y,z
3 3 2 1
Enter the coefficient and three exponents of x,y,z
4 1 0 0
Enter the number of terms in polynomial b
2
Enter the coefficient and three exponents of x,y,z
2 3 2 1
Enter the coefficient and three exponents of x,y,z
6 1 1 1
Polynomial a is
3x^3y^2z^1 + 4x^1y^0z^0
Polynomial b is
2x^3y^2z^1 + 6x^1y^1z^1
The resulting polynomial c is
5x^3y^2z^1 + 6x^1y^1z^1 + 4x^1y^0z^0