un problemilla
necesito ayuda para borrar un elemento aleatoria d una lista enzada simple
Bueno,te ayudo:
Supongo que la lista tien un First y un Last,y cada nodo tiene un Info (la informacion que guarda) y un Next (puntero al proximo nodo)
Algo así:
void Lista::Eliminar(int posicion) //Primero=1
{
Nodo *cursor=First,*aux;
if (Last==NULL&&First==NULL)
return; //Lista vacia
Nodo *cursor=First,*aux;
if (posicion==1)
{
First=First->Next;
delete cursor;
if (First==NULL)
Last=NULL;
return;
}
for (int i=1;i<=posicion-1&&cursor;++i)
{
aux=cursor;
cursor=cursor->Next;
}
if (!cursor)
return; //Mostrar error
if (Last==cursor)
Last=aux;
aux->Next=cursor->Next;
delete cursor;
}
Supongo que la lista tien un First y un Last,y cada nodo tiene un Info (la informacion que guarda) y un Next (puntero al proximo nodo)
Algo así:
void Lista::Eliminar(int posicion) //Primero=1
{
Nodo *cursor=First,*aux;
if (Last==NULL&&First==NULL)
return; //Lista vacia
Nodo *cursor=First,*aux;
if (posicion==1)
{
First=First->Next;
delete cursor;
if (First==NULL)
Last=NULL;
return;
}
for (int i=1;i<=posicion-1&&cursor;++i)
{
aux=cursor;
cursor=cursor->Next;
}
if (!cursor)
return; //Mostrar error
if (Last==cursor)
Last=aux;
aux->Next=cursor->Next;
delete cursor;
}
