Twhite1195 Twhite1195 - 27 days ago 15
C++ Question

Linked list with Template

I need to build a linked list with a template, but I have no idea why is not working, I've built linked lists before, but never with templates. Right now my problem is that, if I create the list everything is ok, but when I try to insert something to it, I get the following errors:

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


with my following code:

//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;
}
}


and

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


using namespace std;

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

system("pause");
return 0;
}

Answer

See the corrected version of Node and linkedList. Note that Node and linkedList do not contain any information about the actual data. In fact you can declare the data (struct MyData) at the end.

For printing I added a function to:

node->data.print();

This way Node and linkedList are not directly responsible for printing the data, and they don't need to know anything about the data. They can ask DataType to print the data. DataType must contain a print function to print its own content.

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;
};

Now you can declare MyData and use it. Make sure MyData includes a print function. Also MyData has to be POD (plain old data, it can't contain pointers) because of the way data is being assigned.

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;
}
Comments