0
I’m racking my brain to solve an exercise, I need to create a program that inserts elements into a chained list and then create a function to verify that among the elements there are equal elements, and if so, delete all repetitions.
I do not know if I should use a recursive function for this, I tried to do a check with an auxiliary pointer, comparing each element with the others, but I could not.
#include<iostream>
using namespace std;
struct no{
int n;
no *prox;
};
struct lista{
no *inicio;
};
int menu(){
int op;
cout << "\nl. Inserir na lista\n";
cout << "2. Remover da lista\n";
cout << "3. Imprimir lista\n";
cout << "4. Sair\n";
cout << "Opcao: ";
cin >> op;
return op;
}
no* criarNo(){
int num;
cout << "Qual numero quer inserir: ";
cin >> num;
no *novo = new no;
novo->n = num;
novo->prox = NULL;
return novo;
}
void inserirLista(lista *l){
no *insere = criarNo();
if(l->inicio == NULL){
l->inicio = insere;
}
else{
no *aux = l->inicio;
if(aux->n > insere->n){
insere->prox = l->inicio;
l->inicio = insere;
}else {
while(aux->prox != NULL && aux->prox->n < insere->n){
aux = aux->prox;
}
insere->prox = aux->prox;
aux->prox = insere;
}
}
}
void imprimirLista(lista *inicio){
if(inicio->inicio == NULL)
cout << "Lista vazia\n";
else{
no *aux = inicio->inicio;
while(aux->prox != NULL){
cout << aux->n << " ";
aux = aux->prox;
}
cout << aux->n << " ";
}
}
void removerRepetidos(lista *l){
/* while(aux->prox != NULL){
if(aux->n == aux->prox){
aux->prox = aux->prox->prox;
}
else()
}*/
}
int main(){
int opcao;
lista *l = new lista;
l->inicio = NULL;
while(true){
opcao = menu();
switch(opcao){
case 1:
inserirLista(l);
break;
case 2:
cout << "Remover numeros repetidos: ";
removerRepetidos(l);
break;
case 3:
imprimirLista(l);
break;
case 4:
return -1;
default:
cout << "Opcao invalida\n";
break;
}
}
return 0;
}