i have to do a java program to perform polynomial operations. i can manage those operations part. but i have a problem when storing the terms of the polynomial in to a linked list.

for example when it is given
polynomial("4 2 5 3 6 0"); // 4x^2+5x^3+6 may be any number of terms
how can i separately insert those terms in pairs to a one linked list.
please help me!

Recommended Answers

All 13 Replies

I didn't quite understood what you really want because I think it is very simple:

Vector v = new Vector();
v.add("4 2 5 3 6 0");


Vector v = new Vector();

or insert them in any format you like

The OO purist solution is to create a tiny class to hold info about the term of the polynomial., as in

class Term {
  private int power, multiplier;
   public Term(int power, multiplier) { etcv};
  // getters as usual
  // other useful stuff such as calculating the term for a given x

then the polynomial just becomes a vector of Terms:

Vector<Term> polynomial  = new Vector<Term>();
polynomial.add(new Term(2, 4)); etc

Maybe overkill in your case, but it's a good direction to start off in!

Of course, it's a lot easier if you just have the index of your array/list be the power: 4x^2+5x^3+6 --> [6, 0, 4, 5]

commented: damn right +18

thank you for everyone who replied to my post. iam really very happy about it. i did that part using String.split(" ") method.

now i have a problem in multiplying two polynomials.

below is a some part of code i used. all the insertFirst() and insertTail() are in their standard way of normal singly linked lists.

public void mulpolynomial(String poly1, String poly2) {

LinkList polyno3 = new LinkList();
	Link p1ptr = polyno1.first;
	Link p2ptr = polyno2.first;

		while(p1ptr != null){

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

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



public void addToLink(int cof, double expo, LinkList polyno3){
			Link p3ptr = polyno3.first;
			if(p3ptr == null){
				polyno3.insertFirst(cof, expo);
      	for( p3ptr = polyno3.first; p3ptr!=null; p3ptr = p3ptr.next){
      if(p3ptr.expoData == expo && p3ptr.expoData != polyno3.first.expoData )  
		     			p3ptr.coffData += cof;
		      		 if(p3ptr.expoData < expo)
		      			polyno3.insertFirst(cof, expo);
		      		 if(p3ptr.expoData > expo){
		      			polyno3.insertTail(cof, expo);


so this is my input
theList.mulpolynomial("2 3 4 2" , "6 2 2 1");
here comes the output of it
List (first-->last): {12, 5.0} {32, 4.0} {48, 4.0} {16, 3.0} {16, 3.0} {16, 3.0}

i dont know why it is happening like that.
i will be so grateful to you if anyone can solve my problem cause i am tackling with this for two days now.

To multiply two polynomials, you need to multiple each part of the first polynomial with each part of the second polynomial. This requires a for loop. And when you multiple polynomials, you need to add their exponents. I don't see you doing either of those things. . ?

no i have done it in that way. see in my code there are two variables called double C and int X for that purpose. its within the first part of my code.

Ok, I see. It looks like you are over complicating it though. If you use a nested for loop, you can go through, multiplying each number, and storing it in an appropriate place.

for (int i = 0; i < firstPolynomialsSize; i++){
for (int j = 0; j < secondPolynomialsSize; j++){
- Multiply i by j. Store the results in an array where the index represents the power, and the number stored at that index represents the coefficient. If there is already a number at the index you are trying to store something at, then add the two coefficients and store it there.

If this does not make sense, maybe you should try a simple example on paper, following the code I just posted.

no i have to do it using linked lists. can you please tell me what is wrong with my code or how should i change it. thankx

You use Ezzaral's suggestion to store the polynomials in the linked list, using the power as the index, and the coefficient as what is stored at that index. Then you modify my suggestion from above, it would look something like

for (int i = 0; i < firstLinkedList.size(); i++){
for (int j = 0; j < secondLinkedList.size(); j++){
- Multiply i * j. Store the results in a linked list where the index represents the power, and the number stored at that index represents the coefficient. If there is already a coefficient at the index you are trying to store something at, then add it to that number and store it there.

i am not using Ezzaral's suggestion. I have implemented the linked list.

public static void mulpolynomial(String poly1, String poly2) // multiply two polynomials

			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  )
		LinkList polyno2 = new LinkList();
		for(int i=0, j=1 ; i < pp2.length && j <pp2.length ; i = i+2, j = j+2  )

thats how i have done it. what i want to do is multiply polyno1 and polyno2. And if there are any terms with equal exponents in the resulting list. add up their coefficients.hope now you got it.

Well, good luck then. I don't know what half of the methods you used are supposed to be doing. I don't know why you'd want to insert something at the tail of a linked list as part of a multiply method. And I don't know what the split method does, sounds like it splits a String into parts, but I also don't know why you would want to do that. If you explain what each section of that code is doing and how you're attempting to multiply the polys, I'd be glad to assist you further. Or you can wait for someone else who understands whats going on. Completely up to you. Either way, good luck.

Here is a suggestion I wanted to give you from the start but after seeing your implementation I thought it would be better to follow your approach since you have already written code for it:

class SinglePolynimial {
  public int power = 0;
  public double factor = 0;

  public SinglePolynimial(double factor, int power) {
     this.power = power;
     this.factor = factor;

  public SinglePolynimial() {

  public void mult(SinglePolynimial single) {
      power = power + single.power;
      factor = factor * single.factor;

 public static SinglePolynimial mult(SinglePolynimial a, SinglePolynimial b) {
      int power = a.power + b.power;
      double factor = a.factor * b.factor;
      return new SinglePolynimial(factor, power);

new SinglePolynimial(3.1, 5);

So this polynimial: 3x^2 + 4x + 5
will be ONE Vector with elements SinglePolynimial objects, each elements for each part of the polynimial: 3x^2 + 4x + 5

So you will have 2 Vectors, multiply each element of one vector with each element of the other vector using the methods of the objects and store them somewhere, (maybe a third vector).

Then you can itereate the elements of the new vector, and those that have the same power, add them.
Or you can do the add in the same with the multiplication.
But be carefull to add only those that have the same power.
Maybe you can add these methods to the class as well:

public static SinglePolynimial add(SinglePolynimial a, SinglePolynimial b) {
      if (a.power!=b.power) {
          return null; //check the result before you use it.
      double factor = a.factor + b.factor;
      return new SinglePolynimial(factor, a.power);

public void add(SinglePolynimial single) {
      if (power!=single.power) {
      factor = factor + single.factor;

thank so much for everyone who helped me......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.