You will want void insert(datatype); since you would be inserting that type (not ints).
A constructor for the datatype struct would be helpful: datatype(string Name, int Id, float Amt): name(Name), id(Id), amt(Amt) {}
Then, generate the values for Name, Id and Amt (random values if you wish) then call insert( datatype(Name,Id,Amt) ); to add it to the list.
Ok, here is my class and insert method, it is giving me error on the variables not being available. I thought everything in a struct was globally available?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class List
{
Node *head;
public:
void insert(datatype);
void remove();
List(){head = 0;}
};
void List::insert( datatype(Name,Id,Amt) );
if (head == NULL){
}
}
void List::insert( datatype dat )
{
Node* temp = new Node;
temp->data = dat;// all 3 datatype members will be copied here
temp->next = head;// the last Node added becomes second in the list
head = temp;// the newly added Node is now the first one in the list
}
Example of usage in main():
1 2 3 4 5 6 7
int main()
{
List myList;// declare an empty list
myList.insert( datatype("Jack", 123, 20.45f) );// insert 1st node
return 0;
}
When I use the code in my main() function, it tells me that datatype is undefined. Is that because the computer executes the main() code first? How do I declare it since it is a struct?
Well i thought I did have everything defined before the main(). I have the struct defined first. Without using a header, can you show me what it would look like?