1gc22cs154 Mini Project
1gc22cs154 Mini Project
Polynomial Examples
Definition: A polynomial (p(x)) is an expression in the variable (x) that takes the form: [ p(x) = a_nx^n + a_{n-
1}x^{n-1} + \ldots + a_1x + a_0 ]
Characteristics:
The sign of each coefficient and exponent is stored within the coefficient and the exponent itself.
Additional terms with equal exponents are possible.
Storage allocation for each term in the polynomial must be done in ascending or descending order of
their exponents
1. Representation:
o Polynomials can be represented using different approaches:
Arrays: In this method, we store the coefficients of the polynomial terms in an array. Each array
element contains both the coefficient and the exponent.
Linked Lists: Using linked lists, each polynomial term corresponds to a node in the list, making
polynomial manipulation efficiency.
Given two polynomials represented by two arrays, write a function that adds given two polynomials.
Example:
Input: A[] = {5, 0, 10, 6}
B[] = {1, 2, 4}
Output: sum[] = {6, 2, 14, 6}
#include <iostream>
sum[i] = A[i];
sum[i] += B[i];
return sum;
}
if (i != 0)
if (i != n - 1)
int main()
// 6x^3
// 4x^2
int B[] = { 1, 2, 4 };
printPoly(A, m);
printPoly(B, n);
printPoly(sum, size);
return 0;
Output:
First polynomial is
5 + 0x^1 + 10x^2 + 6x^3
Second polynomial is
1 + 2x^1 + 4x^2
Sum polynomial is
6 + 2x^1 + 14x^2 + 6x^3
#include <iostream>
// Node class
class Node {
public:
Node* next;
// Constructor of Node
this->power = power;
this->next = NULL;
};
return;
c out << " " << head1->co eff + head2->co eff << "x^"
c out << " " << head1->co eff << "x^" << head1->power
<< " ";
else {
c out << " " << head2->co eff << "x^" << head2->power
head = head->next;
head->next = new_node;
{
cout << "Linked List" << endl;
head = head->next;
// Main function
int main()
insert(head, 4, 1);
insert(head2, 4, 1);
printList(head);
printList(head2);
cout << endl << "Addition:" << endl;
addPolynomials(head, head2);
return 0;
Output
Linked List
5x^2 4x^1
Linked List
6x^2 4x^1
Addition:
11x^2 8x^1
Approach
This implementation takes two arguments p1 and p2, which are lists representing the
coefficients of two polynomials. The function returns a new list representing the sum of the
two input polynomials.
The function first checks the lengths of the two input lists and pads the shorter list with zeros
so that both lists have the same length. We then use the zip function to create pairs of
corresponding coefficients from the two input lists, and the sum function to add the pairs
together. The resulting sum is appended to a new list, which is returned at the end.
#include <iostream>
#include <vector>
p1.resize(len2, 0);
} else {
p2.resize(len1, 0);
std::vector<int> result(len1);
return result;
int main() {
std::vector<int> p1 = {2, 0, 4, 6, 8};
return 0;
Output
[2, 0, 5, 8, 8]
Example:
Input:
1st number = 5x 2 + 4x1 + 2x0
2nd number = -5x 1 - 5x0
Output:
5x2-1x1-3x0
Input:
1st number = 5x 3 + 4x2 + 2x0
2nd number = 5x^1 - 5x^0
Output:
5x3 + 4x2 + 5x1 - 3x0
#include <bits/stdc++.h>
struct Node {
Node* next;
};
newnode->coeff = coeff;
newnode->power = power;
newnode->next = NULL;
// If linked list is empty
if (start == NULL)
return newnode;
ptr = ptr->next;
ptr->next = newnode;
return start;
ptr = ptr->next;
}
ptr1 = start;
ptr2 = ptr1;
if (ptr1->power == ptr2->next->power) {
// Add their coefficients and put it in 1st element
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete (dup);
else
ptr2 = ptr2->next;
ptr1 = ptr1->next;
Node* poly3)
ptr1 = poly1;
ptr2 = poly2;
ptr2 = ptr2->next;
}
ptr2 = poly2;
ptr1 = ptr1->next;
Remove Duplicates(poly3);
return poly3;
// Driver Code
int main()
print List(poly1);
print List(poly2);
print List(poly3);
return 0;
Output
Examples:
Input:
1st Number = 5x^2 * y^1 + 4x^1 * y^2 + 3x^1 * y^1 + 2x^1
2nd Number = 3x^1 * y^2 + 4x^1
Output:
5x^2 * y^1 + 7x^1 * y^2 + 3x^1 * y^1 + 6x^1
Input:
1st Number = 3x^3 * y^2 + 2x^2 + 5x^1 * y^1 + 9y^1 + 2
2nd Number = 4x^3 * y^3 + 2x^3 * y^2 + 1y^2 + 3
Output:
4x^3 * y^3 + 5x^3 * y^2 + 2x^2 + 5x^1 * y^1 + 1y^2 + 9y^1 + 5
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coef;
int exp;
};
temp->coef = coef;
temp->exp = exp;
temp->next = NULL;
if (*poly == NULL) {
*poly = temp;
return;
}
Node* current = *poly;
current = current->next;
current->next = temp;
if (poly == NULL) {
printf("0\n");
return;
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
printf("\n");
if (poly1->exp == poly2->exp) {
poly1 = poly1->next;
poly2 = poly2->next;
poly1 = poly1->next;
} else {
poly2 = poly2->next;
}
while (poly1 != NULL) {
poly1 = poly1->next;
poly2 = poly2->next;
return result;
int main() {
insert(&poly1, 5, 4);
insert(&poly1, 3, 2);
insert(&poly1, 1, 0);
insert(&poly2, 4, 4);
insert(&poly2, 2, 2);
insert(&poly2, 1, 1);
print(poly1);
print(poly2);
printf("Result: ");
print(result);
return 0;
0utput
// Structure of a Node
struct Node {
int data;
};
struct Queue {
struct Node *front, *rear;
};
temp->data = value;
if (q->front == NULL)
q->front = temp;
else
q->rear->link = temp;
q->rear = temp;
q->rear->link = q->front;
int de
del Queue(Queue* q)
if (q->front == NULL) {
c out << "Queue is empty";
return INT_MIN;
if (q->front == q->rear) {
value = q->front->data;
free(q->front);
q->front = NULL;
q->rear = NULL;
value = temp->data;
q->front = q->front->link;
q->rear->link = q->front;
free(temp);
return value;
}
temp = temp->link;
int main()
Display Queue(q);
C out << end l << "Deleted value = " << del Queue(q);
c out << end l << "Deleted value = " << del Queue(q);
Display Queue(q);
display Queue(q);
return 0;
Output