I was trying to create a linked list with structs. The idea is to use 2 different stucts, one which is a node, and another which is a pointer to the nodes (so, I can link the nodes together).
But I wanted to initialize the pointer to the first node as NULL, and create subsequent nodes later:
I am having an error in the 2 constructor methods (List and Polynomial), I can’t use the operator = like the way I am. But I can’t understand why.
struct List
{
//Data members to hold an array of pointers, pointing to a specific node
Node *list[100];
//Default constructor
List();
};
List::List()
{
*list[0] = NULL;
}
class Polynomial
{
public:
[...]
private:
List *poly; //Store the pointer links in an array
Node first_node;
int val;
};
Polynomial::Polynomial()
{
poly = new List();
}
/*******************************************************************************************************************************/
// Method : initialize()
// Description : This function creates the linked nodes
/*******************************************************************************************************************************/
Polynomial::void initialize(ifstream &file)
{
int y[20];
double x[20];
int i = 0, j = 0;
//Read from the file
file >> x[j];
file >> y[j];
first_node(x[j], y[j++]); //Create the first node with coef, and pwr
*poly->list[i] = &first_node; //Link to the fist node
//Creat a linked list
while(y[j] != 0)
{
file >> x[j];
file >> y[j];
*poly->list[++i] = new Node(x[j], y[j++]);
}
val = i+1; //Keeps track of the number of nodes
}
I have been getting errors in the Polynomial constructor and the List constructor.
It seems not like the link list. Maybe you need a struct
node(right ?) contain information andListcontain manynodeafter ? If this two, I has two solutions:first. very (and very) common.
Second. Style like I see in your code.
You see, the second version looks like the first 🙂