0% found this document useful (0 votes)
25 views14 pages

Practical No15 and 22-27 Algorithms

Uploaded by

Tanisha Waichal
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)
25 views14 pages

Practical No15 and 22-27 Algorithms

Uploaded by

Tanisha Waichal
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/ 14

‭Practical No.

25‬
‭Practical No.24‬
‭Practical No.23‬
‭Practical No.22‬
‭ ractical No.15: *Write a C Program to add Two Polynomials using a Linked‬
P
‭List.‬

‭ include <stdio.h>‬
#
‭#include <stdlib.h>‬

/‭/ Node structure for polynomial‬


‭typedef struct Node {‬
‭int coefficient;‬
‭int exponent;‬
‭struct Node *next;‬
‭} Node;‬

/‭/ Function to create a new node‬


‭Node* createNode(int coefficient, int exponent) {‬
‭Node* newNode = (Node*)malloc(sizeof(Node));‬
‭newNode->coefficient = coefficient;‬
‭newNode->exponent = exponent;‬
‭newNode->next = NULL;‬
‭return newNode;‬
‭}‬

/‭/ Function to insert a node in the linked list‬


‭void insertNode(Node **head, int coefficient, int exponent) {‬
‭Node *newNode = createNode(coefficient, exponent);‬
‭if (*head == NULL) {‬
‭*head = newNode;‬
‭return;‬
‭}‬
‭Node *temp = *head;‬
‭while (temp->next != NULL) {‬
‭temp = temp->next;‬
‭}‬
‭temp->next = newNode;‬
‭}‬

/‭/ Function to display the polynomial‬


‭void displayPolynomial(Node *head) {‬
‭Node *temp = head;‬
‭while (temp != NULL) {‬
‭ rintf("%dx^%d", temp->coefficient, temp->exponent);‬
p
‭if (temp->next != NULL) {‬
‭printf(" + ");‬
‭}‬
‭temp = temp->next;‬
}‭ ‬
‭printf("\n");‬
‭}‬

/‭/ Function to add two polynomials‬


‭Node *addPolynomials(Node *poly1, Node *poly2) {‬
‭Node *result = NULL;‬
‭while (poly1 != NULL && poly2 != NULL) {‬
‭if (poly1->exponent > poly2->exponent) {‬
‭insertNode(&result, poly1->coefficient, poly1->exponent);‬
‭poly1 = poly1->next;‬
‭} else if (poly1->exponent < poly2->exponent) {‬
‭insertNode(&result, poly2->coefficient, poly2->exponent);‬
‭poly2 = poly2->next;‬
‭} else {‬
‭insertNode(&result, poly1->coefficient + poly2->coefficient,‬
‭poly1->exponent);‬
‭poly1 = poly1->next;‬
‭poly2 = poly2->next;‬
‭}‬
‭}‬
‭while (poly1 != NULL) {‬
‭insertNode(&result, poly1->coefficient, poly1->exponent);‬
‭poly1 = poly1->next;‬
‭}‬
‭while (poly2 != NULL) {‬
‭insertNode(&result, poly2->coefficient, poly2->exponent);‬
‭poly2 = poly2->next;‬
‭}‬
‭return result;‬
‭}‬

‭int main() {‬
‭Node *poly1 = NULL, *poly2 = NULL, *sum = NULL;‬
/‭/ Creating first polynomial: 3x^2 + 5x^1 + 6‬
‭insertNode(&poly1, 3, 2);‬
‭insertNode(&poly1, 5, 1);‬
‭insertNode(&poly1, 6, 0);‬

/‭/ Creating second polynomial: 2x^1 + 4‬


‭insertNode(&poly2, 2, 1);‬
‭insertNode(&poly2, 4, 0);‬

/‭/ Adding the two polynomials‬


‭sum = addPolynomials(poly1, poly2);‬

/‭/ Displaying the results‬


‭printf("Polynomial 1: ");‬
‭displayPolynomial(poly1);‬
‭printf("Polynomial 2: ");‬
‭displayPolynomial(poly2);‬
‭printf("Sum: ");‬
‭displayPolynomial(sum);‬

‭return 0;‬
‭}‬

‭Output:‬
‭Practical Related Questions:‬

‭ . Write a C program to Create two polynomial P(x) = 3x4 + 2x3 - 4 x2 + 7 and‬


1
‭Q (x) = 5x3 +4 x2 - 5.‬

‭ include <stdio.h>‬
#
‭#include <stdlib.h>‬

/‭/ Node structure for polynomial‬


‭typedef struct Node {‬
‭int coefficient;‬
‭int exponent;‬
‭struct Node* next;‬
‭} Node;‬

/‭/ Function to create a new node‬


‭Node* createNode(int coefficient, int exponent) {‬
‭Node* newNode = (Node*)malloc(sizeof(Node));‬
‭newNode->coefficient = coefficient;‬
‭newNode->exponent = exponent;‬
‭newNode->next = NULL;‬
‭return newNode;‬
‭}‬

/‭/ Function to insert a node in the linked list‬


‭void insertNode(Node** head, int coefficient, int exponent) {‬
‭Node* newNode = createNode(coefficient, exponent);‬
‭if (*head == NULL) {‬
‭*head = newNode;‬
‭return;‬
‭}‬
‭Node* temp = *head;‬
‭while (temp->next != NULL) {‬
‭temp = temp->next;‬
‭}‬
‭temp->next = newNode;‬
‭}‬

/‭/ Function to display the polynomial‬


‭void displayPolynomial(Node* head) {‬
‭Node* temp = head;‬
‭while (temp != NULL) {‬
‭printf("%dx^%d", temp->coefficient, temp->exponent);‬
‭if (temp->next != NULL) {‬
‭printf(" + ");‬
‭}‬
‭temp = temp->next;‬
‭}‬
‭printf("\n");‬
‭}‬

‭int main() {‬
‭Node* P = NULL; // Polynomial P(x)‬
‭Node* Q = NULL; // Polynomial Q(x)‬

/‭/ Creating polynomial P(x) = 3x^4 + 2x^3 - 4x^2 + 7‬


‭insertNode(&P, 3, 4);‬
‭insertNode(&P, 2, 3);‬
‭insertNode(&P, -4, 2);‬
‭insertNode(&P, 7, 0);‬

/‭/ Creating polynomial Q(x) = 5x^3 + 4x^2 - 5‬


‭insertNode(&Q, 5, 3);‬
‭insertNode(&Q, 4, 2);‬
‭insertNode(&Q, -5, 0);‬

‭ rintf("Polynomial P(x): ");‬


p
‭displayPolynomial(P);‬

‭ rintf("Polynomial Q(x): ");‬


p
‭displayPolynomial(Q);‬

‭return 0;‬
‭}‬

‭Output:‬
‭2. Write a C program to display addition of two created polynomial.‬

‭ include <stdio.h>‬
#
‭#include <stdlib.h>‬

/‭/ Node structure for polynomial‬


‭typedef struct Node {‬
‭int coefficient;‬
‭int exponent;‬
‭struct Node* next;‬
‭} Node;‬

/‭/ Function to create a new node‬


‭Node* createNode(int coefficient, int exponent) {‬
‭Node* newNode = (Node*)malloc(sizeof(Node));‬
‭newNode->coefficient = coefficient;‬
‭newNode->exponent = exponent;‬
‭newNode->next = NULL;‬
‭return newNode;‬
‭}‬

/‭/ Function to insert a node in the linked list‬


‭void insertNode(Node** head, int coefficient, int exponent) {‬
‭Node* newNode = createNode(coefficient, exponent);‬
‭if (*head == NULL) {‬
‭*head = newNode;‬
‭return;‬
‭}‬
‭Node* temp = *head;‬
‭while (temp->next != NULL) {‬
‭temp = temp->next;‬
‭}‬
‭temp->next = newNode;‬
‭}‬

/‭/ Function to display the polynomial‬


‭void displayPolynomial(Node* head) {‬
‭Node* temp = head;‬
‭while (temp != NULL) {‬
‭printf("%dx^%d", temp->coefficient, temp->exponent);‬
‭if (temp->next != NULL) {‬
‭printf(" + ");‬
‭}‬
‭temp = temp->next;‬
}‭ ‬
‭printf("\n");‬
‭}‬

/‭/ Function to add two polynomials‬


‭Node* addPolynomials(Node* poly1, Node* poly2) {‬
‭Node* result = NULL;‬
‭while (poly1 != NULL && poly2 != NULL) {‬
‭if (poly1->exponent > poly2->exponent) {‬
‭insertNode(&result, poly1->coefficient, poly1->exponent);‬
‭poly1 = poly1->next;‬
‭} else if (poly1->exponent < poly2->exponent) {‬
‭insertNode(&result, poly2->coefficient, poly2->exponent);‬
‭poly2 = poly2->next;‬
‭} else {‬
‭insertNode(&result, poly1->coefficient + poly2->coefficient,‬
‭poly1->exponent);‬
‭poly1 = poly1->next;‬
‭poly2 = poly2->next;‬
‭}‬
‭}‬
‭while (poly1 != NULL) {‬
‭insertNode(&result, poly1->coefficient, poly1->exponent);‬
‭poly1 = poly1->next;‬
‭}‬
‭while (poly2 != NULL) {‬
‭insertNode(&result, poly2->coefficient, poly2->exponent);‬
‭poly2 = poly2->next;‬
‭}‬
‭return result;‬
‭}‬

‭int main() {‬
‭Node* P = NULL; // Polynomial P(x)‬
‭Node* Q = NULL; // Polynomial Q(x)‬
‭Node* Sum = NULL; // Resultant polynomial‬
/‭/ Creating polynomial P(x) = 3x^4 + 2x^3 - 4x^2 + 7‬
‭insertNode(&P, 3, 4);‬
‭insertNode(&P, 2, 3);‬
‭insertNode(&P, -4, 2);‬
‭insertNode(&P, 7, 0);‬

/‭/ Creating polynomial Q(x) = 5x^3 + 4x^2 - 5‬


‭insertNode(&Q, 5, 3);‬
‭insertNode(&Q, 4, 2);‬
‭insertNode(&Q, -5, 0);‬

/‭/ Adding the two polynomials‬


‭Sum = addPolynomials(P, Q);‬

‭ rintf("Polynomial P(x): ");‬


p
‭displayPolynomial(P);‬

‭ rintf("Polynomial Q(x): ");‬


p
‭displayPolynomial(Q);‬

‭ rintf("Sum of P(x) and Q(x): ");‬


p
‭displayPolynomial(Sum);‬

‭return 0;‬
‭}‬

‭Output:‬

You might also like