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
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
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a term in the polynomial
typedef struct Node {
int coeff;
int exp_x;
int exp_y;
int exp_z;
struct Node* next;
} Node;
// Function to create a new node with given coefficients and exponents
Node* createNode(int coeff, int exp_x, int exp_y, int exp_z) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coeff = coeff;
newNode->exp_x = exp_x;
newNode->exp_y = exp_y;
newNode->exp_z = exp_z;
1
newNode->next = NULL;
return newNode;
// Function to insert a term into the polynomial
void insertTerm(Node** poly, int coeff, int exp_x, int exp_y, int exp_z) {
Node* newNode = createNode(coeff, exp_x, exp_y, exp_z);
if (*poly == NULL) {
*poly = newNode;
newNode->next = *poly;
} else {
Node* temp = *poly;
while (temp->next != *poly) {
temp = temp->next;
temp->next = newNode;
newNode->next = *poly;
// Function to display the polynomial
void displayPoly(Node* poly) {
if (poly == NULL) {
printf("Polynomial is empty\n");
return;
2
}
Node* temp = poly;
do {
printf("%dx^%dy^%dz^%d ", temp->coeff, temp->exp_x, temp->exp_y, temp->exp_z);
if (temp->next != poly) {
printf("+ ");
temp = temp->next;
} while (temp != poly);
printf("\n");
// Function to add two polynomials and store the result in polysum
void addPolynomials(Node* poly1, Node* poly2, Node** polysum) {
Node* temp1 = poly1;
Node* temp2 = poly2;
do {
insertTerm(polysum, temp1->coeff + temp2->coeff, temp1->exp_x, temp1->exp_y, temp1->exp_z);
temp1 = temp1->next;
temp2 = temp2->next;
} while (temp1 != poly1 && temp2 != poly2);
3
int main() {
// Represent and Evaluate Polynomial P(x,y,z) = 6x^2y^2z - 4yz^5 + 3x^3yz + 2xy^5z - 2xyz^3
Node* polyP = NULL;
insertTerm(&polyP, 6, 2, 2, 1);
insertTerm(&polyP, -4, 0, 0, 5);
insertTerm(&polyP, 3, 3, 1, 1);
insertTerm(&polyP, 2, 1, 5, 1);
insertTerm(&polyP, -2, 1, 1, 3);
printf("Polynomial P(x,y,z): ");
displayPoly(polyP);
// Represent and Evaluate Polynomial POLY1(x,y,z) = 2x^2y^2z + 5yz^5 - x^3yz + 3xy^5z
Node* poly1 = NULL;
insertTerm(&poly1, 2, 2, 2, 1);
insertTerm(&poly1, 5, 0, 0, 5);
insertTerm(&poly1, -1, 3, 1, 1);
insertTerm(&poly1, 3, 1, 5, 1);
printf("Polynomial POLY1(x,y,z): ");
displayPoly(poly1);
// Represent and Evaluate Polynomial POLY2(x,y,z) = 4x^2y^2z - 3yz^5 + 2x^3yz - xy^5z + 2xyz^3
Node* poly2 = NULL;
insertTerm(&poly2, 4, 2, 2, 1);
4
insertTerm(&poly2, -3, 0, 0, 5);
insertTerm(&poly2, 2, 3, 1, 1);
insertTerm(&poly2, -1, 1, 5, 1);
insertTerm(&poly2, 2, 1, 1, 3);
printf("Polynomial POLY2(x,y,z): ");
displayPoly(poly2);
// Add POLY1 and POLY2 and store the result in POLYSUM
Node* polySum = NULL;
addPolynomials(poly1, poly2, &polySum);
printf("Sum of POLY1 and POLY2 (POLYSUM(x,y,z)): ");
displayPoly(polySum);
// Free memory
free(polyP);
free(poly1);
free(poly2);
free(polySum);
return 0;
5
OUTPUT