Polynomial Addition
import java.io.*;
class node
{
int coe,exp;
public node next;
public node(int id,int dd)
{
coe=id;
exp=dd;
}
public void displaynode()
{
System.out.print(coe +"x^" +exp );
}
}
class nodeList
{
public node first;
public node last;
public void nodeLIst()
{
first=null;
last=null;
}
public boolean isEmpty()
{
return(first==null);
}
public void insert(int id,int dd)
{
if(isEmpty())
{
first=new node(id,dd);
last=first;
}
else
{
last.next=new node(id,dd);
last=last.next;
}
}
public void displayList()
{
node current=first;
while(current!=null)
{
current.displaynode();
if(current.next!=null)
System.out.print(" + ");
current=current.next;
}
}
public void read() throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int c,e,h=1;
while(h==1)
{
System.out.println("Enter coefficient\n");
c=Integer.parseInt(br.readLine());
System.out.println("Enter exponent\n");
e=Integer.parseInt(br.readLine());
insert(c,e);
System.out.println("1.Enter add more terms\n 0:Exit");
h=Integer.parseInt(br.readLine());
}
}
public void add(nodeList x,nodeList y,nodeList z) throws Exception
{
node p=x.first;
node q=y.first;
while(p!=null&&q!=null)
{
if(p.exp==q.exp)
{
z.insert(p.coe+q.coe,p.exp);
p=p.next;
q=q.next;
}
else
if(p.exp>q.exp)
{
z.insert(p.coe,p.exp);
p=p.next;
}
else
{
z.insert(q.coe,q.exp);
q=q.next;
}
}
while(p!=null)
{
z.insert(p.coe,p.exp);
p=p.next;
}
while(q!=null)
{
z.insert(q.coe,q.exp);
q=q.next;
}
}
}
class polynomial2
{
public static void main(String arg[])throws Exception
{
int id;
int dd;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
nodeList obj1=new nodeList();
System.out.println("\nEnter the first polynomial");
obj1.read();
nodeList obj2=new nodeList();
System.out.println("\nEnter the second polynomial");
obj2.read();
System.out.print("\nThe polynomial are\np(x) =");
obj1.displayList();
System.out.print("\nq(x) =");
obj2.displayList();
System.out.print("\np(x)+q(x) =");
nodeList obj3=new nodeList();
obj3.add(obj1,obj2,obj3);
obj3.displayList();
}
}
Java
import java.io.*;
class node
{
int coe,exp;
public node next;
public node(int id,int dd)
{
coe=id;
exp=dd;
}
public void displaynode()
{
System.out.print(coe +"x^" +exp );
}
}
class nodeList
{
public node first;
public node last;
public void nodeLIst()
{
first=null;
last=null;
}
public boolean isEmpty()
{
return(first==null);
}
public void insert(int id,int dd)
{
if(isEmpty())
{
first=new node(id,dd);
last=first;
}
else
{
last.next=new node(id,dd);
last=last.next;
}
}
public void displayList()
{
node current=first;
while(current!=null)
{
current.displaynode();
if(current.next!=null)
System.out.print(" + ");
current=current.next;
}
}
public void read() throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int c,e,h=1;
while(h==1)
{
System.out.println("Enter coefficient\n");
c=Integer.parseInt(br.readLine());
System.out.println("Enter exponent\n");
e=Integer.parseInt(br.readLine());
insert(c,e);
System.out.println("1.Enter add more terms\n 0:Exit");
h=Integer.parseInt(br.readLine());
}
}
public void add(nodeList x,nodeList y,nodeList z) throws Exception
{
node p=x.first;
node q=y.first;
while(p!=null&&q!=null)
{
if(p.exp==q.exp)
{
z.insert(p.coe+q.coe,p.exp);
p=p.next;
q=q.next;
}
else
if(p.exp>q.exp)
{
z.insert(p.coe,p.exp);
p=p.next;
}
else
{
z.insert(q.coe,q.exp);
q=q.next;
}
}
while(p!=null)
{
z.insert(p.coe,p.exp);
p=p.next;
}
while(q!=null)
{
z.insert(q.coe,q.exp);
q=q.next;
}
}
}
class polynomial2
{
public static void main(String arg[])throws Exception
{
int id;
int dd;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
nodeList obj1=new nodeList();
System.out.println("\nEnter the first polynomial");
obj1.read();
nodeList obj2=new nodeList();
System.out.println("\nEnter the second polynomial");
obj2.read();
System.out.print("\nThe polynomial are\np(x) =");
obj1.displayList();
System.out.print("\nq(x) =");
obj2.displayList();
System.out.print("\np(x)+q(x) =");
nodeList obj3=new nodeList();
obj3.add(obj1,obj2,obj3);
obj3.displayList();
}
}
Java