When we need to add two polynomials together, we can use a linked list to represent each polynomial. Each node in the linked list contains two pieces of information: the coefficient and the power of a term in the polynomial. Let's break down how this works:
Node Structure: We start by defining a node structure that holds the coefficient and power of a term. This structure is the building block of our linked list.
Insert Function: We create an "insert" function to add data to each node in the linked list. This function is used to build our polynomial representation. We keep adding new nodes to the linked list as we read the terms of the polynomial.
Display Function: To understand what's in our linked list, we have a "display" function. This function helps us visualize each term in the polynomial. It essentially prints out the coefficient and power of each term in the linked list.
Main Function (Addition): The core of the program is the "add" function. This function takes two linked lists, which represent two polynomials, as input. It then checks the exponents (powers) of the terms in these polynomials and follows these rules:
If the exponents of both nodes are equal, it adds the coefficients of both nodes together and moves the pointers of both linked lists to the next terms.
If the exponent of the first node is greater, it adds that term to the result and moves to the next term in the first linked list.
If the exponent of the second node is greater, it adds that term to the result and moves to the next term in the second linked list.
Loop Termination: The while loop continues as long as there are terms left in both linked lists. It stops when one of the linked lists reaches the end.
Handling Leftover Terms: After the main loop finishes, we check if there are any terms left in either linked list. If there are, we iterate through the remaining terms and add them to the result.
In simple terms, this program takes two polynomials, adds their terms together based on their exponents, and produces the sum as a linked list. It's a clever way to handle polynomial addition efficiently.
// polynomial addition using linkedlist
import java.util.*;
class pb123{
class Node{
int coef;
int exp;
Node next;
Node(int c,int e){
coef=c;
exp=e;
next=null;
}
}
Node head;
pb123(){
head=null;
}
void insert(int c,int e){
Node n=new Node(c,e);
if(head==null){
head=n;
}
else{
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=n;
}
}
void display(){
Node temp=head;
while(temp!=null){
System.out.print(temp.coef+"x^"+temp.exp+" ");
temp=temp.next;
}
System.out.println();
}
void add(pb123 p1,pb123 p2){
Node t1=p1.head;
Node t2=p2.head;
while(t1!=null && t2!=null){
if(t1.exp==t2.exp){
insert(t1.coef+t2.coef,t1.exp);
t1=t1.next;
t2=t2.next;
}
else if(t1.exp>t2.exp){
insert(t1.coef,t1.exp);
t1=t1.next;
}
else{
insert(t2.coef,t2.exp);
t2=t2.next;
}
}
while(t1!=null){
insert(t1.coef,t1.exp);
t1=t1.next;
}
while(t2!=null){
insert(t2.coef,t2.exp);
t2=t2.next;
}
}
public static void main(String[] args){
pb123 p1=new pb123();
pb123 p2=new pb123();
pb123 p3=new pb123();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of terms in the first polynomial");
int n=sc.nextInt();
for(int i=0;i<n;i++){
System.out.println("Enter the coefficient and exponent of the term");
int c=sc.nextInt();
int e=sc.nextInt();
p1.insert(c,e);
}
System.out.println("Enter the number of terms in the second polynomial");
n=sc.nextInt();
for(int i=0;i<n;i++){
System.out.println("Enter the coefficient and exponent of the term");
int c=sc.nextInt();
int e=sc.nextInt();
p2.insert(c,e);
}
System.out.println("The first polynomial is:");
p1.display();
System.out.println("The second polynomial is:");
p2.display();
System.out.println("The sum of the two polynomials is:");
p3.add(p1,p2);
p3.display();
}
}