Código
#include <iostream> using namespace std; #define VOLVER_MENU cout<<"\n\nPRESIONE ENTER PARA VOLVER AL MENU"; setbuf(stdin, NULL); cin.get(); struct nodo { string nombre; int id; int edad; nodo *siguiente; }; nodo *pIni = NULL; nodo *pFin; nodo *p; char op = 'n'; int cont = 0; int i = 0; int enc =0; void cargar(); void mostrarlista(); void cargarnodo(nodo *x); void consultar(); void eliminar(); void borrarNodo(); int main() { do{ system("cls"); cout<<"Insertar elemento --- (I) (Personas ingresadas) "<<cont<<endl; cout<<"Mostrar elementos --- (M)\n"; cout<<"Consultar elemento -- (C)\n"; cout<<"Eliminar elementos ---(E)\n"; cout<<"Eliminar por id ------(N)\n"; cout<<"Salir --- (S)\n"; cin.get(op); if(toupper(op) == 'I') { cargar(); } else if(toupper(op) == 'M') mostrarlista(); else if(toupper(op) == 'C') consultar(); else if(toupper(op) == 'E') eliminar(); else if(toupper(op) == 'N') borrarNodo(); }while(toupper(op) != 'S'); p = pIni; if(p != NULL) { nodo *aux; while(p!=NULL) { aux = new nodo; aux = pIni; pIni = pIni->siguiente; free(aux); p = p->siguiente; } } return 0; } void mostrarlista() { system("cls"); if(pIni != NULL) { p = pIni; i = 0; while (p->siguiente != NULL) { i++; cout<<i<<"Direccion : "<<int(p)<<endl; cout<<i<<".1) Nombre: "<<p->nombre<<endl; cout<<i<<".2) Id: "<<p->id<<endl; cout<<i<<".3) Edad: "<<p->edad<<endl<<endl; p = p->siguiente; } }else cout<<"NO HAY ELEMENTOS PARA MOSTAR"; VOLVER_MENU } void cargar() { if(pIni == NULL) { pIni = new nodo; pFin = new nodo; pIni->siguiente = pFin; pFin->siguiente = NULL; cargarnodo(pIni); } else { cargarnodo(pFin); pFin->siguiente = new nodo; pFin = pFin->siguiente; pFin->siguiente = NULL; } cont++; p = pIni; } void cargarnodo(nodo *x) { system("cls"); cout<<"introduzca el nombre: "; cin>>x->nombre; cout<<"introduzca el id: "; cin>>x->id; cout<<"introduzca la edad: "; cin>>x->edad; } void consultar() { system("cls"); p = pIni; if(p != NULL) { nodo *c = NULL; c = new nodo; cout<<"INGRESE ID A CONSULTAR\n"; cin>>c->id; enc = 0; while(p != NULL) { if(c->id == p->id) { enc = 1; cout<<"\n\nNombre: "<<p->nombre<<endl; cout<<"id: "<<p->id<<endl; cout<<"Edad: "<<p->edad<<endl; } p = p->siguiente; } if(enc == 0) { cout<<"\n\nELEMENTO NO ENCONTRADO"; } free(c); }else cout<<"NO HAY ELEMENTOS PARA CONSULTAR"; VOLVER_MENU } void eliminar() { system("cls"); p = pIni; if(p!=NULL) { nodo *aux; aux = new nodo; while(p->siguiente != NULL) { aux = pIni; pIni = pIni->siguiente; free(aux); p = p->siguiente; } cout<<"ELEMENTOS ELIMINADOS"; cont = 0; }else cout<<"NO HAY ELEMENTOS PARA ELIMINAR"; VOLVER_MENU } void borrarNodo() { system("cls"); p = pIni; if(p) { nodo *aux = NULL, *ant = NULL; ant = new nodo; aux = new nodo; cout<<"INTRODUZCA EL ID A ELIMINAR\n"; cin>>aux->id; while(aux->id != p->id) { ant = p; p = p->siguiente; } if(aux->id == p->id) { ant = p; p = p->siguiente; free(ant); free(aux); cout<<"ELEMENTO ELIMINADO"; cont--; } }else cout<<"ELEMENTO NO ENCONTRADO"; VOLVER_MENU }