generic linked list

Hi, guys!

I need to create a generic linked list (Polymorphic containers),with class no the structur, I found this answer on the site
http://www.cplusplus.com/forum/general/1643/

but me me do not want the boost class used, and in the third resolution which offers ' Jsmith ' I understood nothing! somebody could me helped

thx

Ps :excusing my English because it's my third étrangére language
Do you have a specific C++ question?
how I must make to define my list ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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(){/*??*/}
};


I want indications ! PLZ
I am not sure what your question is, but as per not understanding solution 3 for the given URL, here is the explanation:
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

/*******************************
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.

1
2
polymorphic_list obj2; 
obj2.insert<int>(2); 


Hope this helps !
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; 
} 


Hope this helps !
Last edited on
thanks a lot kevinchekin :)

but in both methods insertToHead and insertToTail There is not a redandance? when I call the constructor and when I affect d in ptr-> data

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
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.
thx :)

but, in your code kevinchkin :

1
2
3
4
5
6
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;
     }



Topic archived. No new replies allowed.