0

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

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Narue
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.