Advertisement
Alexvans

lista enlazada

Mar 14th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define T 20
  5.  
  6. struct listNode {
  7.     char nombre[T];
  8.     int edad;
  9.     int matricula;
  10.     struct listNode *nextP;
  11. };
  12.  
  13. typedef struct listNode LISTNODE;
  14. typedef LISTNODE *LISTNODEP;
  15. void inserta(LISTNODEP *, char[], int, int);
  16. int elimina(LISTNODEP *, char[], int, int);
  17. int esVacia(LISTNODEP);
  18. void imprimeLista(LISTNODEP);
  19. void menu(void);
  20.  
  21. int esVacia(LISTNODEP sP) {
  22.     return (sP==NULL);
  23. }
  24.  
  25. void imprimeLista(LISTNODEP actualP) {
  26.     if(actualP == NULL) {
  27.         printf("\n Lista vacia");
  28.     }
  29.     else {
  30.         printf("\n La lista es: \n");
  31.  
  32.         while(actualP != NULL) {
  33.             printf("\nNombre:    %s", actualP->nombre);
  34.             printf("\nEdad:      %d", actualP->edad);
  35.             printf("\nMatricula: %d", actualP->matricula);
  36.             actualP = actualP->nextP;
  37.         }
  38.     }
  39. }
  40.  
  41. void menu(void) {
  42.     printf("\nMENU: \n");
  43.     printf("\n1. Inserta un elemento en la lista \n");
  44.     printf("\n2. Borra un elemento de la lista \n");
  45.     printf("\n3. Terminar \n");
  46. }
  47.  
  48. void inserta(LISTNODEP *sP, char value[], int value2, int value3) {
  49.     LISTNODEP nuevoP, anteriorP, actualP;
  50.     nuevoP = (LISTNODEP) malloc (sizeof(LISTNODE));
  51.     if(nuevoP != NULL) {
  52.         //nuevoP->nombre = value;
  53.         strcpy(nuevoP->nombre, value);
  54.         nuevoP->edad = value2;
  55.         nuevoP->matricula = value3;
  56.         nuevoP->nextP = NULL;
  57.         anteriorP = NULL;
  58.         actualP = *sP;
  59.  
  60.         while(actualP != NULL && value>actualP->nombre && value2>actualP->edad && value3>actualP->matricula) {
  61.             anteriorP = actualP;
  62.             actualP = actualP->nextP;
  63.         }
  64.         if(anteriorP == NULL) {
  65.             nuevoP->nextP = *sP;
  66.             *sP = nuevoP;
  67.         }
  68.         else {
  69.             anteriorP->nextP=nuevoP;
  70.             nuevoP->nextP = actualP;
  71.         }
  72.     }
  73.     else {
  74.         printf("\n %s %d %d no se inserto. No hay memoria", value, value2, value3);
  75.     }
  76. }
  77.  
  78. int elimina(LISTNODEP *sP, char value[], int value2, int value3) {
  79.     LISTNODEP anteriorP, actualP, tempP;
  80.  
  81.     if(value == (*sP)->nombre && value2 == (*sP)->edad && value3 == (*sP)->matricula) {
  82.         tempP = *sP;
  83.         *sP = (*sP)->nextP;
  84.         free(tempP);
  85.         return value, value2,value3;
  86.     }
  87.     else {
  88.         anteriorP = *sP;
  89.         actualP = (*sP)->nextP;
  90.  
  91.         while(actualP != NULL && actualP->nombre != value && actualP->edad != value2 && actualP->matricula != value3) {
  92.             anteriorP = actualP;
  93.             actualP = actualP->nextP;
  94.         }
  95.         if(actualP != NULL) {
  96.             tempP = actualP;
  97.             anteriorP->nextP = actualP->nextP;
  98.             free(tempP);
  99.             return value, value2,value3;
  100.         }
  101.     }
  102. }
  103.  
  104. int main() {
  105.     LISTNODEP startP = NULL;
  106.     int op;
  107.     char item;
  108.     int item2, item3;
  109.  
  110.     menu();
  111.     printf("\nEscoge una opcion: \n");
  112.     scanf(" %d", &op);
  113.  
  114.     while(op!=3) {
  115.         switch (op) {
  116.             case 1:
  117.                 printf("\nIngresa nombre: \n");
  118.                 scanf(" %s", &item);
  119.                 printf("\nIngresa edad: \n");
  120.                 scanf(" %d", &item2);
  121.                 printf("\nIngresa matricula: \n");
  122.                 scanf(" %d", &item3);
  123.                 inserta(&startP, &item, item2, item3);
  124.                 imprimeLista(startP);       /*Extra*/
  125.                 break;
  126.             case 2:
  127.                 if(!esVacia(startP)) {
  128.                     printf("\nTeclea el nombre a borrar\n");
  129.                     scanf(" %s", &item);
  130.                     printf("\nTeclea el edad a borrar\n");
  131.                     scanf(" %d", &item2);
  132.                     printf("\nTeclea el matricula a borrar\n");
  133.                     scanf(" %d", &item3);
  134.                     if(elimina(&startP, &item, item2,item3)) {
  135.                         printf(" %s %d %d eliminado \n", item, item2, item3);
  136.                         imprimeLista(startP);
  137.                     }
  138.                     else {
  139.                         printf(" %s %d %d no se encontro \n", item, item2, item3);
  140.                     }
  141.                 }
  142.                 else {
  143.                     printf("\n Lista vacia...");
  144.                 }
  145.                 break;
  146.             default:
  147.                 printf("\n Eleccion no valida...");
  148.                 menu();
  149.                 break;
  150.         }
  151.         printf("\n Escoge una opcion: ");
  152.         scanf(" %d", &op);
  153.     }
  154.     printf("Fin de programa...");
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement