Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在链表中使用模板_C++_Templates - Fatal编程技术网

C++ 在链表中使用模板

C++ 在链表中使用模板,c++,templates,C++,Templates,我遇到了这个问题,我需要实现一个链表,但是节点中存储的元素的数据类型可能是字符串或指向另一个类的指针 class Node{ public: string data; void *link; Node *next; Node(){ link = next = NULL; } Node(string str){ data = str;

我遇到了这个问题,我需要实现一个链表,但是节点中存储的元素的数据类型可能是字符串或指向另一个类的指针

class Node{
    public:
        string data;
        void *link;
        Node *next;

        Node(){
            link = next = NULL;
        }
        Node(string str){
            data = str;
        }
        Node(void *ptr){
            link = ptr;
        }
};

class List{
    Node *head;
    Node *tail;

    public:
        void insert(string str){
            Node *newNode = new Node(str);
            /* ... */
        }
        void insert(void *ptr){
            Node *newNode = new Node(ptr);
            /* ... */
        }
};

我尝试使用模板,但我无法使用,我如何使用模板进行此操作?

您可能会执行以下操作:

template <class T>
class List 
{
public:
    List(): root(NULL) {};
    ~List();
    bool add(const T& item);
    ....

private:
    typedef struct Node {
        T item;
        struct Node *next;
    } Node; 
    Node *root;
};
template<typename T>
class Node
{ 
public: 
    Node *next; 
    T data; 

    Node(const T &value)
        : next(NULL), data(value)
    {
    } 
}; 

template<typename T>
class List
{ 
private:
    Node<T> *head; 
    Node<T> *tail; 

public: 
    List()
        : head(NULL), tail(NULL)
    {
    }

    void insert(const T &value)
    { 
        Node<T> *newNode = new Node<T>(value); 
        if (!head)
            head = newNode;
        if (tail)
            tail->next = newNode;
        tail = newNode;
    } 
}; 
模板
班级名单
{
公众:
List():根(NULL){};
~List();
bool add(常数T和项目);
....
私人:
类型定义结构节点{
T项;
结构节点*下一步;
}节点;
节点*根;
};
看到这个问题的其他答案会很有趣。
C++不是我最强的话题,但是这个例子应该编译和工作。你知道,C++中的结构是一种“默认的公共”类,所以你甚至可以在其中包含函数(我宁愿把私有函数添加到你的列表中)。

< P> STL有一个<代码> STD::list < /Cord>模板化类,你真的应该使用它。但如果您想实现自己的类,请尝试以下方法:

template <class T>
class List 
{
public:
    List(): root(NULL) {};
    ~List();
    bool add(const T& item);
    ....

private:
    typedef struct Node {
        T item;
        struct Node *next;
    } Node; 
    Node *root;
};
template<typename T>
class Node
{ 
public: 
    Node *next; 
    T data; 

    Node(const T &value)
        : next(NULL), data(value)
    {
    } 
}; 

template<typename T>
class List
{ 
private:
    Node<T> *head; 
    Node<T> *tail; 

public: 
    List()
        : head(NULL), tail(NULL)
    {
    }

    void insert(const T &value)
    { 
        Node<T> *newNode = new Node<T>(value); 
        if (!head)
            head = newNode;
        if (tail)
            tail->next = newNode;
        tail = newNode;
    } 
}; 
模板
类节点
{ 
公众:
节点*下一步;
T数据;
节点(常数T和值)
:下一个(空)、数据(值)
{
} 
}; 
模板
班级名单
{ 
私人:
节点*头;
节点*尾部;
公众:
列表()
:头(空)、尾(空)
{
}
无效插入(常数T和值)
{ 
节点*新节点=新节点(值);
如果(!头)
头=新节点;
如果(尾部)
tail->next=newNode;
tail=newNode;
} 
}; 

一个
列表
应该能够混合保存一些字符串和一些指针,还是应该为字符串列表和指针列表提供单独的类型?当你们尝试使用模板方法时,出了什么问题?我希望字符串列表和指针列表分开,当我尝试时,编译器给了我一个错误列表:pIs这是家庭作业吗?是否有理由不包含#我建议您首先创建一个int列表,以确保所有列表操作都能正常工作。在您确信您的int类列表没有任何bug之后,然后将其更改为可以使用任何类型的模板类。就我个人而言,我将使用
root
初始化为
NULL
构建一个空的
列表。通过这种方式,您可以区分空列表和具有单个默认构造元素的列表之间的区别。除此之外,你有一个很好的开始。谢谢你的建议Max。我已经调整了帖子。你是对的,我也会这么做。