Glaas2

Practica2

Feb 27th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int opc, err;
  5.  
  6. struct Persona{
  7. int id;
  8. char nombre;
  9. int edad;
  10. struct Persona *sig;
  11. };
  12.  
  13. void menu(void);
  14. int CrearLista(struct Persona **l, int id, char* nombre, int edad);
  15.  
  16. main()
  17. {
  18. int id=0;
  19. char nombre[100];
  20. int edad=0;
  21. struct Persona *l=NULL;
  22. struct Persona *pL=l;
  23.  
  24. menu();
  25. while(opc!=7)
  26. {
  27.  switch(opc)
  28.     {
  29.     case 1:
  30.         system("clear");
  31.         printf("\nCrear lista\n\n");
  32.         printf("Inserte el ID: ");
  33.         scanf("%d", &id);
  34.         printf("\nInserte el NOMBRE: ");
  35.         setbuf(stdin, NULL);
  36.         gets(nombre);
  37.         printf("\nInserte la EDAD: ");
  38.         scanf("%d", &edad);
  39.         CrearLista(&l,id,nombre,edad);
  40.         if(!CrearLista(&l,id,nombre,edad))
  41.          {
  42.           printf("\nNo se pudo crear la lista\n");
  43.           return 1;
  44.          }
  45.         printf("\n %d", err);
  46.         break;
  47.     case 2:
  48.         system("clear");
  49.         printf("\nModificar lista\n");
  50.         break;
  51.     case 3:
  52.         system("clear");
  53.         printf("\nBorrar Todo\n");
  54.         break;
  55.     case 4:
  56.         system("clear");
  57.         printf("\nBorrar Elemento\n");
  58.         break;
  59.     case 5:
  60.         system("clear");
  61.         printf("\nInsertar Elemento\n");
  62.         break;
  63.     case 6:
  64.         system("clear");
  65.         printf("\nMostrar todo\n");
  66.         break;
  67.     default:
  68.             printf("\n Opcion no reconocida\n");
  69.     }
  70.   main();
  71.    
  72. }
  73.  
  74. }
  75.  
  76. void menu()
  77. {
  78. system("clear");
  79. printf("\n Elija una opcion\n");
  80. printf("1.- Crear Lista\n");
  81. printf("2.- Modificar Lista\n");
  82. printf("3.- Borrar Todo\n");
  83. printf("4.- Borrar Elemento\n");
  84. printf("5.- Insertar Elemento\n");
  85. printf("6.- Mostrar Todo\n");
  86. printf("7.- Salir\n");
  87. printf("Opcion Elegida >_: ");
  88. scanf("%d", &opc);
  89. }
  90.  
  91. int CrearLista(struct Persona **l, int id, char* nombre, int edad)
  92. {
  93. if(*l!=NULL){
  94.   return 0;}
  95. (*l)=(struct Persona *)malloc(sizeof(struct Persona));
  96. if(*l==NULL){
  97.   return 1;}
  98. (*l)->id=id;
  99. (*l)->nombre=nombre;
  100. (*l)->edad=edad;
  101. (*l)->sig=NULL;
  102. return 0;
  103.  
  104. }
  105.  
  106. /*int insertar_nodo(struct Persona **l, int id, char nombre, int edad) //-------------------> Funcion para crear un nodo...lo crea y lo inserta a lista
  107. {
  108.     struct Persona *aux, *nav; // definimos el auxiliar y el navegador de la lista
  109.     aux=(struct Persona *)malloc(sizeof(struct Persona)); //------------------------> ????????????????
  110.     if(!aux)   // esto es igual que definir la sig. condicion ... if(aux==NULL)
  111.     {
  112.         return 1;
  113.     } //------------------------------------> Este if es la condicion para revisar si hay algun error
  114.  
  115.     aux->id=id;                 // Comienza a recorrer
  116.     aux->nombre=nombre;             // los nodos de la
  117.     aux->edad=edad;
  118.     aux->sig=NULL;              // lista para
  119.     nav=pL;                     // insertar el nuevo
  120.  
  121.     while(nav->sig!=NULL)
  122.     {
  123.         nav=nav->sig;
  124.     }
  125.  
  126.     nav->sig=aux;
  127.     return 0; //-------------------------------> Si este es el valor de retorno nos indica que tuvimos exito en la operacion
  128. }
  129.  
  130. int borrar_nodo(struct Persona **pL, int id) //----------------------------------> Funcion para borrar un nodo cualquiera
  131. {
  132.     struct Persona *aux, *nav;
  133.     if(!pL)
  134.     {
  135.         return 1;
  136.     }
  137.  
  138.     aux=nav=*pL;
  139.     while(nav->id !=id && nav->sig!=NULL)
  140.     {
  141.         aux=nav;
  142.         nav=nav->sig; // Para cuando encuentre el nodo a borrar
  143.     }
  144.     if(nav->sig==NULL&&nav->id!=id)
  145.     {
  146.         return 1;
  147.     }
  148.     if(nav==*pL) //--------------------------> Si el elemento a eliminar es el primero haz lo siguiente ...
  149.     {
  150.         *pL=nav->sig;
  151.         aux->sig=nav->sig; // algunas dudas acerca de donde tiene que ir esta linea ... adentro o afuera del if? :/
  152.     }
  153.     free(nav); //--------------------> Liberamos la posición del navegador
  154.     return 0;
  155. }
  156.  
  157. int borrar_lista(struct Persona *pL) //------------------------------------> Funcion para borrar la lista completa
  158. {
  159.     struct Persona *aux;
  160.     if(!pL)
  161.     {
  162.         return 0;
  163.     }
  164.     while (pL)
  165.     {
  166.         aux=pL;
  167.         pL=pL->sig;
  168.         free(aux); // si no funciona la funcion free en linux solamente la cambiamos por delete que es otra funcion
  169.     }
  170.     return 1;
  171. } // Despues de utilizar esta función debemos de aterrizar a pL a su posicion original dentro de la funcion main.
  172.  
  173. int mostrar_lista(struct Persona *pL) //------------------------------> Funcion para mostrar toda la lista
  174. {
  175.     if(!pL)
  176.     {
  177.         return 1;
  178.     }
  179.     while(pL!=NULL)
  180.     {
  181.         printf("%d", pL->id);
  182.         printf("%s", pL->nombre);
  183.         printf("%d", pL->edad);
  184.         pL=pL->sig;
  185.     }
  186.     return 0;
  187. }
  188.  
  189. int mostrar_elemento(struct Persona *pL, int id) //----------------------> Funcion para mostrar un solo elemento de la lista
  190. {
  191.     while(pL!=NULL&&pL->id!=id)
  192.     {
  193.         pL=pL->sig;
  194.         if(!pL)
  195.         {
  196.             return 1;
  197.         }
  198.         printf("%d", pL->id);
  199.         printf("%s", pL->nombre);
  200.         printf("%d", pL->edad);
  201.     }
  202.     return 0;
  203. }
  204.  
  205.  
  206.  
  207. */
Advertisement
Add Comment
Please, Sign In to add comment