Question:
I have this program to work with doubly linked list in C ++. What works badly is the function deletefinal, which as its name implies it would have to delete the node from the end of the list with its content, but instead of doing that it shows memory addresses. How can I fix the error?
#include<iostream>
#include<windows.h>
using namespace std;
struct lista
{
int n;
struct lista *sig, *ant;
}*CAB=NULL, *P=NULL, *AUX=NULL, *F=NULL, *Q=NULL, *QD=NULL, *FD=NULL;
int main (void)
{
void insertar(void);
void visualizar();
void eliminarinicio();
void eliminarfinal();
char opc;
do
{
system("cls");
cout<<"\t|------Operaciones con listas-------|"<<endl;
cout<<"\t|-----------------------------------|"<<endl;
cout<<"\t| 1)Insertar |"<<endl;
cout<<"\t| 2)Visualizar |"<<endl;
cout<<"\t| 3)Eliminar al inicio |"<<endl;
cout<<"\t| 4)Eliminar al final |"<<endl;
cout<<"\t| 0)Salir |"<<endl;
cout<<"\t|-----------------------------------|"<<endl;
cout<<endl<<endl<<"ingrese una opcion"<<endl;
opc=std::cin.get();
system("cls");
switch (opc)
{
case '1':
insertar();
break;
case '2':
visualizar();
break;
case '3':
eliminarinicio();
break;
case '4':
eliminarfinal();
break;
}
}
while (opc!='0');
return 0;
}
void insertar(void)
{
P=CAB;
AUX= new lista;
system("cls");
cout<<"**************INSERTAR******************"<<endl;
cout<<"****************************************"<<endl;
cout<<" Ingrese numero : ";
cin>>AUX->n;
F=AUX;
if (CAB==NULL)
{
CAB=AUX;
P=AUX;
}
else
{
while (P->sig!=CAB)
{
P=P->sig;
}
}
P->sig=AUX;
AUX->sig=CAB;
AUX->ant=P;
CAB->ant=AUX;
}
void visualizar (void) {
system("cls");
if(CAB==NULL){
cout<<"LISTA VACIA";
getchar();
return ;
}
cout<<"*****LOS NUMEROS INGRESADOS FUERON****"<<endl;
P=CAB;
cout<<endl<<"LISTA: "<<endl;
do{
cout<<"->"<<P->n<<endl;
P=P->sig;
}while (P!=CAB);
system("pause");
}
void eliminarinicio(void){
system("cls");
int valor;
if(CAB==NULL){
cout<<"lista vacia"<<endl;
getchar();
return;
}
cout<<"********ELIMINANDO ELEMENTO********"<<endl;
system("pause");
if(CAB!=AUX){
P=CAB;
CAB=CAB->sig;
CAB->ant=AUX;
AUX->sig=CAB;
valor=P->n;
delete P;
cout<<endl<<"se elimino " <<valor<<" de la lista";
}else if(CAB==AUX){
cout<<endl<<"se elimino "<<CAB->n<<", de la lista";
CAB = NULL;
}
getchar();
}
void eliminarfinal(void){ //revisar
system("cls");
int valor;
if(AUX==NULL){
cout<<"LISTA VACIA"<<endl;
getchar();
return;
}
cout<<"********ELIMINANDO ELEMENTO********"<<endl;
system("pause");
if(AUX!=CAB){
P=CAB;
AUX=AUX->ant;
AUX->sig=CAB;
CAB->ant=AUX;
valor=F->n;
delete F;
cout<<endl<<"se elimino " <<valor<<" de la lista";
}else if(AUX==CAB){
cout<<endl<<"se elimino "<<AUX->n<<", de la lista";
CAB = NULL;
}
getchar();
}
Answer:
As user SuperG280 says, the procedure error occurs when trying to remove the last node. Do F = CAB and then delete F, which removes CAB. So it shows addresses in memory instead of the contents of the nodes. One of the solutions is to substitute P = CAB; by F = CAB ;. The corrected deletefinal code would look like this:
void eliminarfinal(void){
system("cls");
int valor;
if(AUX==NULL){
cout<<"LISTA VACIA"<<endl;
getchar();
return;
}
cout<<"********ELIMINANDO ELEMENTO********"<<endl;
system("pause");
if(AUX!=CAB){
F=CAB; //<---------- Acá está el cambio, P=CAB; por F=CAB;
AUX=AUX->ant;
AUX->sig=CAB;
CAB->ant=AUX;
valor=F->n;
delete F;
cout<<endl<<"se elimino " <<valor<<" de la lista";
}else if(AUX==CAB){
cout<<endl<<"se elimino "<<AUX->n<<", de la lista";
CAB = NULL;
}
getchar();
}
Another way to correct the error would be to assign AUX to P instead of CAB as it appears in the code. It would look like this:
void eliminarfinal(void){
system("cls");
int valor;
if(AUX==NULL){
cout<<"LISTA VACIA"<<endl;
getchar();
return;
}
cout<<"********ELIMINANDO ELEMENTO********"<<endl;
system("pause");
if(AUX!=CAB){
F=AUX; //<---------- Acá está el cambio, F=CAB; por F=AUX;
AUX=AUX->ant;
AUX->sig=CAB;
CAB->ant=AUX;
valor=F->n;
delete F;
cout<<endl<<"se elimino " <<valor<<" de la lista";
}else if(AUX==CAB){
cout<<endl<<"se elimino "<<AUX->n<<", de la lista";
CAB = NULL;
}
getchar();
}