c++ - 带模板的链表

标签 c++ templates linked-list

我需要使用模板构建链表,但我不知道为什么不起作用,我以前构建过链表,但从未使用模板。现在我的问题是,如果我创建列表,一切正常,但是当我尝试向其中插入内容时,出现以下错误:

Error   C2664   'Nodo<D>::Nodo(Nodo<D> &&)': cannot convert argument 1 from 'const int' to 'const Nodo<D> &'    Datos2  d:\google drive\visual studio 2015\projects\datos2\datos2\listaSimple.h 69


Error   C2664   'Nodo<D>::Nodo(Nodo<D> &&)': cannot convert argument 1 from 'const int' to 'const Nodo<D> &'    Datos2  d:\google drive\visual studio 2015\projects\datos2\datos2\listaSimple.h 73

使用我的以下代码:

    //linkedList.h
#pragma once
#ifndef _LISTASIMPLE_H
#define _LISTASIMPLE_H

template<class D> 
struct Nodo
{
    int carga;
    int binario;

    D caracter;

    Nodo<D> *Siguiente;//means next
};



template<class D>
class listaSimple
{

public:
    listaSimple();
    ~listaSimple();

    void InsertarInicio(const D&);
    bool ListaVacia();
    void Mostrar();




private:
    Nodo<D> *primero;
    Nodo<D> *ultimo;

};

template<class D> 
listaSimple<D>::listaSimple()
{
    primero = NULL;
}

template<class D>
listaSimple<D>::~listaSimple()
{
    Nodo<D> *aux;
    while (primero != NULL)
    {
        aux = primero;
        primero = primero->Siguiente;
        delete aux;
    }
}

template<class D>
void listaSimple<D>::InsertarInicio(const D& dato)
{
    if (ListaVacia())
    {
        primero = new Nodo<D>(dato);
    }
    else
    {
        Nodo<D> *nodoNuevo = new Nodo<D>(dato);
        nodoNuevo->Siguiente = primero;
        primero = nodoNuevo;
    }
}

template<class D>
bool listaSimple<D>::ListaVacia()
{
    if (primero == NULL)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template<class D>
inline
void listaSimple<D>::Mostrar()
{
    Nodo<D> *aux = primero;
    while (aux != NULL)
    {
        cout << aux->caracter << "->";
        aux = aux->Siguiente;
    }
}

//Source.cpp
#include <iostream>
#include <string>
#include "linkedList.h"


using namespace std;

int main() {
    listaSimple<int> Nueva;
    Nueva.InsertarInicio(5);

    system("pause");
    return 0;
}

最佳答案

请参阅 Node 的更正版本和 linkedList .注意 NodelinkedList不包含有关实际数据的任何信息。事实上,您可以在最后声明数据 ( struct MyData)。

为了打印,我添加了一个函数:

node->data.print();

这边NodelinkedList不直接负责打印数据,他们不需要了解数据的任何信息。他们可以问DataType打印数据。 DataType必须包含 print打印自己的内容的功能。

template<typename DataType>
struct Node
{
    DataType data;
    Node<DataType> *Next;
    Node()
    {
        Next = nullptr;
    }
};

template<typename DataType>
class linkedList
{
public:
    linkedList()
    {
        first = NULL;
    }

    ~linkedList()
    {
        Node<DataType> *aux;
        while (first != NULL)
        {
            aux = first;
            first = first->Next;
            delete aux;
        }
    }

    void InsertBegining(const DataType& data)
    {
        Node<DataType> *newNode = new Node<DataType>;
        newNode->data = data;
        if (first)
        {
            newNode->Next = first;
            first = newNode;
        }

        first = newNode; //<== you forgot this
    }

    void Print()
    {
        Node<DataType> *walk = first;
        while (walk)
        {
            walk->data.print();
            walk = walk->Next;
        }
    }

private:
    Node<DataType> *first;
};

现在你可以声明MyData并使用它。确保 MyData包括 print功能。还有 MyData由于分配数据的方式,必须是 POD(普通旧数据,它不能包含指针)。

int main() 
{
    struct MyData
    {
        int charge;
        int binario;
        char ch;
        void print()
        {
            cout << charge << ", " << binario << ", " << ch << "\n";
        }
    };

    linkedList<MyData> list;
    MyData data;

    data.binario = 1;
    data.ch = 'A';
    data.charge = 10;
    list.InsertBegining(data);

    data.binario = 2;
    data.ch = 'B';
    data.charge = 20;
    list.InsertBegining(data);

    list.Print();

    system("pause");
    return 0;
}

另一种方法:

您可以添加 << MyData 的运算符重载

struct MyData
{
    int charge;
    int binario;
    char ch;

    friend std::ostream& operator<< (std::ostream &out, MyData &x)
    {
        out << x.ch << ", " << x.binario << ", " << x.charge;
        return out;
    }
};

所以 MyData知道如何打印自己。示例:

MyData data;
data.ch = 'A';
data.binario = 1;
data.charge = 10;
cout << data << "\n";

这应该打印 "A, 1, 10" .

然后你可以改变linkList::Print()

...
void Print()
{
    Node<DataType> *walk = first;
    while (walk)
    {
        std::cout << walk->data << "\n";
        walk = walk->Next;
    }
}

现在linkedList独立于MyData只要MyData<<运算符重载(其数据为 POD)。您还可以将此链表用于基本类型。示例:

linkedList<int> test;
test.InsertBegining(1);
test.InsertBegining(2);
test.Print();

关于c++ - 带模板的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443721/

相关文章:

c++ - 如何将 GCC 诊断编译指示与 C++ 模板函数一起使用?

C++:将 const 与 STL 迭代器一起使用

c++ - 在哪里以及为什么我必须放置 "template"和 "typename"关键字?

c++ - 如何访问属于另一个类的私有(private)成员的类的方法

C++ 语言一些可变的实例

c++ - 将 QSqlQueryModel 数据转换为 QVector(s)

c++ - 字符串数组/字符数组

c - C中的链表删除函数问题

algorithm - 循环检测算法 : Is there a condition for Tortoise and Hare to enter into cycle?

java - 在 JVM 通过 JNI 启动时重新加载 java 类路径