# multiplying polynomials using linked lists (solved)

Page 1 of 1

## 6 Replies - 15594 Views - Last Post: 11 April 2009 - 09:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=98278&amp;s=3e789fcc759f9582a73eafbdd6532282&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sm5312

Reputation: 2
• Posts: 106
• Joined: 09-April 09

# multiplying polynomials using linked lists (solved)

Posted 09 April 2009 - 11:32 AM

below is a method describes polynomial multiplication. all the insertTail() and insertFirst() are in the normal format of linked lists. can anyone please show me why it doesnt give me the right multiplication.

```public static void mulpolynomial(String poly1, String poly2) {

String[] pp1 = poly1.split(" ");
String[] pp2 = poly2.split(" ");

for(int i=0, j=1; i < pp1.length && j <pp1.length; i = i+2, j = j+2  )
polyno1.insertTail(Integer.parseInt(pp1[i]),Integer.parseInt(pp1[j]));

polyno1.displayList();

for(int i=0, j=1; i < pp2.length && j <pp2.length; i = i+2, j = j+2  )
polyno2.insertTail(Integer.parseInt(pp2[i]),Integer.parseInt(pp2[j]));

polyno2.displayList();

while(p1ptr != null){

while(p2ptr != null){
double  C = p1ptr.coffData * p2ptr.coffData;
int X = (p1ptr.expoData + p2ptr.expoData);

if(polyno3.first == null){
polyno3.insertTail(C, X);
}

p3ptr = polyno3.first;

p2ptr = p2ptr.next;
}

p2ptr = polyno2.first;
p1ptr = p1ptr.next;
}

polyno3.displayList();
}

if(p3ptr == null){
polyno3.insertTail(cof, expo);
polyno3.displayList();
}

else{
for( p3ptr = polyno3.first; p3ptr!=null; p3ptr = p3ptr.next){
if(p3ptr.expoData == expo  ){
p3ptr.coffData += cof;
break;
}
}

for( p3ptr = polyno3.first; p3ptr!=null; p3ptr = p3ptr.next){
if(p3ptr.expoData > expo  ){
polyno3.insertTail(cof, expo);
break;
}
}

}
}

```

my input- LinkList.mulpolynomial("2 3 4 2 " , "2 3 6 2 ");
output - List (first-->last): 4.0x^6 + 20.0x^5 + 8.0x^5 + 24.0x^4 +

it is adding the terms having same expoonent nicely but gives an exptra term (in this example 8.0x^5)

please someone help me. only this to be corrected in my program.

This post has been edited by pbl: 11 April 2009 - 09:19 AM

Is This A Good Question/Topic? 1

## Replies To: multiplying polynomials using linked lists (solved)

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8379
• Posts: 31,956
• Joined: 06-March 08

## Re: multiplying polynomials using linked lists (solved)

Posted 09 April 2009 - 03:54 PM

sm5312, on 9 Apr, 2009 - 10:32 AM, said:

below is a method describes polynomial multiplication. all the insertTail() and insertFirst() are in the normal format of linked lists. can anyone please show me why it doesnt give me the right multiplication.

I wouldn't say so.

But these 3 methods receive only one argument the object to insert in the LinkedList, yours have 2 args
So whithout seing them not much we can do

This post has been edited by pbl: 09 April 2009 - 04:45 PM

### #3 sm5312

Reputation: 2
• Posts: 106
• Joined: 09-April 09

## Re: multiplying polynomials using linked lists (solved)

Posted 09 April 2009 - 07:15 PM

so i will send you my insertTail() method. sorry i forgot , i havnt use a insertFirst() method.
```public void insertTail(double coff , int expo)//method to insert at the end of the list
{
if(!isEmpty()){
tail  = tail.next;

}

else
first = tail = new Link(coff , expo);
}

```

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8379
• Posts: 31,956
• Joined: 06-March 08

## Re: multiplying polynomials using linked lists (solved)

Posted 09 April 2009 - 07:46 PM

The LinkedList class has surelky a tail but this variable is private so not accessible

Why dont you simply use Java LinkedList:

```LinkedList<Link> poly = new LinkedList<Link>();
...
...

// now your  Link constructor seems to expect a double and a int so
double cof = Double.parseDouble(pp1[i]);
int exp = Integer.parseInt(pp2[i]));

```

### #5 sm5312

Reputation: 2
• Posts: 106
• Joined: 09-April 09

## Re: multiplying polynomials using linked lists (solved)

Posted 11 April 2009 - 09:13 AM

anyway thank you for helping me. i am finish with it now. how can i mark this as solved.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8379
• Posts: 31,956
• Joined: 06-March 08

## Re: multiplying polynomials using linked lists (solved)

Posted 11 April 2009 - 09:19 AM

sm5312, on 11 Apr, 2009 - 08:13 AM, said:

anyway thank you for helping me. i am finish with it now. how can i mark this as solved.

Go the first post
Edit it (full edit) and change the topic title
I'll do that for you

### #7 sm5312

Reputation: 2
• Posts: 106
• Joined: 09-April 09

## Re: multiplying polynomials using linked lists (solved)

Posted 11 April 2009 - 09:31 AM

oh! thankx. i thought there may be a separate option for that...cu