class Node{
public:
void* info;
Node* next;
Node(/*which type for this parameter*/ m, Node *p = NULL) {//how to define my constructor}
};
class List{
Node *first;
public:
List(){ first = NULL; }
void insert( /*which type for this parameter*/ d ) {/*what I must make?*/};
void print(){/*??*/}
};
/*******************************
node_base() is a constructor and it
is initializing the data member next.
*******************************/
class node_base {
node_base* next;
public:
node_base(): next() {}
};
/**********************************************
Generic Class node, which inherits node_base. Note
that even though it is public inheritance, next will not be
inherited as it is private member of node_base.
node( const T& d ) : data( d ), is a constructor and
initializing member data to d.
***********************************************/
template< typename T >
class node : public node_base {
T data;
public:
node( const T& d ) : data( d ) {}
};
/******************************************
In the class declaration below, insert is a generic
function, i.e. any type of data type
******************************************/
class polymorphic_list {
node_base* head;
public:
polymorphic_list() : head() {}
template< typename T >
void insert( const T& d ) {
node_base* n = new node<T>( d );
// ... etc ...
}
};
So for ex. when you'll declare object of node something like
Node <int> obj;
type of member data and d will be int.
and for polymorphic list class.
Ok, thank you Kevinchkin for explanation, I begin understanding ,
But one case of list already to create how I am going to make to link up the elements of my list:
1 2 3
void insert( const T& d ) {
node_base* n = new node<T>( d );
head->next = n; // it's correct ??
And if I want to show the stocks of my list??
1 2 3 4 5 6 7
node_base* current ;
current = head ;
while (current->next != NULL)
return current->data ;
If you want to add elements to your list then you'll have to do this way. (Assuming you are passing head as a reference to this function. This head's value will be copied in p. So the signature of p is node_base** p; )
1 2 3 4 5 6 7 8 9 10
while (p->next != NULL) {
p = p->next; //Finding the end, so that we can add a new node
}
p->next = new node<T>(d);
p = p->next;
p->data = // whatever data you want to specify
p->next = NULL;
To show elements of your list
1 2 3 4 5 6
node_base* current;
current = head;
while (current != NULL) {
cout<<current->data;
current = current->next;
}
class polymorphic_list {
node_base* head, *tail;
public:
polymorphic_list() : head() {}
template< typename T >
void insertToHead( const T& d ) {
node_base* ptr = new node<T>( d );// here There is not a redandance?
ptr->data=d;// with this line
if(isEmpty())
head=ptr;
else
{
ptr->next=head;
head=ptr;
}
}
void insertToTail(const T& d)
{
if(isEmpty())
insertToHead(d);
else
{
node_base* ptr = new node<T>( d );
ptr->data=d;
tail->next=ptr;
tail=tail->next;
}
}
};
It could be...but it depends on what the constructor is doing. If for some reason the constructor didn't initialize ptr->data, with d, then no it isn't. But as it stands, it probably is.
node_base* current;
current = head;
while (current != NULL) {
cout<<current->data;
current = current->next;
}
data is not a member of class node_base !! --> i have the compilation error
& why the signature of p is node_base** p ?? because i have this error :
cannot convert 'node_base**' to 'node_base*' in assignment
1 2 3 4 5 6 7 8
template< typename T >
void insert( const T& d ) {
node_base** ptr = new node<T>( d );
//ptr->data=d;
tail->next=ptr; // in this line
tail=tail->next;
}