0

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.

2
Contributors
1
Reply
4
Views
8 Years
Discussion Span
Last Post by BestJewSinceJC
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.