135 lines
2.7 KiB
C++
135 lines
2.7 KiB
C++
#include <cstdlib>
|
|
#include <iostream>
|
|
#include "lista.h"
|
|
|
|
using namespace std;
|
|
|
|
Lista::Lista()
|
|
{
|
|
head = NULL;
|
|
current = NULL;
|
|
temp = NULL;
|
|
}
|
|
|
|
void Lista::AddNodeToEnd ( int data )
|
|
{
|
|
nodePtr n = new node;
|
|
n->next = NULL;
|
|
n->data = data;
|
|
|
|
if ( head != NULL ) { //Si la lista tiene elementos, inicia el recorrido.
|
|
current = head;
|
|
|
|
while ( current->next != NULL ) { //Si no esta al final de la lista, continua.
|
|
current = current->next;
|
|
}
|
|
current->next = n;
|
|
} else {
|
|
head = n;
|
|
}
|
|
}
|
|
|
|
void Lista::DeleteNode ( int data )
|
|
{
|
|
nodePtr delPtr = NULL;
|
|
temp = head;
|
|
current = head;
|
|
|
|
while ( current != NULL && current->data != data ) { //Siempre que hayan más nodos y la info del nodo actual no sea la buscada, entonces continúa buscando.
|
|
temp = current;
|
|
current = current->next;
|
|
}
|
|
if ( current == NULL ) { //Si se queda sin lista, no estaba el elemento.
|
|
cout << "Valor " << data << " no encontrado en la lista.\n";
|
|
delete delPtr;
|
|
} else {
|
|
delPtr = current;
|
|
current = current->next;
|
|
temp->next = current;
|
|
delete delPtr;
|
|
cout << "Elemento borrado.\n";
|
|
}
|
|
}
|
|
|
|
void Lista::PrintLista()
|
|
{
|
|
current = head;
|
|
while ( current!=NULL ) {
|
|
cout << current->data << endl;
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
void Lista::AddNodeToStart (int data)
|
|
{
|
|
nodePtr n = new node;
|
|
n->next = NULL;
|
|
n->data = data;
|
|
|
|
if (head != NULL)
|
|
{
|
|
current = head;
|
|
n->next = current;
|
|
head = n;
|
|
}
|
|
else
|
|
{
|
|
head = n;
|
|
}
|
|
}
|
|
|
|
void Lista::DeleteNodeByPosition (int position)
|
|
{
|
|
nodePtr delPtr = NULL;
|
|
temp = head;
|
|
current = head;
|
|
|
|
for(int i = 0; i < position ; ++i)
|
|
{
|
|
if(current->next != NULL)
|
|
{
|
|
temp = current;
|
|
current = current->next;
|
|
|
|
}
|
|
else
|
|
{
|
|
cout << "Esa posición no existe.\n";
|
|
break;
|
|
}
|
|
|
|
}
|
|
delPtr = current;
|
|
|
|
if (current->next != NULL)
|
|
{
|
|
current = current->next;
|
|
temp->next = current;
|
|
}
|
|
else
|
|
{
|
|
temp->next = NULL;
|
|
}
|
|
|
|
delete delPtr;
|
|
}
|
|
|
|
void Lista::FindNode(int data)
|
|
{
|
|
nodePtr n = new node;
|
|
temp = head;
|
|
current = head;
|
|
|
|
while ( current != NULL && current->data != data ) { //Siempre que hayan más nodos y la info del nodo actual no sea la buscada, entonces continúa buscando.
|
|
temp = current;
|
|
current = current->next;
|
|
}
|
|
if ( current == NULL ) { //Si se queda sin lista, no estaba el elemento.
|
|
cout << "Valor " << data << " no encontrado en la lista.\n";
|
|
}
|
|
else
|
|
{
|
|
cout << "El dato introducido fue encontrado.\n";
|
|
}
|
|
}
|