#include <iostream>
using namespace std;
class NO {
int idade;
string nome;
NO * no;
public :
void setDados( int , string) ;
int getidade( ) ;
string getnome( ) ;
void setNext( NO* ) ;
NO* getNext( ) ;
~NO( ) { } ;
} ;
class Lista {
NO * inicio;
int quant_el;
public :
Lista( ) {
quant_el = 0 ;
//inicio->setNext(NULL);
}
void inserir( int , string) ;
void pesquisar( NO) ;
void remover( int ) ;
void listar( ) ;
} ;
void NO :: setDados ( int idade, string nome) {
this- > idade = idade;
this- > nome = nome;
}
int NO :: getidade ( ) {
return idade;
}
string NO :: getnome ( ) {
return nome;
}
void NO :: setNext ( NO * no) {
this- > no = no;
}
NO* NO :: getNext ( ) {
return no;
}
void Lista :: inserir ( int idade, string nome) {
NO * aux;
aux = inicio;
NO * novo = new NO( ) ;
novo- > setDados( idade, nome) ;
novo- > setNext( NULL ) ;
if ( quant_el == 0 ) {
inicio = novo;
} else {
while ( ( aux- > getNext( ) ) ! = NULL ) {
aux = aux- > getNext( ) ;
}
aux- > setNext( novo) ;
}
quant_el++ ;
cout << "Foi inserido '" << idade << " " << nome << "'" << endl;
}
void Lista :: pesquisar ( NO obj) {
NO * aux;
int i;
int cont = 1 ;
int cont1 = 0 ;
aux = inicio;
if ( quant_el == 0 ) {
cout << "Não há nenhum dado na sua Lista." << endl;
} else {
for ( i = 0 ; i < quant_el; i++ ) {
cont++ ;
if ( aux- > getidade( ) == obj.getidade ( ) && aux- > getnome( ) == obj.getnome ( ) ) {
cont1++ ;
break ;
}
aux = aux- > getNext( ) ;
}
if ( cont1 == 0 ) {
cout << "O dado não foi encontrado." << endl;
} else {
cout << "Dado encontrado na posição " << cont - 1 << endl;
}
}
}
void Lista :: remover ( int pos) {
int i;
NO * aux, * aux1;
aux = inicio;
aux1 = aux- > getNext( ) ;
if ( pos > quant_el) {
if ( quant_el == 0 ) {
cout << "Sua lista está vazia, não há elementos para remover." << endl;
} else {
cout << "Posição inválida, sua lista possui menos elementos." << endl;
}
} else {
for ( i = 0 ; i < pos - 1 ; i++ ) {
aux = aux1;
aux1 = aux1- > getNext( ) ;
}
aux- > setNext( aux1- > getNext( ) ) ;
quant_el -- ;
cout << "Dado " << i + 1 << " removido: " << endl << aux1- > getidade( ) << " " << aux1- > getnome( ) << endl << endl;
delete aux1;
}
}
void Lista :: listar ( ) {
int i;
NO * aux;
aux = inicio;
if ( quant_el == 0 ) {
cout << "Sua lista está vazia, não há elementos para mostrar." << endl;
} else {
for ( i = 0 ; i < quant_el; i++ ) {
cout << i << " Dado:" << endl;
cout << aux- > getnome( ) << endl << aux- > getidade( ) << endl << endl;
aux = aux- > getNext( ) ;
}
}
}
int main( ) {
setlocale( LC_ALL, "" ) ;
Lista lista1;
NO loiola, rangel, humb;
loiola.setDados ( 18 , "Loiola" ) ;
rangel.setDados ( 29 , "Eustáquio" ) ;
humb.setDados ( 23 , "Humberto" ) ;
lista1.inserir ( 18 , "Filipe" ) ;
lista1.inserir ( 29 , "Eustáquio" ) ;
lista1.inserir ( 10000 , "Raul Seixas" ) ;
lista1.inserir ( 18 , "Leticia" ) ;
lista1.inserir ( 18 , "Mayra" ) ;
//lista1.inserir(23, "Humberto"); Se um nó a mais for inserido, o programa crasha
lista1.pesquisar ( loiola) ;
lista1.pesquisar ( rangel) ;
//lista1.pesquisar(loiola); Se um nó a mais for pesquisado, o programa crasha.
lista1.remover ( 2 ) ;
lista1.remover ( 3 ) ;
lista1.remover ( 1 ) ;
lista1.listar ( ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIE5PIHsKCglpbnQgaWRhZGU7CglzdHJpbmcgbm9tZTsKCU5PICpubzsKCnB1YmxpYzoKCgl2b2lkIHNldERhZG9zKGludCwgc3RyaW5nKTsKCWludCBnZXRpZGFkZSgpOwoJc3RyaW5nIGdldG5vbWUoKTsKCXZvaWQgc2V0TmV4dChOTyopOwoJTk8qIGdldE5leHQoKTsKCX5OTygpIHt9OwoKfTsKCmN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