I'm having trouble removing an item by the value of a simply linked list. I'm doing this:
class No{
public:
int dado;
No *next;
No(int item, No *ptr= NULL){
dado=item;
next=ptr;
}
};
class Lista{
public:
Lista(){
first = last = NULL;
};
bool listaVazia();
void inserirInicio(int item);
void inserirFim(int item);
void inserirPosicao(int item, int posicao);
void buscaItem(int item);
void removeInicio();
void removeFim();
void removeItem(int item);
void imprimirLista();
int tamanhoLista = 0;
private:
No *first, *last;
};
I can already check the function if it is inserting at the beginning and at the end, I need to understand how I'm going to remove it from a position in the middle so it does not get lost:
void Lista::inserirPosicao(int item,int posicao){
if(posicaoValida(posicao)){
if(posicao == 1){ // INSERI NA PRIMEIRA POSICAO
inserirInicio(item);
}else{
if(last->next == NULL){ // INSERI NA ULTIMA POSICAO
inserirFim(item);
return;
}else{ // INSERIR NO MEIO
}
}
}else{
cout << endl << " POSICAO INVALIDA" << endl;
}
}