Hi , I posted previously this problem, but dint get help , so i m posting it again , hoping i will be helped.
I have created a doubly linked list of polynomials. Now I m done with the basics.
I want to overload + operator.
It add two polynomials list , the first element of the first list searches for all the nodes with the same exponent in the second list. if it finds it , its add them together and insert them in a new list.And else if the exponent is nt matched it inserts both the first and second list nodes in the third list.
Now the problem is with the repetiton of nodes, like in the first iteration 2x^1 and 3x^1
are matched and added but then when the first list searchs for the node of x^2 since 3x^1 doesnt match with it. it inserts it. although it has been added previously and shouldnt be in the list.
I want to delete the nodes when i add them together but having problems with the implementation. Can anybuddy help. Its urgent!!
Code:
list operator + (const list& k)
{
node* first;
first = head;
node* f;
f = head;
node* second;
second = k.head;
node* s;
s = k.head;
node* result=head;
list r;
while (first != NULL)
{
second=k.head;
while (second != NULL)
{
if (first->exponent == second->exponent )
{
result->coefficient =first->coefficient + second->coefficient;
result->exponent=first->exponent;
r.insert(result->coefficient,result->exponent);
f=first->previous;
f->next=first->next;
first->next->previous=f;
delete first;
// first=f;
s=second->previous;
s->next=second->next;
second->next->previous=s;
delete second;
//second=s;
}
else
{
r.insert(second->coefficient, second->exponent);
r.insert(first->coefficient, first->exponent);
}
second=second->next;
}
first=first->next;
}
return r;
}