0% found this document useful (0 votes)
17 views

Lab Program 9 Vtu Code

Uploaded by

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

Lab Program 9 Vtu Code

Uploaded by

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

Lab Program 9 :

Develop 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 .

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

struct node
{
int coef;
int xexp, yexp, zexp;
struct node * link;
};
typedef struct node * NODE;

NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if (x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}

NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp -> coef = coef;
temp -> xexp = xexp;
temp -> yexp = yexp;
temp -> zexp = zexp;
cur = head -> link;
while (cur -> link != head)
{
cur = cur -> link;
}
cur -> link = temp;
temp -> link = head;
return head;
}

NODE read_poly(NODE head)


{
int i, j, coef, xexp, yexp, zexp, n;
printf("\nEnter the no of terms in the polynomial: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
{
printf("\n\tEnter the %d term: ", i);
printf("\n\t\tCoef = ");
scanf("%d", & coef);
printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): ");
scanf("%d", & xexp);
scanf("%d", & yexp);
scanf("%d", & zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}

void display(NODE head)


{
NODE temp;
if (head -> link == head)
{
printf("\nPolynomial does not exist.");
return;
}
temp = head -> link;

while (temp != head)


{
printf("%dx^%dy^%dz^%d", temp -> coef, temp -> xexp, temp -> yexp, temp ->
zexp);
temp = temp -> link;
if (temp != head)
printf(" + ");
}
}

int poly_evaluate(NODE head)


{
int x, y, z, sum = 0;
NODE poly;

printf("\nEnter the value of x,y and z: ");


scanf("%d %d %d", & x, & y, & z);

poly = head -> link;


while (poly != head)
{
sum += poly -> coef * pow(x, poly -> xexp) * pow(y, poly -> yexp) * pow(z,
poly -> zexp);
poly = poly -> link;
}
return sum;
}

void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode();
head -> link = head;

printf("\n----Polynomial evaluation P(x,y,z)----\n");


head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;

OUTPUT

.Represent and Evaluate a Polynomial P(x,y,z)

----Polynomial evaluation P(x,y,z)----


Enter the no of terms in the polynomial: 5
Enter the 1 term:
Coef = 6
Enter Pow(x) Pow(y) and Pow(z): 2 2 1
Enter the 2 term:
Coef = -4
Enter Pow(x) Pow(y) and Pow(z): 0 1 5
Enter the 3 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 3 1 1
Enter the 4 term:
Coef = 2
Enter Pow(x) Pow(y) and Pow(z): 1 5 1
Enter the 5 term:
Coef = -2
Enter Pow(x) Pow(y) and Pow(z): 1 1 3
Representation of Polynomial for evaluation:
6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 + -2x^1y^1z^3

Enter the value of x,y and z: 1 1 1


Result of polynomial evaluation is : 5

You might also like