Implementation of Linked List
Implementation of Linked List
Implementation of Linked List
*;
public class Project2
{
public static void main(String args[])
{
int[] c=new int[100];
polynomial1 o1=new polynomial1();
polynomial2 o2=new polynomial2();
node3 start3=null,pre3,ptr3;
Scanner sc=new Scanner(System.in);
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Derive");
System.out.println("5.Exit");
System.out.println("Enter your choice");
int ch=sc.nextInt();
if(ch==1)
{
System.out.println("Enter the number of
polynomilas");
int n1=sc.nextInt();
System.out.println("Polynomial 1 : ");
for(int i=0;i<n1;i++) {
System.out.println("Enter the exponent of
the polynomial ");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o1.poly1(ex, co);
}
System.out.println("Polynomial 2 : ");
for(int i=0;i<n1;i++) {
System.out.println("Enter the exponent of
the polynomial");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o2.poly1(ex, co);
}
node1 t1;
node2 t2;
t1=o1.start1;
while(t1!=null)
{
t2=o2.start2;
while(!(t1.co==t2.co))
{
t2=t2.next2;
}
node3 z=new node3();
z.ex=t1.ex+t2.ex;
z.co=t1.co;
if(start3==null)
{
start3=z;
z.next3=null;
}
else
{
ptr3=start3;
while(ptr3.next3!=null)
{
ptr3=ptr3.next3;
}
ptr3.next3=z;
z.next3=null;
}
t1=t1.next1;
}
ptr3=start3;
System.out.println("The final polynomial is\n");
while(ptr3!=null)
{
System.out.print(ptr3.ex+" x^"+ ptr3.co+" + ");
ptr3=ptr3.next3;
}
while(start3!=null)
start3=start3.next3;
}
else if(ch==2)
{
System.out.println("Enter the number of
polynomilas");
int n1=sc.nextInt();
System.out.println("Enter the values of
Polynomial 1 : ");
int i;
for(i=0;i<n1;i++)
{
System.out.println("Enter the exponent of
the polynomial ");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o1.poly1(ex, co);
}
System.out.println("Enter the values of
Polynomial 2 : ");
for(i=0;i<n1;i++)
{
System.out.println("Enter the exponent of
the polynomial");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o2.poly1(ex, co);
}
node1 t1;
node2 t2;
t1=o1.start1;
while(t1!=null)
{
t2=o2.start2;
while(!(t1.co==t2.co))
{
t2=t2.next2;
}
node3 z=new node3();
z.ex=t1.ex-t2.ex;
z.co=t1.co;
if(start3==null)
{
start3=z;
z.next3=null;
}
else
{
ptr3=start3;
while(ptr3.next3!=null)
{
ptr3=ptr3.next3;
}
ptr3.next3=z;
z.next3=null;
}
t1=t1.next1;
}
ptr3=start3;
System.out.println("The final polynomial is");
while(ptr3!=null)
{
System.out.print(ptr3.ex+" x^"+ ptr3.co+"
+ ");
ptr3=ptr3.next3;
}
}
else if(ch==3)
{
System.out.println("Enter the number of
polynomilas");
int n1=sc.nextInt();
System.out.println("Enter the values of
Polynomial 1 : ");
int i;
for(i=0;i<n1;i++)
{
System.out.println("Enter the exponent of
the polynomial ");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o1.poly1(ex, co);
}
System.out.println("Enter the values of
Polynomial 2 : ");
for(i=0;i<n1;i++)
{
System.out.println("Enter the exponent of
the polynomial");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o2.poly1(ex, co);
}
node1 t1;
node2 t2;
t1=o1.start1;
while(t1!=null)
{
t2=o2.start2;
while((t2!=null))
{
node3 z=new node3();
z.ex=t1.ex*t2.ex;
z.co=t1.co+t2.co;
if(start3==null)
{
start3=z;
z.next3=null;
}
else
{
ptr3=start3;
while(ptr3.next3!=null)
{
ptr3=ptr3.next3;
}
ptr3.next3=z;
z.next3=null;
}
t2=t2.next2;
}
t1=t1.next1;
}
ptr3=start3;
System.out.println("The final polynomial is");
while(ptr3!=null)
{
System.out.print(ptr3.ex+" x^"+ ptr3.co+"
+ ");
ptr3=ptr3.next3;
}
}
else if(ch==4)
{
int i;
System.out.println("Enter the number of terms in
polynomial");
int n2=sc.nextInt();
System.out.println("Enter the values of
Polynomial : ");
for(i=0;i<n2;i++)
{
System.out.println("Enter the exponent of
the polynomial ");
int ex=sc.nextInt();
System.out.println("Enter the degree of
the Polynomial");
int co=sc.nextInt();
o1.poly1(ex, co);
}
node1 t1;
t1=o1.start1;
while(t1!=null)
{
node3 z=new node3();
z.ex=t1.ex*t1.co;
z.co=t1.co-1;
if(start3==null)
{
start3=z;
z.next3=null;
}
else
{
ptr3=start3;
while(ptr3.next3!=null)
{
ptr3=ptr3.next3;
}
ptr3.next3=z;
z.next3=null;
}
t1=t1.next1;
}
ptr3=start3;
System.out.println("The final polynomial
is");
while(ptr3!=null)
{
System.out.print(ptr3.ex+" x^"+
ptr3.co+" + ");
ptr3=ptr3.next3;
}
}
else if(ch==5)
System.exit(0);
}
}
class node1
{
int ex,co;
node1 next1;
}
class polynomial1
{
node1 start1=null,pre1,ptr1;
public void poly1(int ex,int co)
{
node1 z=new node1();
z.ex=ex;
z.co=co;
if(start1==null)
{
start1=z;
z.next1=null;
}
else
{
ptr1=start1;
while(ptr1.next1!=null)
{
ptr1=ptr1.next1;
}
ptr1.next1=z;
z.next1=null;
}
}
public void display()
{
ptr1=start1;
while(ptr1!=null)
{
System.out.println(ptr1.ex+"\t"+ptr1.co);
ptr1=ptr1.next1;
}
ptr1=start1;
}
public node1 cool()
{
ptr1=start1;
ptr1=ptr1.next1;
return ptr1;
}
}
class node2
{
int ex,co;
node2 next2;
}
class polynomial2
{
int[] a=new int[100];
int[] b=new int[100];
int x=0,y=0;
node2 start2=null,pre2,ptr2;
public void poly1(int ex,int co)
{
node2 z=new node2();
z.ex=ex;
z.co=co;
if(start2==null)
{
start2=z;
z.next2=null;
}
else
{
ptr2=start2;
while(ptr2.next2!=null)
{
ptr2=ptr2.next2;
}
ptr2.next2=z;
z.next2=null;
}
}