I can't do
Code:
list::list(const list& l1)
because I call get_head() in the definition
I guess I can post my code for + and the copy constructor. the class isn't really called list, I just used that to simplify, it's actually called Polynomial and each node stores two ints instead of one. I edited the code in my first post to go with that. it's pretty long so I'll pseudocode parts of it
Code:
Polynomial::Polynomial (Polynomial& p1);
{
termptr temp, temp1;
temp = p1.get_head();
if (temp == NULL)
return;
temp1 = new term;
temp1->co = temp->co;
temp1->exp = temp->exp;
temp1->link = NULL;
head = temp1;
temp = temp->link;
for (termptr ptr = temp; ptr != NULL; ptr = ptr->link)
{
temp1->link = new term;
temp1 = temp1->link;
temp1->co = ptr->co;
temp1->exp = ptr->exp;
}
}
// = operator overload is the same
Polynomial Polynomial::operator + (Polynomial& p1)
{
Polynomial temp;
termptr temp_ptr, current, result;
termptr cursor_a = head, cursor_b = p1.get_head();
if (cursor_a->exp > cursor_b->exp)
{
//creates new term
//sets new term = cursor_a
//call temp.set_head with the new term
//increment cursor_a to the next node
}else if (cursor_b->exp > cursor_a->exp)
{
//creates new term
//sets new term = cursor_b
//call temp.set_head with new term
//increment cursor_a to next node
}else
{
//slightly more complicated so I'll code this
temp_ptr = new term;
temp_ptr->co = cursor_a->co + cursor_b->co;
temp_ptr->exp = cursor_a->exp;
temp.set_head(temp_ptr);
cursor_a = cursor_a->link;
cursor_b = cursor_b->link;
}
current = temp.get_head();
while ((cursor_a != NULL) || (cursor_b != NULL))
{
if (cursor_a == NULL)
{
current->link = new term;
current = current->link;
current->co = cursor_b->co;
current->exp = cursor_b->exp;
cursor_b = cursor_b->link;
}else if (cursor_b == NULL)
{
//same thing except set values in current to cursor_a and increment cursor_a
}else if (cursor_a->exp == cursor_b->exp)
{
//same thing except set current->co to cursor_a->co + cursor_b->co and increment
//both
}else if (cursor_a->exp > cursor_b->exp)
{
//same as if cursor_b == NULL
}else if (cursor_b->exp > cursor_a->exp)
{
//same as if cursor_a == NULL
}
}
return temp;
}
there it is, hope that helps