0% found this document useful (0 votes)
20 views7 pages

DSA Program 9

The document outlines a C program for performing operations on a Singly Circular Linked List (SCLL) representing polynomials. It includes functionalities for evaluating a polynomial and adding two polynomials, with appropriate functions for each operation. The program allows user interaction to input polynomial terms and displays results accordingly.

Uploaded by

sagarsriram535
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)
20 views7 pages

DSA Program 9

The document outlines a C program for performing operations on a Singly Circular Linked List (SCLL) representing polynomials. It includes functionalities for evaluating a polynomial and adding two polynomials, with appropriate functions for each operation. The program allows user interaction to input polynomial terms and displays results accordingly.

Uploaded by

sagarsriram535
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/ 7

Laboratory Program – 09

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;
};

typedef struct node *ppointer;

ppointer a,b,c,temp;

int compare(int x1,int y1,int z1,int x2,int y2,int z2)


{
if((x1==x2)&&(y1==y2)&&(z1==z2))
return 0;
else if(x1>x2)
return 1;
else if(x1<x2)
return -1;
else if(y1>y2)
return 1;
else if(y1<y2)
return -1;
else if(z1>z2)
return 1;
else
return -1;
}
ppointer getnode()
{
ppointer x;
x=(ppointer)malloc(sizeof(struct node));
return x;
}

ppointer attach(int c,int e1,int e2,int e3,ppointer head)


{
ppointer cur;
temp=getnode();
temp->coef=c;
temp->ex1=e1;
temp->ex2=e2;
temp->ex3=e3;
temp->link=NULL;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}

ppointer add(ppointer heada,ppointer headb)


{
ppointer starta,startb,headc;
int sum=0,done=0,i=0,j=0;
starta=heada->link;
startb=headb->link;
headc=getnode();
headc->coef=headc->ex1=headc->ex2=headc->ex3=-1;
headc->link=headc;
do
{
switch(compare(starta->ex1,starta->ex2,starta->ex3,startb->ex1,
startb->ex2,startb->ex3))
{
case 0: sum = starta->coef + startb->coef;
if(sum)
headc=attach(sum,starta->ex1,starta->ex2,starta->ex3,headc);
starta=starta->link;
startb=startb->link;
i++;
j++;
if(starta==heada||startb==headb)
done=1;
break;

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;
}

void display(ppointer head)


{
ppointer temp1;
temp1=head->link;
if(temp1==head)
{
printf("\nthere are no terms in polynomial to display\n");
getch();
exit(0);
}
else
{
while(temp1!=head)
{
printf("%dx^%dy^%dz^%d",temp1->coef,temp1->ex1,temp1->ex2,temp1->ex3);
temp1=temp1->link;
if(temp1!=head)
printf("+");
}
}
}

int eval(ppointer head)


{
ppointer temp1;
int x,y,z,ans=0;
printf("\nEnter the value of x,y,z\n");
scanf("%d%d%d",&x,&y,&z);
temp1=head->link;
while(temp1!=head)
{
ans=ans+temp1->coef*pow(x,temp1->ex1)*pow(y,temp1->ex2)*pow(z,temp1->ex3);
temp1=temp1->link;
}
return ans;
}

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

You might also like