Hola Amigos!!!
Bueno me inscribí en este mi primer foro y quería si por favor me ayudaran a ver donde está mi error ya que en verdad no se el porque.
es sobre una Librería pero el método anterior & método siguiente que son la esencia por así decirlo de una Lista Dinámica Doble.
Bueno aquí les dejo todo el código Amigos:
LISTA .H
#define LISTA_H
#include "libro.h"
#include <iostream>
#include <string>
using namespace std;
class nodo
{
private:
libro data;
nodo *sig;
nodo *ant;
public:
void setData(libro dato)
{
data=dato;
}
void setSig(nodo* sigue)
{
sig=sigue;
}
void setAnt(nodo* antes)
{
ant=antes;
}
libro getData()
{
return data;
}
nodo* getSig()
{
return sig;
}
nodo* getAnt()
{
return ant;
}
friend class listaD;
};
class listaD
{
public:
listaD()
{
hd=NULL;
t=NULL;
}
nodo *hd,*t;
void inicializa();
bool vacia();
void insertarordenado(libro);
void mostrar();
void siguiente(string);
void anterior(string);
void eliminar(string);
void buscar(string);
void eliminartodo();
};
#endif // LISTA_H]
.CPP DE LA LISTA
#include "lista.h"
#include "libro.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
void listaD::inicializa()
{
hd=NULL;
t=NULL;
}
bool listaD::vacia()
{
if(hd==NULL&&t==NULL)
{
return true;
}
else
{
return false;
}
}
void listaD::insertarordenado(libro elemento)
{
nodo *aux=hd;
nodo *tmp=new nodo;
tmp->setData(elemento);
tmp->setSig(NULL);
tmp->setAnt(NULL);
//elemento.mostrar();
//system("pause");
// cout<<"LISTACANCIONES"<<aux->elemento;
if(hd==NULL)
{
hd=t=tmp;
}
else
{
if(hd->getData().getnombre()>elemento.getnombre())
{
tmp->sig=hd;
hd->ant=tmp;
hd=tmp;
}
else
{
while(aux->getSig()&&aux->getSig()->getData().getnombre()<elemento.getnombre())
{
aux=aux->getSig();
}
tmp->sig=aux->sig;
tmp->ant=aux;
aux->sig=tmp;
if(tmp->getSig()) tmp->sig->ant=tmp;
}
}
}
void listaD::mostrar()
{
nodo *aux=hd;
listaD ls;
if(vacia())
{
cout<< "\tLISTA VACIA"<<endl;
system("pause");
}
else
{
while(aux!=NULL)
{
aux->data.mostrar();
aux=aux->getSig();
}
}
}
void listaD::eliminar(string elemento)
{
nodo *aux=hd;
if(vacia())
{
cout<< "\tLISTA VACIA"<<endl;
system("pause");
}
else
{
while(aux!=NULL&&aux->getData().getnombre()!=elemento)
{
aux=aux->getSig();
}
if(aux==NULL)
{
cout<< "\tNO ENCONTRADO"<<endl;
system("pause");
}
else if(aux==t&&aux==hd)
{
delete(aux);
t=hd=NULL;
}
else if(aux==t)
{
t=t->getAnt();
delete(aux);
t->sig=NULL;
}
else if(aux==hd)
{
hd=hd->getSig();
delete(aux);
t->ant=NULL;
}
else if(hd!=NULL&&t!=NULL)
{
aux->ant->sig=aux->sig;
aux->sig->ant=aux->ant;
delete(aux);
}
}
}
void listaD::buscar(string elemento)
{
nodo *tmp=hd;
int i=1, band=0;
while(tmp!=NULL)
{
if(tmp->getData().getnombre()==elemento)
{
cout<< "\tELEMENTO ENCONTRADO"<<endl;
cout<< "\tPOSICION: "<<i<<endl;
system("pause");
tmp->getData().mostrar();
band=1;
system("pause");
}
tmp=tmp->getSig();
i++;
}
if(band==0)
{
cout<< "\tELEMENTO NO ENCONTRADO"<<endl;
system("pause");
}
}
void listaD::eliminartodo()
{
nodo *tmp=hd=t;
if(hd==NULL&&t==NULL)
{
cout<< "\tLISTA VACIA"<<endl;
system("pause");
}
else
{
while(hd!=NULL)
{
tmp=hd=t;
hd=hd->getSig();
t=t->getSig();
delete(tmp);
}
}
}
void listaD::siguiente(string elemento)
{
nodo *aux=hd;
if(vacia())
{
cout<< "\tLISTA VACIA"<<endl;
system("pause");
}
else
{
while(aux!=NULL&&aux->getData().getnombre()!=elemento)
{
aux=aux->getSig();
}
if(aux==NULL)
{
cout<< "\tNO EXISTE"<<endl;
system("pause");
}
else if(aux==t)
{
cout<< "\tULTIMO ELEMENTO"<<endl;
system("pause");
}
else if(aux->getData().getnombre()==elemento)
{
cout<< "\tSIGUIENTE"<<endl;
aux->getSig()->getData().mostrar();
}
}
}
void listaD::anterior(string elemento)
{
nodo *aux=t;
if(vacia())
{
cout<< "\tLISTA VACIA"<<endl;
system("pause");
}
else
{
while(aux!=NULL&&aux->getData().getnombre()!=elemento)
{
aux=aux->getAnt();
}
if(aux==NULL)
{
cout<< "\tNO ENCONTRADO"<<endl;
system("pause");
}
else if(aux==hd)
{
cout<< "\tPRIMER ELEMENTO"<<endl;
system("pause");
}
else if(aux->getData().getnombre()==elemento)
{
cout<< "\tANTERIOR"<<endl;
aux->getAnt()->getData().mostrar();
}
}
}
Espero Contar con su ayuda por favor la verdad no tengo idea por que no muestre. el método mostrar si funciona igual si quiere lo que viene siendo el .h y .cpp de el libro que es donde se ira guardando todo ala lista estaré al pendiente de sus respuestas.
Saludos!!!!!!!