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

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Mi funcion de borrar falla
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Mi funcion de borrar falla  (Leído 1,596 veces)
Evox4

Desconectado Desconectado

Mensajes: 9


Ver Perfil
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?


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Que falla en la funcion?
PHP
Alex_bro 0 1,477 Último mensaje 11 Abril 2009, 16:03 pm
por Alex_bro
Funcion que falla y borra una tabla entera
PHP
zellion 8 4,089 Último mensaje 2 Junio 2011, 11:47 am
por zellion
Problema con la función borrar en un arrays unidimensional [C++]
Programación C/C++
Jesusinfo 3 2,172 Último mensaje 23 Julio 2016, 07:08 am
por Jesusinfo
Funcion borrar elemento de una lista STRUCT
Programación C/C++
galapok11 3 2,119 Último mensaje 17 Agosto 2016, 17:24 pm
por AlbertoBSD
Que falla en esta funcion??
Programación C/C++
Mozzard 4 1,399 Último mensaje 10 Octubre 2018, 19:22 pm
por MAFUS
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines