You are on page 1of 26

11)Write a C program to create a binary search tree and for implementing the in order,

Pre order, post order traversal using recursion


#include <stdlib.h>
typedef struct tnode {
int data;
struct tnode *right, *left;
} TNODE;

TNODE *CreateBST(TNODE *, int);


void Inorder(TNODE *);
void Preorder(TNODE *);
void Postorder(TNODE *);

main() {
TNODE *root = NULL; /* Main Program */
int opn, elem, n, i;
do {
clrscr();
printf("n ### Binary Search Tree Operations ### nn");
printf("n Press 1-Creation of BST");
printf("n 2-Traverse in Inorder");
printf("n 3-Traverse in Preorder");
printf("n 4-Traverse in Postorder");
printf("n 5-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
root = NULL;
printf("nnBST for How Many Nodes ?");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("nRead the Data for Node %d ?", i);
scanf("%d", &elem);
root = CreateBST(root, elem);
}
printf("nBST with %d nodes is ready to Use!!n", n);
break;
case 2:
printf("n BST Traversal in INORDER n");
Inorder(root);
break;
case 3:
printf("n BST Traversal in PREORDER n");
Preorder(root);
break;
case 4:
printf("n BST Traversal in POSTORDER n");
Postorder(root);
break;
case 5:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 5);
}
TNODE *CreateBST(TNODE *root, int elem) {
if (root == NULL) {
root = (TNODE *) malloc(sizeof(TNODE));
root->left = root->right = NULL;
root->data = elem;
return root;
} else {
if (elem < root->data)
root->left = CreateBST(root->left, elem);
else if (elem > root->data)
root->right = CreateBST(root->right, elem);
else
printf(" Duplicate Element !! Not Allowed !!!");

return (root);
}
}
void Inorder(TNODE *root) {
if (root != NULL) {
Inorder(root->left);
printf(" %d ", root->data);
Inorder(root->right);
}
}

void Preorder(TNODE *root) {


if (root != NULL) {
printf(" %d ", root->data);
Preorder(root->left);
Preorder(root->right);
}
}

void Postorder(TNODE *root) {


if (root != NULL) {
Postorder(root->left);
Postorder(root->right);
printf(" %d ", root->data);
}
}
Output:
### Binary Search Tree Operations ###

Press 1-Creation of BST


2-Traverse in Inorder
3-Traverse in Preorder
4-Traverse in Postorder
5-Exitn
Your option ? 1
nnBST for How Many Nodes ?5
nRead the Data for Node 1 ?9
nRead the Data for Node 2 ?8
nRead the Data for Node 3 ?36
nRead the Data for Node 4 ?85
nRead the Data for Node 5 ?1
nBST with 5 nodes is ready to Use!!nnnnn Press a Key to Continue . . .
### Binary Search Tree Operations ###

Press 1-Creation of BST


2-Traverse in Inorder
3-Traverse in Preorder
4-Traverse in Postorder
5-Exitn
Your option ? 2
n BST Traversal in INORDER n 1 8 9 36 85 nnnn Press a Key to Continue . . .
### Binary Search Tree Operations ###

Press 1-Creation of BST


2-Traverse in Inorder
3-Traverse in Preorder
4-Traverse in Postorder
5-Exitn
Your option ?
3
n BST Traversal in PREORDER n 9 8 1 36 85 nnnn Press a Key to Continue . . .
### Binary Search Tree Operations ###

Press 1-Creation of BST


2-Traverse in Inorder
3-Traverse in Preorder
4-Traverse in Postorder
5-Exitn
Your option ?
4
n BST Traversal in POSTORDER n 1 8 85 36 9 nnnn Press a Key to Continue . . .
### Binary Search Tree Operations ###

Press 1-Creation of BST


2-Traverse in Inorder
3-Traverse in Preorder
4-Traverse in Postorder
5-Exitn
Your option ?5
13 a)Write a C program for finding the Depth First Search of a graph.

#include <stdio.h>
#include <stdlib.h>
struct node {
int vertex;
struct node* next;
};
struct node* createNode(int v);
struct Graph {
int numVertices;
int* visited;
// We need int** to store a two dimensional array.
// Similary, we need struct node** to store an array of Linked lists
struct node** adjLists;
};
void DFS(struct Graph* graph, int vertex) {
struct node* adjList = graph->adjLists[vertex];
struct node* temp = adjList;

graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);

while (temp != NULL) {


int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
// Create a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Create graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists = malloc(vertices * sizeof(struct node*));
graph->visited = malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Add edge from dest to src
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// Print the graph
void printGraph(struct Graph* graph) {
int v;
for (v = 0; v < graph->numVertices; v++) {
struct node* temp = graph->adjLists[v];
printf("\n Adjacency list of vertex %d\n ", v);
while (temp) {
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}
int main() {
struct Graph* graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
printGraph(graph);
DFS(graph, 2);
return 0;
}
Output:
Adjacency list of vertex 0
2 -> 1 ->
Adjacency list of vertex 1
2 -> 0 ->
Adjacency list of vertex 2
3 -> 1 -> 0 ->
Adjacency list of vertex 3
2 ->
Visited 2
Visited 3
Visited 1
Visited 0
13) b)Write a C program for finding the Breadth First Search of a graph
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct queue* createQueue();
void enqueue(struct queue* q, int);
int dequeue(struct queue* q);
void display(struct queue* q);
int isEmpty(struct queue* q);
void printQueue(struct queue* q);

struct node {
int vertex;
struct node* next;
};
struct node* createNode(int);
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);

struct node* temp = graph->adjLists[currentVertex];

while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}

// Creating a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Creating a graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists = malloc(vertices * sizeof(struct node*));
graph->visited = malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;

// Add edge from dest to src


newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}

// Create a queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
// Check if the queue is empty
int isEmpty(struct queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}
// Adding elements into queue
void enqueue(struct queue* q, int value) {
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
// Removing elements from queue
int dequeue(struct queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
printf("Resetting queue ");
q->front = q->rear = -1;
}
}
return item;
}
// Print the queue
void printQueue(struct queue* q) {
int i = q->front;
if (isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);

bfs(graph, 0);

return 0;
}
Output:
Queue contains
0 Resetting queue Visited 0

Queue contains
2 1 Visited 2

Queue contains
1 4 Visited 1

Queue contains
4 3 Visited 4

Queue contains
3 Resetting queue Visited 3

12 a)Write a C program for finding the transitive closure of a digraph Using warshall’s
Algorithm

#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n) {
int i,j,k;
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b) {
;
if(a>b)
return(a); else
return(b);
}
void main() {
int p[10][10]= {
0
}
,n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for (i=1;i<=e;i++) {
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}
Output:

12 B)Write a C program for finding the shortest path from a given source to any vertex
in a digraph using Dijkstra’s algorithm.
We are given a graph with a source vertex in the graph. And we have to find the shortest path
from the source vertex to all other vertices of the graph.
The Dijikstra’s algorithm is a greedy algorithm to find the shortest path from the source
vertex of the graph to the root node of the graph.

Algorithm
Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree.
Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source vertex
so that it is picked first.
Step 3 : Loop until all vertices of the graph are in the shortPath.
Step 3.1 : Take a new vertex that is not visited and is nearest.
Step 3.2 : Add this vertex to shortPath.
Step 3.3 : For all adjacent vertices of this vertex update distances. Now check every adjacent
vertex of V, if sum of distance of u and weight of edge is else the update it.

#include <limits.h>
#include <stdio.h>
#define V 9
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n) {
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist, V);
}
int main() {
int graph[V][V] = { { 0, 6, 0, 0, 0, 0, 0, 8, 0 },
{ 6, 0, 8, 0, 0, 0, 0, 13, 0 },
{ 0, 8, 0, 7, 0, 6, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 6, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 13, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 }
};
dijkstra(graph, 0);
return 0;
}

Output
Vertex Distance from Source
00
16
2 14
3 21
4 21
5 11
69
78
8 15

10)Write a C program for the representation of polynomials using circular linked list
and for the addition of two such polynomials

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

// Structure of a node
// in a circular linked list
struct Node {
// Stores coefficient
// of a node
int coeff;

// Stores power of'


// variable x of a node
int powx;

// Stores power of
// variable y of a node
int powy;

// Stores pointer
// to next node
struct Node* next;
};

// Function to dynamically create a node


void create_node(int c, int p1, int p2, struct Node** temp) {
// Stores new node
struct Node *r;
// Stores temp node
struct Node *z = *temp;

// Dynamically create a new node


r = (struct Node*)malloc(sizeof(struct Node));
// Update coefficient of r
r->coeff = c;
// Update power of variable x in r
r->powx = p1;
// Update power of variable y in r
r->powy = p2;

// If z is null
if (z == NULL) {
// Update temp node
(*temp) = r;
// Update next pointer of temp node
(*temp)->next = (*temp);
}
else {
// Update next pointer of z
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp Node
(*temp) = r;
}
}

// Function to add polynomial of two list


void add_poly(struct Node* poly1, struct Node* poly2, struct Node** temp) {
// Stores head node of polynomial1
struct Node *start1 = poly1;
// Stores head node of polynomial1
struct Node *start2 = poly2;
// Update poly1
poly1 = poly1->next;
// Update poly2
poly2 = poly2->next;

// Traverse both circular linked list


while ((poly1 != start1 && poly2 != start2)) {
// Stores new node
struct Node* r;
// Stores temp node
struct Node* z = *temp;
// Dynamically create a new node
r = (struct Node*)malloc(sizeof(struct Node));
// Update coefficient of r
r->coeff = 0;

// If power of x of poly1 is greater than power of x of poly2


if (poly1->powx > poly2->powx) {
// Update coefficient of r
r->coeff = poly1->coeff;
// Update of power of x in r
r->powx = poly1->powx;
// Update of power of y in r
r->powy = poly1->powy;
// Update poly1
poly1 = poly1->next;
}
// If power of x of 1st polynomial is less than power of x of 2nd poly
else if (poly1->powx < poly2->powx) {
// Update coefficient OF r
r->coeff = poly2->coeff;
// Update power of x in r
r->powx = poly2->powx;
// Update power of y in r
r->powy = poly2->powy;
// Update ploy2
poly2 = poly2->next;
}
// If power of x of 1st polynomial is equal to power of x of 2nd poly
else {
// Power of y of 1st polynomial is greater than power of y of poly2
if (poly1->powy > poly2->powy) {
// Update coefficient of r
r->coeff = poly1->coeff;
// Update power of x in r
r->powx = poly1->powx;
// Update power of y in r
r->powy = poly1->powy;
// Update poly1
poly1 = poly1->next;
}
// If power of y of poly1 is less than power of y of ploy2
else if (poly1->powy < poly2->powy) {
// Update coefficient of r
r->coeff = poly2->coeff;
// Update power of x in r
r->powx = poly2->powx;
// Update power of y in r
r->powy = poly2->powy;
// Update poly2
poly2 = poly2->next;
}
// If power of y of 1st poly is equal to power of y of ploy2
else {
// Update coefficient of r
r->coeff = poly2->coeff + poly1->coeff;
// Update power of x in r
r->powx = poly1->powx;
// Update power of y in r
r->powy = poly1->powy;
// Update poly1
poly1 = poly1->next;
// Update poly2
poly2 = poly2->next;
}
}

// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update next pointer of temp
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}

// If there are nodes left to be traversed in poly1 or poly2 then


// append them in resultant polynomial .
while (poly1 != start1 || poly2 != start2) {
// If poly1 is not empty
if (poly1 != start1) {
// Stores new node
struct Node *r;
// Stores temp node
struct Node *z = *temp;
// Create new node
r = (struct Node*)malloc(sizeof(struct Node));
// Update coefficient or r
r->coeff = poly1->coeff;
// Update power of x in r
r->powx = poly1->powx;
// Update power of y in r
r->powy = poly1->powy;
// Update poly1
poly1 = poly1->next;

// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update pointer to next node
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}

// If poly2 is not empty


if (poly2 != start2) {
// Stores new node
struct Node *r;
// Stores temp node
struct Node *z = *temp;
// Create new node
r = (struct Node*)malloc(sizeof(struct Node));
// Update coefficient of z
z->coeff = poly2->coeff;
// Update power of x in z
z->powx = poly2->powx;
// Update power of y in z
z->powy = poly2->powy;
// Update poly2
poly2 = poly2->next;

// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update next pointer of temp
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}
}

// Stores new node


struct Node *r;
// Stores temp node
struct Node *z = *temp;
// Create new node
r = (struct Node*)malloc(sizeof(struct Node));
// Update coefficient of r
r->coeff = 0;

// If power of x of start1 greater than power of x of start2


if (start1->powx > start2->powx) {
// Update coefficient of r
r->coeff = start1->coeff;
// Update power of x in r
r->powx = start1->powx;
// Update power of y in r
r->powy = start1->powy;
}
// If power of x of start1 less than power of x of start2
else if (start1->powx < start2->powx) {
// Update coefficient of r
r->coeff = start2->coeff;
// Update power of x in r
r->powx = start2->powx;
// Update power of y in r
r->powy = start2->powy;
}
// If power of x of start1 equal to power of x of start2
else {
// If power of y of start1 greater than power of y of start2
if (start1->powy > start2->powy) {
// Update coefficient of r
r->coeff = start1->coeff;
// Update power of x in r
r->powx = start1->powx;
// Update power of y in r
r->powy = start1->powy;
}
// If power of y of start1 less than power of y of start2
else if (start1->powy < start2->powy) {
// Update coefficient of r
r->coeff = start2->coeff;
// Update power of x in r
r->powx = start2->powx;
// Update power of y in r
r->powy = start2->powy;
}
// If power of y of start1 equal to power of y of start2
else {
// Update coefficient of r
r->coeff = start2->coeff + start1->coeff;
// Update power of x in r
r->powx = start1->powx;
// Update power of y in r
r->powy = start1->powy;
}
}

// If z is null
if (z == NULL) {
// Update temp
(*temp) = r;
// Update next pointer of temp
(*temp)->next = (*temp);
}
else {
// Update next pointer of r
r->next = z->next;
// Update next pointer of z
z->next = r;
// Update temp
(*temp) = r;
}
}

// Display the circular linked list


void display(struct Node* node) {
// Stores head node of list
struct Node* start = node;
// Update node
node = node->next;

// Traverse the list


while (node != start && node->coeff != 0) {
// Print coefficient of current node
printf("%dx^%d", node->coeff, node->powx);
// If power of variable x is not zero
if (node->powx != 0)
printf(" * ");

// If power of variable y is not zero


if(node->powy != 0)
printf("y^%d", node->powy);

// Add next term of the polynomial


if (node != start && node->next->coeff != 0) {
printf(" + ");
}

// Update node
node = node->next;
}

// Print coefficient of current node


printf("%d", node->coeff);
// If power of variable x is not zero
if (node->powx != 0)
printf("x^%d", node->powx);

// If power of variable y is not zero


if (node->powy != 0)
printf("y^%d", node->powy);

printf("\n\n");
}

// Driver Code
int main() {
// Stores node of first polynomial
struct Node *poly1 = NULL;
// Stores node of second polynomial
struct Node *poly2 = NULL;
// Stores node of resultant polynomial
struct Node *store = NULL;

// Create first polynomial


create_node(5, 2, 1, &poly1);
create_node(4, 1, 2, &poly1);
create_node(3, 1, 1, &poly1);
create_node(2, 1, 0, &poly1);
create_node(3, 0, 1, &poly1);
create_node(2, 0, 0, &poly1);

// Create second polynomial


create_node(3, 1, 2, &poly2);
create_node(4, 1, 0, &poly2);
create_node(2, 0, 1, &poly2);
create_node(6, 0, 0, &poly2);

// Function call to add two polynomial


add_poly(poly1, poly2, &store);

// Display polynomial 1
printf("Polynomial 1\n");
display(poly1);

// Display polynomial 2
printf("Polynomial 2\n");
display(poly2);

// Display final addition of 2-variable polynomial


printf("Polynomial after addition\n");
display(store);

return 0;
}

Explanation :
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
Explanation:
The coefficient of x^2 * y^1 in 1st numbers is 5 and 0 in the 2nd number. Therefore,
sum of the coefficient of x^2 * Y^1 is 5.
The coefficient of x^1 * y^2 in 1st numbers is 4 and 3 in the 2nd number. Therefore,
sum of the coefficient of x^1 * Y^2 is 7.
The coefficient of x^1 * y^1 in 1st numbers is 3 and 0 in the 2nd number. Therefore,
sum of the coefficient of x^1 * Y^1 is 2.
The coefficient of x^1 * Y^0 in 1st numbers is 2 and 4 in the 2nd number. Therefore,
sum of the coefficient of x^1 * Y^0 is 6.
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
Approach: Follow the below steps to solve the problem:
1. Create two circular linked lists , where each node will consist of the
coefficient, power of x, power of y and pointer to the next node.
2. Traverse both the polynomials and check the following conditions:
 If power of x of 1st polynomial is greater than power of x of
second polynomial then store node of first polynomial in resultant
polynomial and increase counter of polynomial 1.
 If power of x of 1st polynomial is less than power of x of second
polynomial then store the node of second polynomial in resultant
polynomial and increase counter of polynomial 2.
 If power of x of 1st polynomial is equal to power of x of second
polynomial and power of y of 1st polynomial is greater than
power of y of 2nd polynomial then store the node of first
polynomial in resultant polynomial and increase counter of
polynomial 1.
 If power of x of 1st polynomial is equal to power of x of second
polynomial and power of y of 1st polynomial is equal to power
of y of 2nd polynomial then store the sum of coefficient of both
polynomial in resultant polynomial and increase counter of both
polynomial 1 and polynomial 2.
3. If there are nodes left to be traversed in 1st polynomial or in 2nd polynomial
then append them in resultant polynomial.
4. Finally, print the resultant polynomial.

You might also like