the list stores a group of terms in a polynomial
Code:
struct term
{
int co; //coefficient of a term
int exp; //exponent of a term
term* link;
};
typedef term* termptr;
class Polynomial
{
public:
void sort();
friend istream& operator >> (istream& ins, Polynomial& p1);
//other random stuff you don't need to know for this
private:
termptr head;
};
I'm trying to sort them once they've been inputted in order of exponent from greatest to least. here's what my sort function looks like:
Code:
void Polynomial::sort()
{
termptr cursor, current, prev;
int largest = 0;
current = head;
cursor = current->link;
prev = current;
for (termptr ptr = head; ptr != NULL; ptr = ptr->link)
{
largest = current->exp;
while (cursor)
{
if (cursor->exp > largest)
{
largest = cursor->exp;
prev->link = cursor->link;
ptr = cursor;
current = cursor;
cursor = prev;
cursor = cursor->link;
}else
{
prev = prev->link;
cursor = cursor->link;
}
}
current = current->link
prev = current
cursor = current->link;
}
}
istream& operator >> (istream& ins, Polynomial& p1)
{
char x;
string line;
termptr new_term, current;
new_term = new term;
new_term->link = NULL;
p1.set_head(new_term);
current = new_term;
getline(ins, line);
if (ins)
{
line.erase(remove(line.begin(), line.end(), ' '), line.end());
stringstream ss(line);
while (ss>>current->co >> x >> current->exp)
{
new_term = new term;
new_term->link = NULL;
current->link = new_term;
current = current->link;
}
}
current->link = NULL;
return ins;
}
//here's my application file
int main()
{
Polynomial poly1;
cout << "Enter a polynomial: ";
cin >> poly1;
cout << endl;
poly1.sort();
cout << poly1 << endl;
return 0;
}
it compiles fine, but when I try to call it in my application file I get a segmentation fault and I can't figure out why