resueltos operaciones listas ligadas insertar fuente enlazadas eliminar elementos ejercicios ejemplos código con codigo buscar agregar c++ linked-list push-back

c++ - operaciones - Implementación de la función miembro de pushback de la lista enlazada



listas ligadas (1)

Soy un programador novato y esta es mi segunda pregunta sobre Stack Overflow.

Estoy tratando de implementar una función pushback para mi lista vinculada utilizando un puntero de cola. Parece lo suficientemente sencillo, pero tengo la sensación de que estoy olvidando algo o que mi lógica es perversa. ¡Las listas enlazadas son difíciles!

Aquí está mi código:

template <typename T> void LinkedList<T>::push_back(const T n) { Node *newNode; // Points to a newly allocated node // A new node is created and the value that was passed to the function is stored within. newNode = new Node; newNode->mData = n; newNode->mNext = nullptr; newNode->mPrev = nullptr; //If the list is empty, set head to point to the new node. if (head == nullptr) { head = newNode; if (tail == nullptr) { tail = head; } } else // Else set tail to point to the new node. tail->mPrev = newNode; }

Gracias por tomarse el tiempo de leer esto.


Señalando el mPrev incorrecto al nodo incorrecto. Y nunca estableció mNext del nodo de tail anterior si no era nulo para continuar la cadena de avance de su lista.

template <typename T> void LinkedList<T>::push_back(const T n) { Node *newNode; // Points to a newly allocated node // A new node is created and the value that was passed to the function is stored within. newNode = new Node; newNode->mData = n; newNode->mNext = nullptr; newNode->mPrev = tail; // may be null, but that''s ok. //If the list is empty, set head to point to the new node. if (head == nullptr) head = newNode; else tail->mNext = newNode; // if head is non-null, tail should be too tail = newNode; }