Sei sulla pagina 1di 3

//LISTAS CON VARIABLES LOCALES

#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>

struct lista
{
char nombre[20];
int edad;
lista *sig;
}*CAB=NULL, *AUX, *F, *P;
void insertar()
{clrscr();
AUX=new lista;
cout<<"\n NOMBRE: ";
gets(AUX->nombre);
cout<<"\n EDAD: ";
cin>>AUX->edad;
AUX->sig=NULL;
if(CAB==NULL)
CAB=AUX;
else
{
if(CAB->edad>AUX->edad)
{
AUX->sig=CAB;
CAB=AUX;
}
else
{
P=F=CAB;
while(P->edad<AUX->edad&&P!=NULL)
{
if(P==CAB)
{
P=P->sig;
}
else
{
P=P->sig;
F=F->sig;
}
AUX->sig=F->sig;
F->sig=AUX;
}
}
}
}
void visualizar()
{clrscr();
if(CAB==NULL)
{
cout<<"\n LA LISTA ESTA VACIA";
}
else
{
AUX=CAB;
while(AUX!=NULL)
{
cout<<"\n NOMBRE: "<<AUX->nombre;
cout<<"\n EDAD: "<<AUX->edad;
cout<<endl;
AUX=AUX->sig;
}
}
getch();
}
void extraer()
{
int b;
clrscr();
if(CAB==NULL)
{cout<<"\n LA LISTA ESTA VACIA";
}
else
{
cout<<"\n INGRESE LA EDAD A EXTRAER: ";
cin>>b;
if(CAB->edad==b)
{
P=CAB;
CAB=CAB->sig;
delete(P);
cout<<"REGISTRO ELIMINADO";
}
else
{
P=F=CAB;
while(P->edad!=b && P!=NULL)
{
if(P==CAB)
P=P->sig;
else
{
P=P->sig;
F=F->sig;
}
}
if(P==NULL)
cout<<"\n EDAD NO ENCONTRADA";
else
{
F->sig=P->sig;
delete(P); //elimina de memoria una posicion
cout<<"\n REGISTRO ELIMINADO";
}
}
}
}
void main()
{ char op;
do
{ clrscr();
cout<<"\n\n******** MENU ********";
cout<<"\n\n\n 1. INSERTAR";
cout<<"\n\n 2. VISUALIZAR";
cout<<"\n\n 3. EXTRAER";
cout<<"\n\n 4. SALIR";
cout<<"\n\n\n***********************";
cout<<"\n\n Ingrese su opcion: ";
op=tolower(getche());
switch (op)
{ case'1': insertar(); break;
case'2': visualizar(); break;
case'3': extraer(); break;
case'4': cout<<"\n\n SALIENDO........";break;
default: cout<<"\n OPCION NO VALIDA"; break;
}
getch();
}
while (op!='4');
}

Potrebbero piacerti anche