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;
node* f;
node* second;
node* s;
list r;

while (first != NULL)
{
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;
}```

2. You do not have to delete any elements from any lists. You just have to advance the head pointers, isn't it. Whenever an element has been processed, whether it's in the first or second list, just advance its corresponding head pointer once, problem solved.

3. I haven't gone thoroughly into your code. But you delete second in the while loop. Then outside you do second = second->next; That seems like a bug. If the element second is pointing is deleted, then second->next points at nowhere. Thus second=second->next is bad.
It would make sense if you uncommented the //second = s; line.
Note that then you could do
Code:
```second->next->previous = second->previous;
second->previous->next = second->next;
second = second->next; //do this inside the if rathern than outside
delete second;```
and not need the pointer s
(the same is for first and f of course)