Hi! guys! I have a question about generic linked list.
This is an example using C.
but now I have to translate it to C++, but I must use template instead of void*.Code:typedef struct list{ void *info; struct list *next; } List;
Code:template <class T> class Node{ public: T info; Node<T>* next; Node(T m, Node<T> *p = NULL) {info = m; prox = 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.
How can I use templates with generic data nodes?Code:int main(){ // One type nodes List<int> L; List<float> F; List<string> S; }
example:
*FirstPointer -> int -> float -> string -> int -> char -> NULL
thx



LinkBack URL
About LinkBacks



