Hello if anyone is kind enough to help me out with this problem. Basically what I am trying to do is after two polynomials gets multiplied, I want to simplify all the like terms. For example lets say after multiplication of two polynomials the answer is "2x^3+3x+4x^2+5x+1". I want the simplify function to put the like terms together. Now the answer gets stored in a list called TermList. I know my code works except for one place. That is where I am deleting the node. I am new to stl, I know how to handle this issue with regular link list but with stl I am not sure how to do it. The logic of this function is one pointer will point to the first node (curTerm), and an another pointer (nextTerm) will traversre the list looking for like term. If not found nextTerm pointer will move on. But if found nextTerm node's base will get added to the curTerm base and nextTerm node will get deleted. Now I know in a link list I would just have an extra pointer trail behind the nextTerm, and during deletion I would just point the trailing pointer point to nextTerm->next. and then delete the nextTerm node. But with stl I know that you do not have to do that. I just do not know how to do it though. I did add my code on the bottom, please tell me what I need to do to make this work. Thank you in advance.
P.S. if anyone has a better algorithm to this, I would be more than glad to take suggestions.
void Polynomial::Simplify()
{
cout<<"simplfy() is getting executed" << endl; //for testing purpose
list<Term>::iterator curTerm; //iter will be compared to
list<Term>::iterator nextTerm;//iter will traverse and compare
list<Term>::iterator delTerm;//iter not really needed added for testing
curTerm = TermList.begin();//iter points at the beggining of the list
nextTerm = TermList.begin();
for(curTerm; curTerm != TermList.end(); curTerm++)
{
nextTerm = curTerm++;
cout<<"simplify: currterm is: " << curTerm->base << " " << curTerm->exp << endl;//testing purpose
for(nextTerm; nextTerm != TermList.end(); nextTerm++)
{
cout<<"nextTerm is: " << nextTerm->base << " " << nextTerm->exp <<endl;//testing purpose
if(curTerm->exp == nextTerm->exp)
{//if like term is found
cout<<"found match" << endl;
curTerm->base = curTerm->base + nextTerm->base;
delTerm = nextTerm;//trying to delete the node
delTerm = TermList.erase(delTerm);
nextTerm++;
}
}
}
}