I have a Doubly Linked List and Node template. I want to implement an object of type LinkedList within the structure from which I create the main LinkedList. The problem I have is that I don't quite understand how to access the internal list.
MyNode receives a structure to create the nodes that go inside the Linked List; so I want to access a Linked List which will be inside the main structure. I have no idea how to do this, I'm so lost.
I attach my LL template and my structure declaration.
And then I only allocated memory inside my void main(). Please I need help.
Template:
#pragma once
#ifndef LL
#define ll
template <class T1> struct Nodo {
public:
T1 info;
Nodo* prev;
Nodo* next;
Nodo(){ }
Nodo(T1 data)
{
this->info = info;
}
~Nodo() {}
};
template <class T> class ListaLigada
{
public:
T* head=NULL, *tail=NULL;
ListaLigada();
void AddNodo(T nodo);
};
template <class T> void ListaLigada<T>::AddNodo(T nodo)
{
T* aux = NULL;
aux = new T(nodo);
aux->next = NULL;
aux->prev = NULL;
if (head == NULL)
{
head = aux;
tail = aux;
}
else
{
tail->next = aux;
aux->prev = tail;
tail = aux;
}
}
#endif
You have serious design, concept, and implementation issues in your code.
LL
, but definell
.#pragma once
, which is the include guard of the Microsoft C++ compiler, so it is not standard or portable.default
, same for the destructor.Nodo::info
to itself.Alumno
stores a pointer to list, this is error prone, use an instance.Alumno
stores text data as character arrays; that approximation is typical of C, in C++ it is usually usedstd::string
.To access the list inside
Alumno
, just use the dot (.
) operator:But this would be with the code as you have it now, which is neither correct nor adequate, my proposal is the following:
I also propose to change
Alumno
:With the above code, you can add
Materia
s as follows: