Hello. Not sure if this belongs in the beginner's forum or not, but thought I would put it here to start.
So, I made a linked list to represent a polynomial:
1 2 3 4 5 6
|
struct nodeType
{
double coeff; //coefficiect
int pow; // power
nodeType *link;
}; // end nodeType
|
And then made a class to represent the polynomial (making it, editing it, etc.):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class poly
{
private:
nodeType *head;
public:
poly(); //Default Constructor
//This will set head to NULL;
poly(double, int, nodeType *);//Copy Constructor
//This will set the double to coefficient, the int to power, and the pointer to link
~poly(); //Destructor
//This will deallocate all memory created with this program
int degree();
//This function returns the degree of the polynomial
double coefficient(int);
//This function returns the coefficient of the power input
void changeofcoefficient(double, int);
//This function finds the list member with the specified power and changes the coeffient
nodeType* mult(double);
//This function multiplies the polynomial by a constant, then returns the polynomial
void print();
//This function prints the polynomial
};
|
In my changeofcoefficient function (which is the only one you can edit the polynomials with...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
void poly:: changeofcoefficient(double newcoeff, int power)
{
nodeType *cur, *prev;
if (head == NULL)
{
head = new nodeType;
head->coeff = newcoeff;
head->pow = power;
head->link = NULL;
}
else
{
int n=0;
bool testcase = true;
cur = head;
prev = cur;
if (head->link == NULL)
cout << "Head's link is NULL" << endl;
else
cout << "Head's link is not NULL" << endl;
if (cur->link == NULL)
cout << "Current's link is NULL" << endl;
else
cout << "Current's link is not NULL" << endl;
if (cur->link != NULL)
cout << "Current's link is NOT NULL" << endl;
//Very confused. When tested, cur->link is null, yet it still enters loop, causing cur->power to be NULL, causing a core dump.
//Also, head is always == NULL, not sure why. And when head == NULL, skips over if (head == NULL) statement, and continues.
//So in this case, this is a very confusing case when true =/= true and false =/= false.
while ((cur->link != NULL) || (cur->pow > power)); //CORE DUMP HAPPENS HERE
// do
{
cout << "FLAG" <<++n<< endl;
prev = cur;
cout << "FLAG" <<++n<< endl;
cur = cur->link;
cout << "FLAG" <<++n<< endl;
/*
testcase = (testcase && (cur->pow > power));
cout << "FLAG9" << endl;
testcase = (testcase && (cur->link != NULL));
cout << "FLAG10" << endl;
*/
}
// while (testcase);
// while ((cur->pow > power) && (cur->link != NULL)); //CORE DUMP HAPPENS HERE
cout << "FLAG11" << endl;
if (cur->pow == power)
if (cur->pow == power)
{
cout << "CHANGING EXISTING COEF" << endl;
cur->coeff = newcoeff;
}
cout << "FLAG12" << endl;
|
etc.
My core dump happens here: when I compile and run, I get this:
What would you like in your polynomial? Please enter in the coefficient.
2
Please enter in the power.
2
Would you like to add elements to the current polynomial?
y
What would you like in your polynomial? Please enter in the coefficient.
3
Please enter in the power.
3
Head's link is NULL
Current's link is NULL
FLAG1
FLAG2
FLAG3
FLAG11
Segmentation fault (core dumped)
--------------------------------------------------------------------
Problems: Even when the while loop is false, it inters it anyway.
Even if head == NULL as shown by the cout statement, it foregoes the if statement on top.
Then obviously the coredump.
Another note: This function is called twice, once when head is actually null, and it works fine, the other time when head is not null (adding a secnod element to the polynomial) yet it reports head is null and core dumps that time.
If you need more code, let me know.
And this is a class project, but it was already due today, now just trying to learn from it :)