Solved

# infinite loop in linked list

Posted on 2009-04-28
Medium Priority
581 Views
I'm getting stuck in an infinite loop if my lists have a different number of nodes.  I'm trying to add and subtract polynomials.  Any help?
#include <string>
#include <iostream>
using namespace std;
struct TermType
{
int coefficient;
int exponent;
bool NegCoef;

TermType* next;
};

class Polynomial
{
private:

public:
// default constructor: create an empty polynomial
Polynomial();

// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);

// destructor
// release dynamic memory related to the polynomial
//~Polynomial();

// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);

void operator+=(const Polynomial& p1);

// subtract a polynomial
void operator-=(const Polynomial& p1);

// output a polynomial
void print();

//new input function
void insertTerm(bool,int,int);
};

Polynomial::Polynomial()
{
}
void Polynomial::operator+=(const Polynomial& p1)
{
cout<<"Start print"<<endl;
int counter=0;
while ((poly1!=NULL)||(poly2!=NULL))
{
counter++;
if (poly2==NULL)
cout<<poly1->coefficient<<"x^"<<poly1->exponent;

if (poly1==NULL)
cout<<poly2->coefficient<<"x^"<<poly2->exponent;
if (poly1!=NULL&&poly2!=NULL)
{
if (poly1->exponent==poly2->exponent)
{
cout<<poly1->coefficient+poly2->coefficient<<"x^"<<poly1->exponent;
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
continue;
}
if (poly1->exponent>poly2->exponent)
{
cout<<poly1->coefficient<<"x^"<<poly1->exponent;
if (poly2!=NULL)
poly2=poly2->next;
continue;
}
if (poly2->exponent>poly1->exponent)
{
cout<<poly2->coefficient<<"x^"<<poly2->exponent;
if (poly1!=NULL)
poly1=poly1->next;
continue;
}

}
}
}
void Polynomial::insertTerm(bool negCoef, int coef, int exp)
{
cout << "begin insert!" << endl;
cout << negCoef << "    " << coef << "    " << exp << endl;

TermType* newTerm = new TermType;
if (negCoef == 1)
newTerm->coefficient = 0 - coef;
else
newTerm->coefficient = coef;
newTerm->exponent = exp;
newTerm->next = NULL;

else
{
while (current->next != NULL)
current=current->next;
current->next = newTerm;
}
cout << "end insert!" << endl;
}
{
string s;
cout << "Enter a term (ex: +4x^3): ";
cin >> s;
int index = 0;
bool firstTerm = 1;
bool nowExp = 0;
bool negativeCoef = 0;
int coef = 0;
int exp = 0;

while (s[index] != '#')
{
cout << "char : " << s[index] << endl;
if (s[index] == '+' || s[index] == '-')
{
cout << "+-" << endl;
if (firstTerm == 0)
{
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
}
else
{
firstTerm = 0;
}
nowExp = 0;
if (s[index] == '-')
negativeCoef = 1;
else
negativeCoef = 0;
coef = 0;
exp = 0;
}
else if (s[index] == 'x')
{
cout << "x" << endl;
if (s[index+1] != '^')
{
cout << "Incorrect formula!" << endl;
break;
}
}
else if (s[index] == '^')
{
cout << "^" << endl;
nowExp = 1;
}
else if (s[index]>='0' && s[index]<='9')
{
cout << "number" << endl;
if (nowExp == 0)
coef = coef*10 + (s[index]-48);
else
exp = exp*10 + (s[index]-48);
}
else
{
cout << "The formula contains invalid characters: " << s[index] << endl;
break;
}
index++;
}
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
cout << "done!"<< endl;
}
void Polynomial::print()
{
while (newTerm != NULL)
{
cout<<newTerm->coefficient<<"x^"<<newTerm->exponent;
newTerm=newTerm->next;
}
cout << endl;
}
int main()
{
Polynomial p, p1;
cout<<"Second Term:"<<endl;
//p.print();
//p1.print();
p+=(p1);
system ("pause");
return 0;
}
0
Question by:scuzzo

LVL 45

Accepted Solution

AndyAinscow earned 750 total points
ID: 24257597
When poly1 or poly2 is NULL where do you move to the next member of the list ?
0

Author Closing Comment

ID: 31575671
Andy, thanks for the help.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
###### Suggested Courses
Course of the Month16 days, 2 hours left to enroll