Linked list
Step through a singly linked list of C++ pointers.
Everything is simulated in this browser; nothing is sent to any server.
Values
Create the new node
Create the new node
n_ = 0
Detached node
42
0x1010Pointercabeza_
nullptr — Empty list- Current
- Pointer
Step 1 / 46
Code
#include <iostream>
using namespace std;
class Nodo { // A node holds the data and a pointer to the next one
private: // Private attributes: only the methods touch them
int dato;
Nodo* siguiente;
public:
Nodo(int d) : dato(d), siguiente(nullptr) {}
int getDato() const { return dato; }
Nodo* getSiguiente() const { return siguiente; }
void setDato(int d) { dato = d; }
void setSiguiente(Nodo* s) { siguiente = s; }
};
class Lista {
Nodo* cabeza_; // First node of the list
int n_; // Number of nodes
public:
Lista() : cabeza_(nullptr), n_(0) {}
// Constructor de copia (no se anima)
Lista(const Lista& otra) : cabeza_(nullptr), n_(0) {
for (Nodo* p = otra.cabeza_; p != nullptr; p = p->getSiguiente())
insertarFinal(p->getDato());
}
// Destructor (no se anima)
~Lista() {
while (cabeza_ != nullptr) {
Nodo* temp = cabeza_;
cabeza_ = cabeza_->getSiguiente();
delete temp;
}
}
void insertarElemento(int valor) { // Insert at the head: O(1)
Nodo* nuevo = new Nodo(valor); // Create the node
nuevo->setSiguiente(cabeza_); // The new node points to the old head
cabeza_ = nuevo; // cabeza_ now points to the new node
n_++;
}
void insertarFinal(int valor) { // Insert at the tail: O(n)
Nodo* nuevo = new Nodo(valor); // Create the node
if (cabeza_ == nullptr) { // Empty list?
cabeza_ = nuevo;
} else {
Nodo* actual = cabeza_;
while (actual->getSiguiente() != nullptr) { // Walk to the tail
actual = actual->getSiguiente();
}
actual->setSiguiente(nuevo); // Link the new node after the tail
}
n_++;
}
// Devuelve la posición (0-based) o -1 si no existe
int buscar(int valor) const {
Nodo* actual = cabeza_;
int indice = 0;
while (actual != nullptr) {
if (actual->getDato() == valor) return indice; // Compare the data
actual = actual->getSiguiente();
indice++;
}
return -1; // Not found
}
void eliminarNodo(int indice) {
if (indice < 0 || indice >= n_) return; // Validate the index
Nodo* anterior = nullptr;
Nodo* actual = cabeza_;
for (int i = 0; i < indice; i++) {
anterior = actual;
actual = actual->getSiguiente();
}
if (anterior == nullptr)
cabeza_ = actual->getSiguiente(); // It was the head
else
anterior->setSiguiente(actual->getSiguiente()); // Skip the node
delete actual; // Free the memory
n_--;
}
void invertirLista() {
Nodo* anterior = nullptr;
Nodo* actual = cabeza_;
while (actual != nullptr) {
Nodo* siguiente = actual->getSiguiente(); // Save the next node
actual->setSiguiente(anterior); // Flip the link
anterior = actual;
actual = siguiente;
}
cabeza_ = anterior; // The last node becomes the head
}
void imprimirLista() const {
for (Nodo* p = cabeza_; p != nullptr; p = p->getSiguiente()) { // Walk the list
cout << p->getDato();
if (p->getSiguiente() != nullptr) cout << " -> "; // Separator between values
}
cout << endl;
}
};
int main() {
Lista lista;
lista.insertarElemento(3);
lista.insertarElemento(6);
lista.insertarElemento(9);
lista.imprimirLista(); // 9 -> 6 -> 3
lista.insertarFinal(1);
lista.eliminarNodo(lista.buscar(6));
lista.imprimirLista(); // 9 -> 3 -> 1
lista.invertirLista();
lista.imprimirLista(); // 1 -> 3 -> 9
return 0;
}