Hi! guys! I have a question about generic linked list.
This is an example using C.

typedef struct list{
	void *info;
	struct list *next;
} List;

but now I have to translate it to C++, but I must use template instead of void*.

template <class T>
class Node{
	public:
	T info;
	Node<T>* next;
	Node(T m, Node<T> *p = NULL) {info = m; next = p;}
};

template <class T>
class List{
	Node<T> *first;
	
	public:
	List(void){ first = NULL; }
        void insert(T m);
	void print();
};

I figured out that I can only get a list with one type node and not a list with different types of nodes.

int main(){
          // One type nodes
        List<int> L;
        List<float> F;
        List<string> S;
}

How can I use templates with generic data nodes?

example:

*FirstPointer -> int -> float -> string -> int -> char -> NULL

thx

>How can I use templates with generic data nodes?
Store polymorphic objects or pointers to void.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.