Quickish Data Structure Question
May 1, 2010 at 9:29am UTC
When you implement a data structure like a linear linked list as a class is it wrong to have the lll object be a member of your data class? Should it be the other way around: the data object as part of the linked list? Is there a general rule or is it case-by-case?
Thanks so much.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
class LLL
{
public :
LLL();
~LLL();
int add();
int remove();
private :
node * head;
};
class data
{
public :
data();
~data();
private :
LLL linked_list;
}
May 1, 2010 at 11:12am UTC
The general rule that applies here is the use of
templates :
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
//your declarations/definitions will look like this:
class Data
{
public :
Data();
~Data();
private :
//private stuff...
}
template <typename DataType>
//OR template <class DataType>
class LLL
{
public :
LLL();
~LLL();
int add();
int remove();
private :
node * head;
};
//more stuff here...
//and your instantiated objects will look like this:
int main()
{
LLL<Data> data_list;
LLL<int > int_list;
LLL<LLL*> list_of_pointers_to_list;
//etc...
return 0;
}
For more info about templates check this out:
http://cplusplus.com/doc/tutorial/templates/
Last edited on May 1, 2010 at 11:15am UTC
Topic archived. No new replies allowed.