Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int opc, err;
- struct Persona{
- int id;
- char nombre;
- int edad;
- struct Persona *sig;
- };
- void menu(void);
- int CrearLista(struct Persona **l, int id, char* nombre, int edad);
- main()
- {
- int id=0;
- char nombre[100];
- int edad=0;
- struct Persona *l=NULL;
- struct Persona *pL=l;
- menu();
- while(opc!=7)
- {
- switch(opc)
- {
- case 1:
- system("clear");
- printf("\nCrear lista\n\n");
- printf("Inserte el ID: ");
- scanf("%d", &id);
- printf("\nInserte el NOMBRE: ");
- setbuf(stdin, NULL);
- gets(nombre);
- printf("\nInserte la EDAD: ");
- scanf("%d", &edad);
- CrearLista(&l,id,nombre,edad);
- if(!CrearLista(&l,id,nombre,edad))
- {
- printf("\nNo se pudo crear la lista\n");
- return 1;
- }
- printf("\n %d", err);
- break;
- case 2:
- system("clear");
- printf("\nModificar lista\n");
- break;
- case 3:
- system("clear");
- printf("\nBorrar Todo\n");
- break;
- case 4:
- system("clear");
- printf("\nBorrar Elemento\n");
- break;
- case 5:
- system("clear");
- printf("\nInsertar Elemento\n");
- break;
- case 6:
- system("clear");
- printf("\nMostrar todo\n");
- break;
- default:
- printf("\n Opcion no reconocida\n");
- }
- main();
- }
- }
- void menu()
- {
- system("clear");
- printf("\n Elija una opcion\n");
- printf("1.- Crear Lista\n");
- printf("2.- Modificar Lista\n");
- printf("3.- Borrar Todo\n");
- printf("4.- Borrar Elemento\n");
- printf("5.- Insertar Elemento\n");
- printf("6.- Mostrar Todo\n");
- printf("7.- Salir\n");
- printf("Opcion Elegida >_: ");
- scanf("%d", &opc);
- }
- int CrearLista(struct Persona **l, int id, char* nombre, int edad)
- {
- if(*l!=NULL){
- return 0;}
- (*l)=(struct Persona *)malloc(sizeof(struct Persona));
- if(*l==NULL){
- return 1;}
- (*l)->id=id;
- (*l)->nombre=nombre;
- (*l)->edad=edad;
- (*l)->sig=NULL;
- return 0;
- }
- /*int insertar_nodo(struct Persona **l, int id, char nombre, int edad) //-------------------> Funcion para crear un nodo...lo crea y lo inserta a lista
- {
- struct Persona *aux, *nav; // definimos el auxiliar y el navegador de la lista
- aux=(struct Persona *)malloc(sizeof(struct Persona)); //------------------------> ????????????????
- if(!aux) // esto es igual que definir la sig. condicion ... if(aux==NULL)
- {
- return 1;
- } //------------------------------------> Este if es la condicion para revisar si hay algun error
- aux->id=id; // Comienza a recorrer
- aux->nombre=nombre; // los nodos de la
- aux->edad=edad;
- aux->sig=NULL; // lista para
- nav=pL; // insertar el nuevo
- while(nav->sig!=NULL)
- {
- nav=nav->sig;
- }
- nav->sig=aux;
- return 0; //-------------------------------> Si este es el valor de retorno nos indica que tuvimos exito en la operacion
- }
- int borrar_nodo(struct Persona **pL, int id) //----------------------------------> Funcion para borrar un nodo cualquiera
- {
- struct Persona *aux, *nav;
- if(!pL)
- {
- return 1;
- }
- aux=nav=*pL;
- while(nav->id !=id && nav->sig!=NULL)
- {
- aux=nav;
- nav=nav->sig; // Para cuando encuentre el nodo a borrar
- }
- if(nav->sig==NULL&&nav->id!=id)
- {
- return 1;
- }
- if(nav==*pL) //--------------------------> Si el elemento a eliminar es el primero haz lo siguiente ...
- {
- *pL=nav->sig;
- aux->sig=nav->sig; // algunas dudas acerca de donde tiene que ir esta linea ... adentro o afuera del if? :/
- }
- free(nav); //--------------------> Liberamos la posición del navegador
- return 0;
- }
- int borrar_lista(struct Persona *pL) //------------------------------------> Funcion para borrar la lista completa
- {
- struct Persona *aux;
- if(!pL)
- {
- return 0;
- }
- while (pL)
- {
- aux=pL;
- pL=pL->sig;
- free(aux); // si no funciona la funcion free en linux solamente la cambiamos por delete que es otra funcion
- }
- return 1;
- } // Despues de utilizar esta función debemos de aterrizar a pL a su posicion original dentro de la funcion main.
- int mostrar_lista(struct Persona *pL) //------------------------------> Funcion para mostrar toda la lista
- {
- if(!pL)
- {
- return 1;
- }
- while(pL!=NULL)
- {
- printf("%d", pL->id);
- printf("%s", pL->nombre);
- printf("%d", pL->edad);
- pL=pL->sig;
- }
- return 0;
- }
- int mostrar_elemento(struct Persona *pL, int id) //----------------------> Funcion para mostrar un solo elemento de la lista
- {
- while(pL!=NULL&&pL->id!=id)
- {
- pL=pL->sig;
- if(!pL)
- {
- return 1;
- }
- printf("%d", pL->id);
- printf("%s", pL->nombre);
- printf("%d", pL->edad);
- }
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment