0

This is my code for multiplying two polynomials using liked list.It works fine but the problem is if I multiply (3x^2+5x+3)*(4x^3+5^x+2)

I get the result as 12x^5+15x^2+6x^2+20x^4+25x^2+10x+12x^3+15x +6.

But how can I make it so that it outputs the terms with similar exponents are added togther like 12x^5+43x^2+..

public class LinkedPoly{
    static String exponent="";
    Node2 head;
    Node2 current;

    LinkedPoly(){
        head=null;

    }
    public void createList(int c,int e){
        head=new Node2(c,e,head);
    }
    public static LinkedPoly multiply(LinkedPoly list1,LinkedPoly list2){
        Node2 temp1=list1.head;
        Node2 temp2=list2.head;
        Node2 temp3=temp2;
        LinkedPoly multiplyList=new LinkedPoly();

        while(temp1!=null){
            while(temp2!=null){
                multiplyList.createList((temp1.coef*temp2.coef),(temp1.exp+temp2.exp)); 
                temp2=temp2.next;
            }
            temp2=temp3;
            temp1=temp1.next;
        }

        return multiplyList;
    }
dedek
  • 7,981
  • 3
  • 38
  • 68
clarkson
  • 561
  • 2
  • 16
  • 32

2 Answers2

1

One idea is to put the values into a map keyed on the degree of the exponent with a value indicating the coefficient. I.e.,

Map<Integer,Integer> exponents = new HashMap<Integer,Integer>()
....
// inside your while loop
int newcoeff = temp1.coef*temp2.coef
int newexp   = temp1.exp+temp2.exp
if(exponents.containsKey(newexp))
    exponents.put(newexp, exponents.get(newexp) + newcoeff)
else 
    exponents.put(newexp,newcoeff)

and then convert the HashMap back to a list.

dfb
  • 13,133
  • 2
  • 31
  • 52
  • This is the right basic idea. To avoid having a call to `containsKey` _and_ a call to `get`, it's probably better just to call `get`, then check whether the result is null. – Dawood ibn Kareem Jun 25 '14 at 19:21
  • @DavidWallace - In this case it doesn't matter, but this is generally not a good practice on map structures that allow null values since you can't distinguish between the item being absent or null – dfb Jun 25 '14 at 19:47
  • @dfb : Thanks for the answer.But I haven't learnt about maps.So if you can guide me with another approach that would be really helpful – clarkson Jun 26 '14 at 01:31
0

I hope I'm not solving some school homework or exercise for you. In that case you shouldn't use it!

This solution does not use Map, but it is much slower then the one posted by @dfb.

/**
 * @param list will be modified (merged).
 * @return the merged list param. 
 */
public static LinkedPoly merge(LinkedPoly list) {
    Node2 temp1 = list.head;

    while (temp1 != null) {
        Node2 iter = temp1; 
        Node2 temp2 = iter.next;
        while (temp2 != null) {
            if (temp1.exp == temp2.exp) {
                temp1.coef += temp2.coef;

                //removing temp2 form the source list
                iter.next = temp2.next;
            }
            iter = iter.next;
            temp2 = iter.next;
        }
        temp1 = temp1.next;
    }

    return list;
}

Instead of LinkedPoly.multiply(a, b) just call LinkedPoly.merge(LinkedPoly.multiply(a, b))

dedek
  • 7,981
  • 3
  • 38
  • 68