realizar un proyecto con listas enlazadas y con arboles no graficos
Publicado por yudy (1 intervención) el 24/05/2012 05:55:20
buenas noches
les agradeceria una ayuda
tengo un problema con este programa tengo que realizar un parqueadero que entre la placa y la hora de entrada esrealizarle un menu, lo mismo es en arboles, tengo el programa pero al crearle el menu no entiendo por que me genera error algo hice mal pero no entiendo por favor ayudenme que es para un proyecto.
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct lista
{
int hora;
char plac[5];
struct lista *sig;
};
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()
{
struct lista *dato;
int hora;
crear(&dato);
int opc=0;
do
{
clrscr();
cout<<" MENU DEL PARQUEADERO ";
cout<<"\n\n\n";
cout<<"1. insertar\n";
cout<<"2. recorrer\n";
cout<<"3. eliminar\n";
cout<<"4. buscar\n\n";
cout<<"5. salir \n\n";
cout<<"Digite una lugar: ";
cin >>opc;
switch (opc)
{
case 1: insetar();
break;
case 2: recorrer();
break;
case 3: eliminar();
break;
case 4: buscar();
break;
case 5:salir (1);
}
getch();
}
while (opc!=5);
}
if(buscar(&dato,hora,plac) == 1)
cout<<"\n la hora y placa "<<hora,plac<<" fue encontrado\n";
else
cout<<"no existe "<<endl;
getchar();
return 0;
}
void crear(struct lista **dato)
{
*dato = NULL;
}
void insertar(struct lista **dato, int hora)
{
struct lista *auxiliar, *puntero, *anterior;
auxiliar = new lista;
if(!auxiliar)
{
cout<<"error:memoria insuficiente"<<endl;
exit(1);
}
auxiliar->hora = hora;
anterior = NULL;
puntero = *dato; //puntero es el puntero auxiliar que recorre la lista
while((puntero != NULL) && (puntero->hora < hora))
{
anterior = puntero;
puntero = puntero->sig;
}
if(anterior == NULL)
{
auxiliar->sig = *dato;
*dato = auxiliar;
}
else
{
anterior->sig = auxiliar;
auxiliar->sig = puntero;
}
}
void eliminar(struct lista **dato, int hora)
{
struct lista *puntero, *anterior;
puntero = *dato;
anterior = NULL;
while((puntero != NULL) && (puntero->hora < hora))
{
anterior = puntero;
puntero = puntero->sig;
}
if(puntero->hora != hora)
cout<<"El numero no existe "<<endl;
else
{
if(anterior == NULL)
*hora = (*hora)->sig;
else
anterior->sig = puntero->sig;
delete puntero;
}
}
void recorrer(struct lista **dato)
{
struct lista *puntero;
puntero = *dato;
while(puntero != NULL)
{
cout<<puntero->hora<< " ";
puntero = puntero->sig;
}
}
int buscar(struct lista **dato, int hora)
{
struct lista *puntero;
puntero = *dato;
while((puntero != NULL) && (puntero->hora < hora)) puntero = puntero->sig;
if(puntero->hora == hora)
return 1;
else
return 0;
}
y el de arboles es para areglarlo para que quede un parqueadero con la hora y placa que salga en posorden,inorden,preorden
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream.h>
struct arbol
{
int dato;
struct arbol *izq;
struct arbol *der;
}
*raiz;
enum
{
FALSO=0, VERDADERO };
/*PROTOTIPOS*/
int vacio( struct arbol *hoja );
int eshoja( struct arbol *hoja );
struct arbol *insertar( struct arbol *raiz, struct arbol *hoja, int num );
int busqueda( struct arbol *hoja, int num );
int nodos( struct arbol *hoja );
void auxnodos( struct arbol *hoja, int *cont );
struct arbol *borrarx( struct arbol *hoja, int num );
struct arbol *podar( struct arbol *hoja );
void preorden( struct arbol *hoja );
void inorden( struct arbol *hoja );
void posorden( struct arbol *hoja );
void menu_recorridos( void );
void menu_busquedas( void );
void menu_nodos( void );
void menu_podar( void );
void inicializar( void )
{
raiz= NULL;
}
int vacio( struct arbol *hoja )
{
if( !hoja ) return VERDADERO;
return FALSO;
}
int eshoja( struct arbol *hoja )
{
if( hoja->izq==NULL && hoja->der==NULL ) return VERDADERO;
return FALSO;
}
struct arbol *insertar( struct arbol *raiz, struct arbol *hoja, int num )
{
if( !hoja )
{
hoja= (struct arbol *) malloc( sizeof (struct arbol) );
hoja->dato= num;
hoja->izq= NULL;
hoja->der= NULL;
if( !raiz ) return hoja;
else
if( num<raiz->dato ) raiz->izq= hoja;
else raiz->der= hoja;
return hoja;
} else if( num<hoja->dato ) insertar( hoja, hoja->izq, num );
else insertar( hoja, hoja->der, num );
return raiz;
}
int busqueda( struct arbol *hoja, int num )
{
while( hoja )
{ if( num==hoja->dato ) return VERDADERO;
else
{
if( num<hoja->dato ) hoja= hoja->izq;
else hoja= hoja->der;
}
}
return FALSO;
}
int nodos( struct arbol *hoja )
{
int nodos=0;
auxnodos( hoja, &nodos );
return nodos;
}
void auxnodos( struct arbol *hoja, int *cont )
{
if( !hoja ) return;
(*cont)++; auxnodos( hoja->izq, cont );
auxnodos( hoja->der, cont );
}
struct arbol *borrarx( struct arbol *hoja, int num )
{
if( hoja->dato==num )
{
struct arbol *p, *p2;
if( vacio( hoja ) )
{
free( hoja );
hoja= NULL;
return hoja;
}
else if( hoja->izq==NULL )
{
p= hoja->der;
free( hoja );
return p;
}
else if( hoja->der==NULL )
{
p= hoja->izq;
free( hoja );
return p;
}
else
{
p= hoja->der;
p2= hoja->der;
while( p->izq ) p= p->izq;
p->izq= hoja->izq; free( hoja );
return p2;
}
}
else if( num<hoja->dato ) hoja->izq= borrarx( hoja->izq, num );
else hoja->der= borrarx( hoja->der, num );
return hoja;
}
struct arbol *podar( struct arbol *hoja )
{
if( !hoja ) return hoja;
podar( hoja->izq );
podar( hoja->der );
free( hoja );
hoja= NULL;
return hoja;
}
/*Recorridos*/
void preorden( struct arbol *hoja )
{
if( !hoja ) return;
cout << hoja->dato ;
preorden( hoja->izq );
preorden( hoja->der );
}
void inorden( struct arbol *hoja )
{
if( !hoja ) return;
inorden( hoja->izq );
cout <<hoja->dato ;
inorden( hoja->der );
}
void posorden( struct arbol *hoja )
{
if( !hoja ) return;
posorden( hoja->izq );
posorden( hoja->der );
cout <<hoja->dato ;
}
/*Menus del Arbol*/
void menu_recorridos( void )
{
char _op='S';
while( _op!='4' )
{
clrscr();
cout << "1. PreOrden." ;
cout << "\n2. InOrden." ;
cout << "\n3. PosOrden." ;
cout << "\n4. Salir." ;
cout << "\n\n:: " ;
_op= getch();
switch( _op )
{
case '1':
preorden( raiz );
getch();
break;
case '2':
inorden( raiz );
getch();
break;
case '3': posorden( raiz );
getch();
break;
}
}
return;
}
void menu_busquedas( void )
{
int val;
cout << "\n\nNumero: " ;
cin>> val ;
if( busqueda( raiz, val ) ) cout << "\n\nEncontrado.." ;
else cout << "\n\nError, No se encuentra." ;
getch();
}
void menu_nodos( void )
{
cout << "\n\nEl Numero de Nodos es " << nodos( raiz ) ;
getch();
}
void menu_podar( void )
{
char _op='A';
int val;
while( _op!='3' )
{
clrscr();
cout << "1. Podar Un Nodos del Arbol." ;
cout << "\n2. Podar Todo el Arbol." ;
cout << "\n3. Salir." ;
_op= getch();
switch( _op )
{
case '1': cout << "\n\nNumero: " ;
cin>> val ;
raiz= borrarx( raiz, val );
cout << "\n\n.... Borrado ...." ;
break;
case '2': raiz= podar( raiz );
cout << "\n\nArbol Borrado por Completo." ;
getch();
break;
}
}
return;
}
int main()
{
char _op='A';
int val;
inicializar();
while( _op!='S' )
{
clrscr();
cout << "IMPLEMENTACION DE UN ARBOL BINARIO \n\n";
cout << "Insertar" ;
cout << "\nRecorridos" ;
cout << "\nBusquedas" ;
cout << "\nNodos" ;
cout << "\nPodar" ;
cout << "\nSalir" ;
cout << "\n\n Ingrese la Opcion : " ;
_op= toupper( getch() );
switch( _op )
{
case 'I': cout << "\n\nNumero: " ;
cin>> val ;
if( busqueda( raiz, val ) )
{
cout << "\n\nEste numero ya ha sido insertado." ;
getch();
break;
}
raiz= insertar( raiz, raiz, val );
break;
case 'R': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
menu_recorridos();
break;
case 'B': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
menu_busquedas();
break;
case 'A': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
case 'N': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
menu_nodos();
break;
case 'P': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
break;
}
menu_podar();
break;
}
}
cout << "\n\nPulsa para salir..." ;
getchar();
return 0;
}
agradeceria la ayuda
les agradeceria una ayuda
tengo un problema con este programa tengo que realizar un parqueadero que entre la placa y la hora de entrada esrealizarle un menu, lo mismo es en arboles, tengo el programa pero al crearle el menu no entiendo por que me genera error algo hice mal pero no entiendo por favor ayudenme que es para un proyecto.
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct lista
{
int hora;
char plac[5];
struct lista *sig;
};
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()
{
struct lista *dato;
int hora;
crear(&dato);
int opc=0;
do
{
clrscr();
cout<<" MENU DEL PARQUEADERO ";
cout<<"\n\n\n";
cout<<"1. insertar\n";
cout<<"2. recorrer\n";
cout<<"3. eliminar\n";
cout<<"4. buscar\n\n";
cout<<"5. salir \n\n";
cout<<"Digite una lugar: ";
cin >>opc;
switch (opc)
{
case 1: insetar();
break;
case 2: recorrer();
break;
case 3: eliminar();
break;
case 4: buscar();
break;
case 5:salir (1);
}
getch();
}
while (opc!=5);
}
if(buscar(&dato,hora,plac) == 1)
cout<<"\n la hora y placa "<<hora,plac<<" fue encontrado\n";
else
cout<<"no existe "<<endl;
getchar();
return 0;
}
void crear(struct lista **dato)
{
*dato = NULL;
}
void insertar(struct lista **dato, int hora)
{
struct lista *auxiliar, *puntero, *anterior;
auxiliar = new lista;
if(!auxiliar)
{
cout<<"error:memoria insuficiente"<<endl;
exit(1);
}
auxiliar->hora = hora;
anterior = NULL;
puntero = *dato; //puntero es el puntero auxiliar que recorre la lista
while((puntero != NULL) && (puntero->hora < hora))
{
anterior = puntero;
puntero = puntero->sig;
}
if(anterior == NULL)
{
auxiliar->sig = *dato;
*dato = auxiliar;
}
else
{
anterior->sig = auxiliar;
auxiliar->sig = puntero;
}
}
void eliminar(struct lista **dato, int hora)
{
struct lista *puntero, *anterior;
puntero = *dato;
anterior = NULL;
while((puntero != NULL) && (puntero->hora < hora))
{
anterior = puntero;
puntero = puntero->sig;
}
if(puntero->hora != hora)
cout<<"El numero no existe "<<endl;
else
{
if(anterior == NULL)
*hora = (*hora)->sig;
else
anterior->sig = puntero->sig;
delete puntero;
}
}
void recorrer(struct lista **dato)
{
struct lista *puntero;
puntero = *dato;
while(puntero != NULL)
{
cout<<puntero->hora<< " ";
puntero = puntero->sig;
}
}
int buscar(struct lista **dato, int hora)
{
struct lista *puntero;
puntero = *dato;
while((puntero != NULL) && (puntero->hora < hora)) puntero = puntero->sig;
if(puntero->hora == hora)
return 1;
else
return 0;
}
y el de arboles es para areglarlo para que quede un parqueadero con la hora y placa que salga en posorden,inorden,preorden
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream.h>
struct arbol
{
int dato;
struct arbol *izq;
struct arbol *der;
}
*raiz;
enum
{
FALSO=0, VERDADERO };
/*PROTOTIPOS*/
int vacio( struct arbol *hoja );
int eshoja( struct arbol *hoja );
struct arbol *insertar( struct arbol *raiz, struct arbol *hoja, int num );
int busqueda( struct arbol *hoja, int num );
int nodos( struct arbol *hoja );
void auxnodos( struct arbol *hoja, int *cont );
struct arbol *borrarx( struct arbol *hoja, int num );
struct arbol *podar( struct arbol *hoja );
void preorden( struct arbol *hoja );
void inorden( struct arbol *hoja );
void posorden( struct arbol *hoja );
void menu_recorridos( void );
void menu_busquedas( void );
void menu_nodos( void );
void menu_podar( void );
void inicializar( void )
{
raiz= NULL;
}
int vacio( struct arbol *hoja )
{
if( !hoja ) return VERDADERO;
return FALSO;
}
int eshoja( struct arbol *hoja )
{
if( hoja->izq==NULL && hoja->der==NULL ) return VERDADERO;
return FALSO;
}
struct arbol *insertar( struct arbol *raiz, struct arbol *hoja, int num )
{
if( !hoja )
{
hoja= (struct arbol *) malloc( sizeof (struct arbol) );
hoja->dato= num;
hoja->izq= NULL;
hoja->der= NULL;
if( !raiz ) return hoja;
else
if( num<raiz->dato ) raiz->izq= hoja;
else raiz->der= hoja;
return hoja;
} else if( num<hoja->dato ) insertar( hoja, hoja->izq, num );
else insertar( hoja, hoja->der, num );
return raiz;
}
int busqueda( struct arbol *hoja, int num )
{
while( hoja )
{ if( num==hoja->dato ) return VERDADERO;
else
{
if( num<hoja->dato ) hoja= hoja->izq;
else hoja= hoja->der;
}
}
return FALSO;
}
int nodos( struct arbol *hoja )
{
int nodos=0;
auxnodos( hoja, &nodos );
return nodos;
}
void auxnodos( struct arbol *hoja, int *cont )
{
if( !hoja ) return;
(*cont)++; auxnodos( hoja->izq, cont );
auxnodos( hoja->der, cont );
}
struct arbol *borrarx( struct arbol *hoja, int num )
{
if( hoja->dato==num )
{
struct arbol *p, *p2;
if( vacio( hoja ) )
{
free( hoja );
hoja= NULL;
return hoja;
}
else if( hoja->izq==NULL )
{
p= hoja->der;
free( hoja );
return p;
}
else if( hoja->der==NULL )
{
p= hoja->izq;
free( hoja );
return p;
}
else
{
p= hoja->der;
p2= hoja->der;
while( p->izq ) p= p->izq;
p->izq= hoja->izq; free( hoja );
return p2;
}
}
else if( num<hoja->dato ) hoja->izq= borrarx( hoja->izq, num );
else hoja->der= borrarx( hoja->der, num );
return hoja;
}
struct arbol *podar( struct arbol *hoja )
{
if( !hoja ) return hoja;
podar( hoja->izq );
podar( hoja->der );
free( hoja );
hoja= NULL;
return hoja;
}
/*Recorridos*/
void preorden( struct arbol *hoja )
{
if( !hoja ) return;
cout << hoja->dato ;
preorden( hoja->izq );
preorden( hoja->der );
}
void inorden( struct arbol *hoja )
{
if( !hoja ) return;
inorden( hoja->izq );
cout <<hoja->dato ;
inorden( hoja->der );
}
void posorden( struct arbol *hoja )
{
if( !hoja ) return;
posorden( hoja->izq );
posorden( hoja->der );
cout <<hoja->dato ;
}
/*Menus del Arbol*/
void menu_recorridos( void )
{
char _op='S';
while( _op!='4' )
{
clrscr();
cout << "1. PreOrden." ;
cout << "\n2. InOrden." ;
cout << "\n3. PosOrden." ;
cout << "\n4. Salir." ;
cout << "\n\n:: " ;
_op= getch();
switch( _op )
{
case '1':
preorden( raiz );
getch();
break;
case '2':
inorden( raiz );
getch();
break;
case '3': posorden( raiz );
getch();
break;
}
}
return;
}
void menu_busquedas( void )
{
int val;
cout << "\n\nNumero: " ;
cin>> val ;
if( busqueda( raiz, val ) ) cout << "\n\nEncontrado.." ;
else cout << "\n\nError, No se encuentra." ;
getch();
}
void menu_nodos( void )
{
cout << "\n\nEl Numero de Nodos es " << nodos( raiz ) ;
getch();
}
void menu_podar( void )
{
char _op='A';
int val;
while( _op!='3' )
{
clrscr();
cout << "1. Podar Un Nodos del Arbol." ;
cout << "\n2. Podar Todo el Arbol." ;
cout << "\n3. Salir." ;
_op= getch();
switch( _op )
{
case '1': cout << "\n\nNumero: " ;
cin>> val ;
raiz= borrarx( raiz, val );
cout << "\n\n.... Borrado ...." ;
break;
case '2': raiz= podar( raiz );
cout << "\n\nArbol Borrado por Completo." ;
getch();
break;
}
}
return;
}
int main()
{
char _op='A';
int val;
inicializar();
while( _op!='S' )
{
clrscr();
cout << "IMPLEMENTACION DE UN ARBOL BINARIO \n\n";
cout << "Insertar" ;
cout << "\nRecorridos" ;
cout << "\nBusquedas" ;
cout << "\nNodos" ;
cout << "\nPodar" ;
cout << "\nSalir" ;
cout << "\n\n Ingrese la Opcion : " ;
_op= toupper( getch() );
switch( _op )
{
case 'I': cout << "\n\nNumero: " ;
cin>> val ;
if( busqueda( raiz, val ) )
{
cout << "\n\nEste numero ya ha sido insertado." ;
getch();
break;
}
raiz= insertar( raiz, raiz, val );
break;
case 'R': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
menu_recorridos();
break;
case 'B': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
menu_busquedas();
break;
case 'A': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
case 'N': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
getch();
break;
}
menu_nodos();
break;
case 'P': if( vacio( raiz ) )
{
cout << "\n\nEl Arbol Aun esta Vacio." ;
break;
}
menu_podar();
break;
}
}
cout << "\n\nPulsa para salir..." ;
getchar();
return 0;
}
agradeceria la ayuda
Valora esta pregunta


0