elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / Mi funcion de borrar falla en: 10 Octubre 2016, 02:13 am
Me falla la funcion de borrar en mi lista doblemente ligada circular, que puede ser?

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum opcion {EXIT,INSERT,FIND,DELETE,SHOW,CURR,NEXT,PREV,FINDPREV,FINDNEXT};
  5.  
  6. struct nodo {
  7.    char nombre[40];
  8.    struct nodo *next;
  9.    struct nodo *prev;
  10. };
  11.  
  12. int numNodos;
  13. int pos;
  14. struct nodo *head, *tail;
  15.  
  16.  
  17. struct nodo *crearNodo () {
  18.    struct nodo *x;
  19.  
  20.    x= (struct nodo *) malloc (sizeof (struct nodo));
  21.    printf ("Nombre: ");
  22.    gets (x->nombre);
  23.    x->next=NULL;
  24.    x->prev=NULL;
  25.    numNodos++;  
  26.    return x;
  27. }
  28. struct nodo *buscar_ant (struct nodo *act) {
  29.    char *k= act->nombre;
  30.  
  31.    if (act == NULL) return NULL;
  32.  
  33.    act= act->prev;
  34.    while (act) {
  35.        if (strcmp (act->nombre,k) == 0)
  36.            break;
  37.        act= act->prev;
  38.    }
  39.    return act;
  40. }
  41. struct nodo *buscar_sig (struct nodo *act) {
  42.    char *k= act->nombre;
  43.  
  44.    if (act == NULL) return NULL;
  45.  
  46.    act= act->next;
  47.    while (act) {
  48.        if (strcmp (act->nombre,k) == 0)
  49.            break;
  50.        act= act->next;
  51.    }
  52.    return act;
  53. }
  54.  
  55. struct nodo *buscar (struct nodo *lista, char *k) {
  56.   system("cls");
  57.     struct nodo *actual;
  58.     char aux [40];
  59.     int p = 1;
  60.     int encontrado = 0;
  61.  
  62.        actual = lista;
  63.        system("cls");
  64.        printf("\n Indique nombre a buscar    ");
  65.        fflush(stdin);
  66.        gets(aux);
  67.        system("cls");
  68.        while (p <= numNodos)
  69.           {  
  70.            if(strcmp(aux, actual->nombre)==0)
  71.                    {
  72.                      printf("\n %s  EXISTE", aux );
  73.                      encontrado = 1;
  74.                    }
  75.                      p++;
  76.                      actual = actual->next;
  77.           }                                                                                      
  78.   if (encontrado==0)  
  79.      printf("\n %s NO EXISTE", aux );
  80.  
  81.   printf("\n\n\n\n\n\n\n");                                                                      
  82.   system("pause");      
  83. }
  84.  
  85.  
  86. // operacion eliminar: elimina el primer nodo que coincida con la llave k
  87. // recibe referencia del apuntador al inicio de la lista y llave a eliminar
  88. // devuelve el nodo eliminado o null si no se encuentra el nodo
  89.  
  90. struct nodo *borrar (struct nodo *lista, char *k) {
  91.   system("cls");
  92.    struct nodo  *aux;
  93.    struct nodo  *actual;
  94.    int nodo_eliminar;
  95.    int x;
  96.  
  97.    printf("\n Indique la posición del nombre  a eliminar    ");
  98.    scanf(" %d", &nodo_eliminar);
  99.    if(nodo_eliminar > numNodos || nodo_eliminar < 1)
  100.       printf("ELEMENTO INEXISTENTE");
  101.  
  102.    if(nodo_eliminar == 1) {
  103.       aux = lista;
  104.       lista = lista->next;
  105.       if(lista == NULL)
  106.          tail = NULL;
  107.  
  108.    else
  109.      lista->prev == tail;
  110.       }
  111.       else if(nodo_eliminar == numNodos)
  112.       {
  113.          aux = tail;
  114.          tail->prev->next = tail;
  115.          tail = tail->prev;
  116.       }else {                                                                                                                                                                                                                    
  117.             actual = tail;
  118.             for(x=1; x<nodo_eliminar;++x)
  119.               actual = actual->next;
  120.             aux = actual;
  121.             actual->prev->next = actual->next;
  122.             actual->next->prev = actual->prev;
  123.   }
  124.   free(aux);
  125.   numNodos--;  
  126. }
  127.  
  128. void listar (struct nodo *lista) {
  129.    system("cls");
  130.  
  131.     struct nodo *actual;
  132.     pos=1;
  133.     actual=lista;
  134.     while(pos<=numNodos)
  135.       {
  136.          printf("<-%s->", lista->nombre);
  137.          lista= lista->next;
  138.          pos++;
  139.       }
  140.       if(numNodos==0)
  141.           printf("\n LISTA VACIA");
  142.       printf("\n\n\n\n\n\n\n");
  143.       system("pause");    
  144. }
  145.  
  146. struct nodo *insertar (struct nodo *head, struct nodo *x) {
  147.    if (head != NULL) {
  148.        head->prev= x;       // El antecesor al primer nodo de la lista apunta al nuevo nodo
  149.        x->next= head;       // El sucesor del nuevo nodo apunta al primer nodo de la lista
  150.    }
  151.    head= x;                // El inicio de la lista apunta al nuevo nodo
  152.  
  153.    return head;
  154. }
  155. struct nodo *curr (struct nodo *actual) {
  156.    if (actual) {
  157.        printf ("%s\n",actual->nombre);
  158.        return actual;
  159.    }
  160.    printf ("No hay nodo actual\n");
  161.  
  162.    return actual;
  163. }
  164. struct nodo *prev (struct nodo *actual) {
  165.    if (actual && actual->prev) {
  166.        printf ("%s\n",actual->prev->nombre);
  167.        return actual->prev;
  168.    }
  169.    printf ("No hay nodo anterior\n");
  170.  
  171.    return actual;
  172. }
  173.  
  174. struct nodo *next (struct nodo *actual) {
  175.    if (actual && actual->next) {
  176.        printf ("%s\n",actual->next->nombre);
  177.        return actual->next;
  178.    }
  179.    printf ("No hay nodo siguiente\n");
  180.  
  181.    return actual;
  182. }
  183.  
  184. int main ()
  185. {
  186.    char nombre[40];
  187.    enum opcion op= EXIT;
  188.    struct nodo *x,*act=NULL;
  189.    struct nodo *head= NULL;
  190.    struct nodo *tail= NULL;
  191.    struct nodo *lista;
  192.     char aux [40];
  193.     int p = 1;
  194.     int encontrado = 0;
  195.  
  196.    while (1) {
  197.        system ("cls");
  198.        printf ("1. Insertar\n");
  199.        printf ("2. Buscar\n");
  200.        printf ("3. Borrar\n");
  201.        printf ("4. Listar\n");
  202.        printf ("5. Actual\n");
  203.        printf ("6. Siguiente\n");
  204.        printf ("7. Anterior\n");
  205.        printf ("8. Buscar anterior\n");
  206.        printf ("9. Buscar siguiente\n");
  207.        printf ("0. Salir\n");
  208.        printf ("\nOpcion: ");
  209.        scanf ("%d", &op);
  210.        fflush (stdin);
  211.  
  212.        switch (op) {
  213.            case INSERT:
  214.                x= insertar (head,crearNodo ());
  215.                if (head == NULL)   // No hay nodos
  216.                tail= x;
  217.                head= x;        // Head apunta al nuevo nodo
  218.                tail->next= head;    // Siguiente del ultimo nodo apunta al primer nodo
  219.                head->prev= tail;    // Anterior del primer nodo apunta al ultimo nodo
  220.                act= head;      // Nodo actual apunta a primer nodo
  221.                break;
  222.            case FIND:
  223.        x= buscar (head,nombre);
  224.                break;
  225.            case DELETE:
  226.                x= borrar (head,nombre);
  227.                break;              
  228.            case SHOW:
  229.                listar (head);
  230.                system ("pause");
  231.                break;
  232.            case CURR:
  233.                curr (act);
  234.                system ("pause");
  235.                break;
  236.            case NEXT:
  237.                act= next (act);
  238.                system ("pause");
  239.                break;
  240.            case PREV:
  241.                act= prev (act);
  242.                system ("pause");
  243.                break;
  244.            case FINDNEXT:
  245.                x= buscar_sig (act);
  246.                if (x != NULL) {
  247.                    printf ("Se encuentro: %s\n", x->nombre);
  248.                    act= x;
  249.                }
  250.                else
  251.                    printf ("No se encuentra: %s\n", x->nombre);
  252.                system ("pause");
  253.                break;
  254.            case FINDPREV:
  255.                x= buscar_ant (act);
  256.                if (x != NULL) {
  257.                    printf ("Se encuentro: %s\n", x->nombre);
  258.                    act= x;
  259.                }
  260.                else
  261.                    printf ("No se encuentra: %s\n", x->nombre);
  262.                system ("pause");
  263.                break;
  264.            case EXIT:
  265.                return;
  266.            default:
  267.                printf ("Opcion invalida\n");
  268.        }
  269.    }      
  270.  
  271. }


Alguna idea?
2  Programación / Programación C/C++ / [Ayuda] Como puede hacer de mi programa una Lista doblemente ligada "circular" C en: 9 Octubre 2016, 09:31 am
He desarrollado el código de mi lista doblemente ligada pero ahora quisiera implementarla de forma circular. No comprendo exactamente que diferencias tienen con la circular ya que yo lo veo de la misma manera. PD: No me lo he robado >:(

Esta algo largo pero necesito implementarlo de varias maneras para que sea completo se que le falta pero no quiero complicarme la vida.

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum opcion {EXIT,INSERT,FIND,DELETE,SHOW,CURR,NEXT,PREV,FINDPREV,FINDNEXT};
  5.  
  6. struct nodo {
  7.    char nombre[40];
  8.    struct nodo *next;
  9.    struct nodo *prev;
  10. };
  11.  
  12. struct nodo *crearNodo () {
  13.    struct nodo *x;
  14.    x= (struct nodo *) malloc (sizeof (struct nodo));
  15.    printf ("Nombre: ");
  16.    gets (x->nombre);
  17.    x->next=NULL;
  18.    x->prev=NULL;
  19.    return x;
  20. }
  21.  
  22. struct nodo *buscar (struct nodo *lista, char *k) {
  23.    if (lista == NULL) return NULL;
  24.    while (lista) {
  25.        if (strcmp (lista->nombre,k) == 0)
  26.            break;
  27.        lista= lista->next;
  28.    }
  29.    return lista;
  30. }
  31.  
  32. struct nodo *buscar_sig (struct nodo *act) {
  33. char *k = act->nombre;
  34.    if (act == NULL) return NULL;
  35.  
  36.     act= act->next;
  37.     while (act) {
  38.        if (strcmp (act->nombre,k) == 0)
  39.         break;    
  40.         act= act->prev;
  41.        }
  42.        return act;
  43.     }
  44.  
  45.  
  46. struct nodo *buscar_ant (struct nodo *act) {
  47. char *k = act->nombre;
  48.    if (act == NULL) return NULL;
  49.  
  50.     act= act->prev;
  51.     while (act) {
  52.        if (strcmp (act->nombre,k) == 0)
  53.         break;    
  54.         act= act->prev;
  55.        }
  56.        return act;
  57.     }
  58. // operacion eliminar: elimina el primer nodo que coincida con la llave k
  59. // recibe referencia del apuntador al inicio de la lista y llave a eliminar
  60. // devuelve el nodo eliminado o null si no se encuentra el nodo
  61.  
  62. struct nodo *borrar (struct nodo **lista, char *k) {
  63.    struct nodo *ant, *act;
  64.  
  65.    if (*lista == NULL) return NULL;
  66.  
  67.    act= *lista;
  68.    ant= *lista;
  69.    while (act != NULL) {
  70.        if (strcmp (act->nombre,k) == 0) // nodo encontrado
  71.            break;
  72.        ant= act;
  73.        act= act->next;                      // avance al sig. nodo
  74.    }
  75.    if (act == NULL) return NULL;
  76.  
  77.    // Si se encontro el nodo
  78.    if (act == *lista)  {               // Si es el primer nodo
  79.        *lista= act->next;               // Se mueve el aptdor. al inicio de la lista al siguiente nodo
  80.        if (act->next)
  81.            act->next->prev= NULL;
  82.    }
  83.    else {
  84.        ant->next= act->next;         // Se liga el nodo antecesor con el nodo sucesor del nodo a eliminar
  85.        act->prev= ant;
  86.    }
  87.    return act;
  88. }
  89.  
  90.  
  91. void listar (struct nodo *lista) {
  92.    if (lista == NULL) {
  93.        printf ("Lista vacia\n");
  94.        return;
  95.    }
  96.    do {
  97.        if (lista->prev && lista->next)
  98.            printf ("<-%s->",lista->nombre);
  99.        else if (lista->prev == NULL && lista->next == NULL)
  100.            printf ("%s",lista->nombre);
  101.        else if (!lista->next)
  102.            printf ("<-%s",lista->nombre);
  103.        else if (!lista->prev)
  104.            printf ("%s->",lista->nombre);
  105.        lista= lista->next;
  106.    }
  107.    while (lista != NULL);
  108.    printf ("\n");
  109. }
  110. struct nodo *insertar (struct nodo *head, struct nodo *x) {
  111.    if (head != NULL) {     //Tiene elementos
  112.        head->prev= x;       // El antecesor al primer nodo de la lista apunta al nuevo nodo
  113.        x->next= head;       // El sucesor del nuevo nodo apunta al primer nodo de la lista
  114.        //x->prev=
  115.  
  116.    }
  117.  
  118.    head= x;                // El inicio de la lista apunta al nuevo nodo
  119.  
  120.    return head;
  121. }
  122.  
  123. struct nodo *curr (struct nodo *actual) {
  124.    if (actual) {
  125.        printf ("%s\n",actual->nombre);
  126.        return actual;
  127.    }
  128.    printf ("No hay nodo actual\n");
  129.  
  130.    return actual;
  131. }
  132.  
  133. struct nodo *prev (struct nodo *actual) {
  134.    if (actual && actual->prev) {
  135.        printf ("%s\n",actual->prev->nombre);
  136.        return actual->prev;
  137.    }
  138.    printf ("No hay nodo anterior\n");
  139.  
  140.    return actual;
  141. }
  142. struct nodo *next (struct nodo *actual) {
  143.    if (actual && actual->next) {
  144.        printf ("%s\n",actual->next->nombre);
  145.        return actual->next;
  146.    }
  147.    printf ("No hay nodo siguiente\n");
  148.  
  149.    return actual;
  150. }
  151. int main ()
  152. {
  153.    char nombre[40];
  154.    enum opcion op= EXIT;
  155.    struct nodo *x,*act=NULL;
  156.    struct nodo *head= NULL;
  157.    struct nodo *tail= NULL;
  158.  
  159.    while (1) {
  160.        system ("cls");
  161.        printf ("1. Insertar\n");
  162.        printf ("2. Buscar\n");
  163.        printf ("3. Borrar\n");
  164.        printf ("4. Listar\n");
  165.        printf ("5. Actual\n");
  166.        printf ("6. Siguiente\n");
  167.        printf ("7. Anterior\n");
  168.        printf ("8. Buscar Anterior \n");
  169.        printf ("9. Buscar Siguiente\n");
  170.        printf ("0. Salir\n");
  171.        printf ("\nOpcion: ");
  172.        scanf ("%d", &op);
  173.        fflush (stdin);
  174.  
  175.        switch (op) {
  176.            case INSERT:
  177.                x= insertar (head,crearNodo ());
  178.                if (head == NULL)
  179.                tail=x;
  180.                tail->next=x;
  181.                head=x;
  182.                head->prev =tail;
  183.                act= head;
  184.                break;
  185.            case FIND:
  186.                printf ("Buscar: ");
  187.                gets (nombre);
  188.                act= buscar (head,nombre);
  189.                if (act == NULL)
  190.                    printf ("No se encuentra: %s\n", nombre);
  191.                else
  192.                    printf ("Se encuentra: %s\n", nombre);
  193.                system ("pause");
  194.                break;
  195.            case DELETE:
  196.                printf ("Buscar: ");
  197.                gets (nombre);
  198.                x= borrar (&head,nombre);
  199.                if (x != NULL) {
  200.                    printf ("Se elimino: %s\n", x->nombre);
  201.                    if (x == act) act= NULL;
  202.                }
  203.                else
  204.                    printf ("No se encuentra: %s\n", nombre);
  205.                system ("pause");
  206.                break;              
  207.            case SHOW:
  208.                listar (head);
  209.                system ("pause");
  210.                break;
  211.            case CURR:
  212.                curr (act);
  213.                system ("pause");
  214.                break;
  215.            case NEXT:
  216.                act= next (act);
  217.                system ("pause");
  218.                break;
  219.            case PREV:
  220.                act= prev (act);
  221.                system ("pause");
  222.                break;
  223.            case FINDNEXT:    
  224.                x= buscar_sig (act);
  225.                if (x != NULL) {
  226.                    printf ("Se encontro: %s\n", x->nombre);
  227.                    act=x;
  228.                }
  229.                else
  230.                    printf ("No se encuentra: %s\n", nombre);
  231.                system ("pause");
  232.                break;
  233.            case FINDPREV:
  234.         x= buscar_ant (act);
  235.                if (x != NULL) {
  236.                    printf ("Se encontro: %s\n", x->nombre);
  237.                    act=x;
  238.                }
  239.                else
  240.                    printf ("No se encuentra: %s\n", nombre);
  241.                system ("pause");
  242.                break;
  243.            case EXIT:
  244.                return;
  245.            default:
  246.                printf ("Opcion invalida\n");
  247.        }
  248.    }      
  249. }
  250.  
3  Programación / Programación C/C++ / [Ayuda] Como podria insertar el elemento en mi Cola? en: 25 Septiembre 2016, 20:34 pm
Código
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define MAX 10
  4.  
  5. //ENTREGAR COLA CIRCULAR
  6.  
  7. typedef struct{
  8. struct cola *head;
  9.    struct cola *tail;
  10. int nodos; //contando
  11. } colacirc ;
  12.  
  13. struct cola{
  14. int dato;
  15. struct cola *sig;
  16. };
  17.  
  18. int colaVacia (colacirc *cc) {
  19. return cc->nodos == 0;
  20. }
  21.  
  22. int colaLlena(colacirc *cc ){
  23. return cc->nodos == MAX;
  24. }
  25.  
  26. struct cola nuevo (){
  27. struct cola n;
  28. printf("Introduce un numero");
  29. scanf("%d", &n.dato );
  30. return n;
  31. }
  32.  
  33. int insertarCC(colacirc *cc, struct cola c) {
  34. if(colaLlena(cc) == 1){
  35. printf("La cola esta llena\n");
  36. return 0;
  37. }
  38. if (colaVacia(cc) == 1)
  39. cc->head = &c;
  40. else
  41. cc->tail->sig = &c;
  42.     cc->tail=&c;
  43. cc->nodos++;
  44. return 1;
  45.  
  46. }
  47.  
  48. struct cola *eliminarCC(colacirc *cc){
  49. struct cola *c;
  50. if(colaVacia(cc) == 1){
  51. printf("La cola esta vacian\n");
  52. return NULL;
  53. }
  54. c = cc->head;
  55. if (cc->nodos == 1 )
  56. cc->head = cc->tail = NULL;
  57. else cc->head = cc->head->sig;
  58. cc->nodos--;
  59. return c;
  60. }
  61.  
  62.  
  63. int main (){
  64. struct cola *cc;
  65.  nuevo(&cc);
  66. insertarCC(&cc, 10);
  67.  
  68. return 0;
  69.  
  70. }
  71.  
  72.  


Este es el codigo lo que quiero hacer es poder insertar un elemento a ella, ya tengo todo lo demas solo falta eso.  :-\
4  Programación / Programación C/C++ / Cual es el problema con mi codigo en C? en: 4 Septiembre 2016, 23:59 pm
Código
  1.  
  2. #include <stdlib.h>
  3. #include<stdio.h>
  4. #define MAX_NOMBRE 30
  5.  
  6. struct pelicula{
  7. char *nombre;
  8. char *genero;
  9. short año;
  10. int numDirectores;
  11. char *directores[10];
  12. };
  13.  
  14. void imprimirDatosPelicula(struct pelicula);
  15. void LeerDatosPelicula( struct pelicula *peli);
  16.  
  17. int main(){
  18. struct pelicula peli;
  19. LeerDatosPelicula(&peli);
  20. imprimirDatosPelicula(peli);
  21. return 0;
  22. }
  23.  
  24. void imprimirDatosPelicula(struct pelicula movie){
  25. printf("PELICULA: %s\n", movie.nombre);
  26. printf("GENERO: %s\n", movie.genero);
  27. printf("año: %d\n", movie.año);
  28. printf("# DIRECTOR(ES): %d \n", movie.numDirectores);
  29. int cont = 0;
  30. for ( ; cont < movie.numDirectores ; cont++){
  31. printf("DIRECTOR: %s\n", movie.directores[cont]);
  32.    }
  33. }
  34.  
  35.  
  36. void LeerDatosPelicula(struct pelicula *peli){
  37.  int i=0;
  38.  
  39.   printf("\n\n\tlEER DATOS PELICULA\n");
  40.  printf("Pelicula: ");
  41.    fgets(peli->nombre, MAX_NOMBRE, stdin);
  42. fflush(stdin);
  43. printf ("Genero: ");
  44. fgets(peli->genero, MAX_NOMBRE, stdin);
  45. fflush(stdin);
  46. printf("año: ");
  47. scanf("%d",peli->año);
  48. printf("Cuantos directores?: ");
  49. scanf("%d",peli->numDirectores);
  50. if(peli->numDirectores < 1)   peli->numDirectores=1;
  51. if(peli->numDirectores > 10)  peli->numDirectores=10;
  52.  
  53. for(i=0; i< 1; i++){
  54. peli->directores[i]=(char*) malloc (sizeof (char)*MAX_NOMBRE);
  55. printf("Director %d:  ",i+1);
  56. fflush(stdin);
  57. gets(peli->directores[i]);
  58. }
  59.  
  60. }
  61.  
  62.  
  63.  

Estoy usando Devc++ y al parecer compila muy bien pero a la hora de pedir datos marca error. Alguien que me ayude.

Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines