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

1gc22cs154 Mini Project

The document discusses polynomials and various ways they can be represented and manipulated using data structures. Polynomials are algebraic expressions with non-negative integer powers of variables. They can be represented using arrays, linked lists, or other approaches. Methods are presented for adding and multiplying polynomials represented as linked lists by traversing the lists and combining like terms.

Uploaded by

varshaputtu1402
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)
31 views

1gc22cs154 Mini Project

The document discusses polynomials and various ways they can be represented and manipulated using data structures. Polynomials are algebraic expressions with non-negative integer powers of variables. They can be represented using arrays, linked lists, or other approaches. Methods are presented for adding and multiplying polynomials represented as linked lists by traversing the lists and combining like terms.

Uploaded by

varshaputtu1402
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/ 36

POLYNOMIALS IN DATA STRUCTURES



Polynomials are algebraic expressions in which the power of variables is non-negative


integers. They are used in various fields of mathematics, astronomy, economics, etc. There
are various examples of the polynomials such as 2x + 3, x2 + 4x + 5, etc.
Polynomial is defined as the mathematical expression that are made up of variables,
constant in which the powers of variables are non negative integers. In mathematics a
Polynomials is defined.

P(x) = anxn + an-1xn-1 +an-2xn-2 + . . . + a1x + a0


 x is Variable of the Polynomial
 xn, xn-1, xn-2, . . . , x are various exponents of variable
 an, an-1, an-2, . . . , a1 are Coefficeints of respective exponents of variable
 a0 is Constant Term

Polynomial Examples

Various examples of the polynomial equations are,


 3x3 + 5x2 – 4x,
 x2 + 2x,
 5x + y
 √2(x) + y, etc.

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 ]

 The non-negative integer (n) is called the degree of the polynomial.


 Each term in the polynomial consists of two parts: the coefficient and the exponents.
 For example, in the polynomial (10x^2 + 26x), the coefficients are 10 and 26, and the exponents are 2
and 1.

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.

Program to add two polynomials




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}

The first input array represents "5 + 0x^1 + 10x^2 + 6x^3"


The second array represents "1 + 2x^1 + 4x^2"
And Output is "6 + 2x^1 + 14x^2 + 6x^3"
Addition is simpler than multiplication of polynomials. We initialize the result as one of the
two polynomials, then we traverse the other polynomial and add all terms to the result.

// Simple C++ program to add two polynomials

#include <iostream>

using namespace std;


// A utility function to return maximum of two integers

int max(int m, int n) { return (m > n) ? m : n; }

// A[] represents coefficients of first polynomial

// B[] represents coefficients of second polynomial

// m and n are sizes of A[] and B[] respectively

int* add(int A[], int B[], int m, int n)

int size = max(m, n);

int* sum = new int[size];

// Initialize the product polynomial

for (int i = 0; i < m; i++)

sum[i] = A[i];

// Take every term of first polynomial

for (int i = 0; i < n; i++)

sum[i] += B[i];

return sum;
}

// A utility function to print a polynomial

void printPoly(int poly[], int n)

for (int i = 0; i < n; i++) {

cout << poly[i];

if (i != 0)

cout << "x^" << i;

if (i != n - 1)

cout << " + ";

// Driver program to test above functions

int main()

// The following array represents polynomial 5 + 10x^2 +

// 6x^3

int A[] = { 5, 0, 10, 6 };


// The following array represents polynomial 1 + 2x +

// 4x^2

int B[] = { 1, 2, 4 };

int m = sizeof(A) / sizeof(A[0]);

int n = sizeof(B) / sizeof(B[0]);

cout << "First polynomial is \n";

printPoly(A, m);

cout << "\nSecond polynomial is \n";

printPoly(B, n);

int* sum = add(A, B, m, n);

int size = max(m, n);

cout << "\nsum polynomial is \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

// Program to add two polynomials represented

// in linked list using recursion

#include <iostream>

using namespace std;

// Node class

class Node {

public:

int co eff, power;

Node* next;

// Constructor of Node

Node(int co eff, int power)

this->co eff = co eff;

this->power = power;
this->next = NULL;

};

// Function to add polynomials

void add Polynomials(Node* head1, Node* head2)

// Checking if our list is empty

if (head1 == NULL && head2 == NULL)

return;

// List contains elements

else if (head1->power == head2->power) {

c out << " " << head1->co eff + head2->co eff << "x^"

<< head1->power << " ";

Add Polynomials(head1->next, head2->next);

else if (head1->power > head2->power) {

c out << " " << head1->co eff << "x^" << head1->power
<< " ";

Add Polynomials(head1->next, head2);

else {

c out << " " << head2->co eff << "x^" << head2->power

<< " ";

Add Polynomials(head1, head2->next);

void insert(Node* head, int co eff, int power)

Node* new _node = new Node(co eff, power);

while (head->next != NULL) {

head = head->next;

head->next = new_node;

void printList(Node* head)

{
cout << "Linked List" << endl;

while (head != NULL) {

cout << " " << head->coeff << "x"

<< "^" << head->power;

head = head->next;

// Main function

int main()

Node* head = new Node(5, 2);

insert(head, 4, 1);

Node* head2 = new Node(6, 2);

insert(head2, 4, 1);

printList(head);

cout << endl;

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

Implementation of a function that adds two polynomials represented as lists:

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>

std::vector<int> add_polynomials(std::vector<int> p1, std::vector<int> p2) {

int len1 = p1.size();

int len2 = p2.size();

if (len1 < len2) {

p1.resize(len2, 0);

} else {

p2.resize(len1, 0);

std::vector<int> result(len1);

for (int i = 0; i < len1; i++) {

result[i] = p1[i] + p2[i];

return result;

int main() {
std::vector<int> p1 = {2, 0, 4, 6, 8};

std::vector<int> p2 = {0, 0, 1, 2};

std::vector<int> result = add_polynomials(p1, p2);

for (int i = 0; i < result.size(); i++) {

std::cout << result[i] << " ";

return 0;

Output

[2, 0, 5, 8, 8]

Adding two polynomials using Linked List


Given two polynomial numbers represented by a linked list. Write a function that add these
list

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

Multiplication of two polynomials using Linked list


Given two polynomials in the form of linked list. The task is to find the multiplication of both
polynomials.
Examples:

Input: Poly1: 3x^2 + 5x^1 + 6, Poly2: 6x^1 + 8


Output: 18x^3 + 54x^2 + 76x^1 + 48
On multiplying each element of 1st polynomial with
elements of 2nd polynomial, we get
18x^3 + 24x^2 + 30x^2 + 40x^1 + 36x^1 + 48
On adding values with same power of x,
18x^3 + 54x^2 + 76x^1 + 48

Input: Poly1: 3x^3 + 6x^1 - 9, Poly2: 9x^3 - 8x^2 + 7x^1 + 2


Output: 27x^6 - 24x^5 + 75x^4 - 123x^3 + 114x^2 - 51x^1 - 18
// C++ implementation of the above approach

#include <bits/stdc++.h>

using namespace std;

// Node structure containing powerer

// and coefficient of variable

struct Node {

int coeff, power;

Node* next;

};

// Function add a new node at the end of list

Node* addnode(Node* start, int coeff, int power)

// Create a new node

Node* newnode = new Node;

newnode->coeff = coeff;

newnode->power = power;

newnode->next = NULL;
// If linked list is empty

if (start == NULL)

return newnode;

// If linked list has nodes

Node* ptr = start;

while (ptr->next != NULL)

ptr = ptr->next;

ptr->next = newnode;

return start;

// Function To Display The Linked list

void printList(struct Node* ptr)

while (ptr->next != NULL) {

cout << ptr->coeff << "x^" << ptr->power ;

if( ptr->next!=NULL && ptr->next->coeff >=0)

c out << "+";

ptr = ptr->next;
}

C out << ptr ->co eff << "\n";

// Function to add coefficients of

// two elements having same power

void remove Duplicates(Node* start)

Node *ptr1, *ptr2, *dup;

ptr1 = start;

/* Pick elements one by one */

while (ptr1 != NULL && ptr1->next != NULL) {

ptr2 = ptr1;

// Compare the picked element

// with rest of the elements

while (ptr2->next != NULL) {

// If power of two elements are same

if (ptr1->power == ptr2->next->power) {
// Add their coefficients and put it in 1st element

ptr1->co eff = ptr1->co eff + ptr2->next->co eff;

dup = ptr2->next;

ptr2->next = ptr2->next->next;

// remove the 2nd element

delete (dup);

else

ptr2 = ptr2->next;

ptr1 = ptr1->next;

// Function two Multiply two polynomial Numbers

Node* multiply(Node* poly1, Node* poly2,

Node* poly3)

// Create two pointer and store the

// address of 1st and 2nd polynomials


Node *ptr1, *ptr2;

ptr1 = poly1;

ptr2 = poly2;

while (ptr1 != NULL) {

while (ptr2 != NULL) {

int co eff, power;

// Multiply the coefficient of both

// polynomials and store it in co eff

Co eff = ptr1->co eff * ptr2->co eff;

// Add the power of both polynomials

// and store it in power

power = ptr1->power + ptr2->power;

// Invoke add node function to create

// a new node by passing three parameters

poly3 = add node(poly3, co eff, power);

// move the pointer of 2nd polynomial

// two get its next term

ptr2 = ptr2->next;
}

// Move the 2nd pointer to the

// starting point of 2nd polynomial

ptr2 = poly2;

// move the pointer of 1st polynomial

ptr1 = ptr1->next;

// this function will be invoke to add

// the coefficient of the elements

// having same power from the resultant linked list

Remove Duplicates(poly3);

return poly3;

// Driver Code

int main()

Node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;


// Creation of 1st Polynomial: 3x^2 + 5x^1 + 6

poly1 = add node(poly1, 3, 3);

poly1 = add node(poly1, 6, 1);

poly1 = add node(poly1, -9, 0);

// Creation of 2nd polynomial: 6x^1 + 8

poly2 = add node(poly2, 9, 3);

poly2 = add node(poly2, -8, 2);

poly2 = add node(poly2, 7, 1);

poly2 = add node(poly2, 2, 0);

// Displaying 1st polynomial

C out << "1st Polynomial:- ";

print List(poly1);

// Displaying 2nd polynomial

C out << "2nd Polynomial:- ";

print List(poly2);

// calling multiply function

poly3 = multiply(poly1, poly2, poly3);


// Displaying Resultant Polynomial

C out << "Resultant Polynomial:- ";

print List(poly3);

return 0;

Output

1st Polynomial:- 3x^3+6x^1-9


2nd Polynomial:- 9x^3-8x^2+7x^1+2
Resultant Polynomial:- 27x^6-24x^5+75x^4-123x^3+114x^2-51x^1-18

Adding two polynomials using Circular Linked List


Given two polynomial numbers represented by a circular linked list, the task is to add these
two polynomials by adding the coefficients of the powers of the same variable.
Note: In given polynomials, the term containing the higher power of x will come first.

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;

struct Node* next;

};

typedef struct Node Node;

void insert(Node** poly, int coef, int exp) {

Node* temp = (Node*) malloc(sizeof(Node));

temp->coef = coef;

temp->exp = exp;

temp->next = NULL;

if (*poly == NULL) {

*poly = temp;

return;

}
Node* current = *poly;

while (current->next != NULL) {

current = current->next;

current->next = temp;

void print(Node* poly) {

if (poly == NULL) {

printf("0\n");

return;

Node* current = poly;

while (current != NULL) {

printf("%dx^%d", current->coef, current->exp);

if (current->next != NULL) {

printf(" + ");

}
current = current->next;

printf("\n");

Node* add(Node* poly1, Node* poly2) {

Node* result = NULL;

while (poly1 != NULL && poly2 != NULL) {

if (poly1->exp == poly2->exp) {

insert(&result, poly1->coef + poly2->coef, poly1->exp);

poly1 = poly1->next;

poly2 = poly2->next;

} else if (poly1->exp > poly2->exp) {

insert(&result, poly1->coef, poly1->exp);

poly1 = poly1->next;

} else {

insert(&result, poly2->coef, poly2->exp);

poly2 = poly2->next;

}
while (poly1 != NULL) {

insert(&result, poly1->coef, poly1->exp);

poly1 = poly1->next;

while (poly2 != NULL) {

insert(&result, poly2->coef, poly2->exp);

poly2 = poly2->next;

return result;

int main() {

Node* poly1 = NULL;

insert(&poly1, 5, 4);

insert(&poly1, 3, 2);

insert(&poly1, 1, 0);

Node* poly2 = NULL;

insert(&poly2, 4, 4);
insert(&poly2, 2, 2);

insert(&poly2, 1, 1);

printf("First polynomial: ");

print(poly1);

printf("Second polynomial: ");

print(poly2);

Node* result = add(poly1, poly2);

printf("Result: ");

print(result);

return 0;

0utput

First polynomial: 5x^4 + 3x^2 + 1x^0

Second polynomial: 4x^4 + 2x^2 + 1x^1

Result: 9x^4 + 5x^2 + 1x^1 + 1x^0


Circular Linked List Implementation of Circular Queue
Prerequisite – Circular Singly Linked List We have discussed basics and how to implement
circular queue using array in set 1. Circular Queue (Introduction and Array
Implementation) In this post another method of circular queue implementation is discussed,
using Circular Singly Linked List.

// C++ program for insertion and

// deletion in Circular Queue

#include <bits/std c++.h>

using namespace std;

// Structure of a Node

struct Node {

int data;

struct Node* link;

};

struct Queue {
struct Node *front, *rear;

};

// Function to create Circular queue

void end Queue(Queue* q, int value)

struct Node* temp = new Node;

temp->data = value;

if (q->front == NULL)

q->front = temp;

else

q->rear->link = temp;

q->rear = temp;

q->rear->link = q->front;

// Function to delete element from Circular Queue

int de

del Queue(Queue* q)

if (q->front == NULL) {
c out << "Queue is empty";

return INT_MIN;

// If this is the last node to be deleted

int value; // Value to be dequeued

if (q->front == q->rear) {

value = q->front->data;

free(q->front);

q->front = NULL;

q->rear = NULL;

else // There are more than one nodes

struct Node* temp = q->front;

value = temp->data;

q->front = q->front->link;

q->rear->link = q->front;

free(temp);

return value;
}

// Function displaying the elements of Circular Queue

void display Queue(struct Queue* q)

struct Node* temp = q->front;

c out << end l << "Elements in Circular Queue are: ";

while (temp->link != q->front) {

c out << temp->data << " ";

temp = temp->link;

C out << temp->data;

/* Driver of the program */

int main()

// Create a queue and initialize front and rear

Queue* q = new Queue;

q->front = q->rear = NULL;

// Inserting elements in Circular Queue


End Queue(q, 14);

end Queue(q, 22);

end Queue(q, 6);

// Display elements present in Circular Queue

Display Queue(q);

// Deleting elements from Circular Queue

C out << end l << "Deleted value = " << del Queue(q);

c out << end l << "Deleted value = " << del Queue(q);

// Remaining elements in Circular Queue

Display Queue(q);

end Queue(q, 9);

end Queue(q, 20);

display Queue(q);

return 0;

Output

Elements in Circular Queue are: 14 22 6


Deleted value = 14
Deleted value = 22
Elements in Circular Queue are: 6
Elements in Circular Queue are: 6 9 20
CONCLUSION:
In conclusion, polynomial algorithms are an essential part of data structures
and computer science. They enable efficient problem-solving by providing
algorithms that can handle larger inputs without exponential increases in
execution time.
INFORMATION GATHERED FROM:
Google , chrome ,DSA text book etc.

You might also like