6 Replies - 15231 Views - Last Post: 11 April 2009 - 09:31 AM Rate Topic: -----

#1 sm5312   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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(" ");
			
		
			LinkList polyno1 = new LinkList();
			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();
		
		LinkList polyno2 = new LinkList();
		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();
		
		LinkList polyno3 = new LinkList();
		Link n = new Link();
		
	Link p1ptr = polyno1.first;
	Link p2ptr = polyno2.first;
	Link p3ptr = polyno3.first;	
		while(p1ptr != null){
		
			while(p2ptr != null){
				double  C = p1ptr.coffData * p2ptr.coffData;
				int X = (p1ptr.expoData + p2ptr.expoData);
			
				
				addToLink(C, X, polyno3);

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

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

		polyno3.displayList();
		}



	public static void addToLink(double cof, int expo, LinkList polyno3){
			Link p3ptr = polyno3.first;


			if(p3ptr == null){
				polyno3.insertTail(cof, expo);
				polyno3.first.displayLink();
				polyno3.displayList();
			}
		
		
			else{
				  for( p3ptr = polyno3.first; p3ptr!=null; p3ptr = p3ptr.next){
					  p3ptr.displayLink();
					  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   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: multiplying polynomials using linked lists (solved)

Posted 09 April 2009 - 03:54 PM

View Postsm5312, 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.
The "normal" LinkedList methods are add() and addTail() which is your insertTail() and addFirst() which must be your insertFirst()

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

Was This Post Helpful? 0
  • +
  • -

#3 sm5312   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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()){
			  Link newLink = new Link(coff , expo);
			  tail.next= newLink;
			  tail  = tail.next;

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


Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8378
  • View blog
  • 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
so you use your own LinkedList class

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]));
poly.add(new Link(cof, exp);



Was This Post Helpful? 0
  • +
  • -

#5 sm5312   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: multiplying polynomials using linked lists (solved)

Posted 11 April 2009 - 09:19 AM

View Postsm5312, 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
Was This Post Helpful? 0
  • +
  • -

#7 sm5312   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1