Void* and Template for Linked List

This is a discussion on Void* and Template for Linked List within the C++ Programming forums, part of the General Programming Boards category; Hi! guys! I have a question about generic linked list. This is an example using C. Code: typedef struct list{ ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Void* and Template for Linked List

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

    Code:
    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*.


    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.

    Code:
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    How can I use templates with generic data nodes?
    Perhaps by using a List<boost::any>, but I am not familiar with boost::any myself.

    Why do you want a linked list with nodes of varying unrelated types in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Actually I was trying to make a list of different basic types and then to make one with generic classes.

    Now I got the idea.

    thx

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    It is possible to do it, but... Perhaps boost is easier (I don't know myself). Otherwise you will need to use polymorphism and probably a variable to remember the type in the linked list, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21