using namespace std;
struct lista{
int dato;
struct lista sig;
}entero;
void crear(struct lista );
void insertar(struct lista , int);
void eliminar(struct lista , int);
void recorrer(struct lista );
int buscar(struct lista **, int);
int main(){
system("cls");
system("COLOR 3f");
int dato;
crear(&entero);
cout << "OPERACIONES CON LA LISTA ENLAZADA\n\n";
cout << "Para finalizar la insercion ingrese el cero\n\n";
cout << "Ingrese un numero: ";
cin >> dato;
while (dato != 0){
insertar(&entero, dato);
cout << "Ingrese un numero: ";
cin >> dato;
}
cout << "\n\nELEMENTOS DE LA LISTA \n\n";
recorrer(&entero);
cout << "\n\n Ingrese el numero a eliminar: ";
cin >> dato;
eliminar(&entero, dato);
recorrer(&entero);
cout << "\n\nIngrese el numero a buscar: ";
cin >> dato;
if (buscar(&entero, dato) == 1)
cout << "\nEl numero " << dato << " fue encontrado\n";
else
cout << "El numero no existe " << endl;
getchar();
return 0;
}
//funciones d elistas simplemente enlazada
void crear(struct lista *entero){
entero = NULL;
}
void insertar(struct lista *entero, int dato){
struct lista auxiliar, puntero, anterior;
auxiliar = new lista;
if (!auxiliar){
cout << "Error:memoria insuficiente" << endl;
exit(1);
}
auxiliar->dato = dato;
anterior = NULL;
puntero = entero; //puntero es el puntero auxiliar que recorre la lista
while ((puntero != NULL) && (puntero->dato < dato)){
anterior = puntero;
puntero = puntero->sig;
}
if (anterior == NULL){
auxiliar->sig = entero;
*entero = auxiliar;
}
else{
anterior->sig = auxiliar;
auxiliar->sig = puntero;
}
}
void eliminar(struct lista *entero, int dato){
struct lista puntero, anterior;
puntero = entero;
anterior = NULL;
while ((puntero != NULL) && (puntero->dato < dato)){
anterior = puntero;
puntero = puntero->sig;
}
if (puntero->dato != dato)
cout << "El numero no existe" << endl;
else
{
if (anterior == NULL)//1er lista
entero = (entero)->sig;
else
anterior->sig = puntero->sig;
delete puntero;
}
}
void recorrer(struct lista *entero){
struct lista puntero;
puntero = *entero;
while (puntero != NULL){
cout << puntero->dato << " ";
puntero = puntero->sig;
}
}
int buscar(struct lista *entero, int dato){
struct lista puntero;
puntero = *entero;
while ((puntero != NULL) && (puntero->dato < dato))puntero = puntero->sig;
if (puntero->dato == dato)
return 1;
else
return 0;
}
include
include
include
include
using namespace std; struct lista{ int dato; struct lista sig; }entero; void crear(struct lista ); void insertar(struct lista , int); void eliminar(struct lista , int); void recorrer(struct lista ); int buscar(struct lista **, int);
int main(){ system("cls"); system("COLOR 3f"); int dato; crear(&entero); cout << "OPERACIONES CON LA LISTA ENLAZADA\n\n"; cout << "Para finalizar la insercion ingrese el cero\n\n"; cout << "Ingrese un numero: "; cin >> dato; while (dato != 0){ insertar(&entero, dato); cout << "Ingrese un numero: "; cin >> dato; } cout << "\n\nELEMENTOS DE LA LISTA \n\n"; recorrer(&entero); cout << "\n\n Ingrese el numero a eliminar: "; cin >> dato; eliminar(&entero, dato); recorrer(&entero); cout << "\n\nIngrese el numero a buscar: "; cin >> dato; if (buscar(&entero, dato) == 1) cout << "\nEl numero " << dato << " fue encontrado\n"; else cout << "El numero no existe " << endl; getchar(); return 0; } //funciones d elistas simplemente enlazada void crear(struct lista *entero){ entero = NULL; } void insertar(struct lista *entero, int dato){ struct lista auxiliar, puntero, anterior; auxiliar = new lista; if (!auxiliar){ cout << "Error:memoria insuficiente" << endl; exit(1); } auxiliar->dato = dato; anterior = NULL; puntero = entero; //puntero es el puntero auxiliar que recorre la lista while ((puntero != NULL) && (puntero->dato < dato)){ anterior = puntero; puntero = puntero->sig; } if (anterior == NULL){ auxiliar->sig = entero; *entero = auxiliar; } else{ anterior->sig = auxiliar; auxiliar->sig = puntero; } }
void eliminar(struct lista *entero, int dato){ struct lista puntero, anterior; puntero = entero; anterior = NULL; while ((puntero != NULL) && (puntero->dato < dato)){ anterior = puntero; puntero = puntero->sig; } if (puntero->dato != dato) cout << "El numero no existe" << endl; else { if (anterior == NULL)//1er lista entero = (entero)->sig; else anterior->sig = puntero->sig; delete puntero; } }
void recorrer(struct lista *entero){ struct lista puntero; puntero = *entero; while (puntero != NULL){ cout << puntero->dato << " "; puntero = puntero->sig; } }
int buscar(struct lista *entero, int dato){ struct lista puntero; puntero = *entero; while ((puntero != NULL) && (puntero->dato < dato))puntero = puntero->sig; if (puntero->dato == dato) return 1; else return 0; }