Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Evox4 en 9 Octubre 2016, 09:31 am



Título: [Ayuda] Como puede hacer de mi programa una Lista doblemente ligada "circular" C
Publicado por: Evox4 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.  


Título: Re: [Ayuda] Como puede hacer de mi programa una Lista doblemente ligada "circular" C
Publicado por: ivancea96 en 9 Octubre 2016, 13:05 pm
La única diferencia, es que el nodo "siguiente" del nodo final, es el primero. Y el nodo "anterior" del primer nodo, es el último. Está conectada la cola con la cabeza.

Eso sí, hay q hacer ligeros cambios. por ejemplo, no puedes hacer un:
Código
  1. while(ptr->next != NULL){
  2.    // ...
  3.    ptr = ptr->next;
  4. }

Porque si la lsita tiene elementos, qerá un bucle infinito.
Quitando esos cambios, el resto es parecido.


Título: Re: [Ayuda] Como puede hacer de mi programa una Lista doblemente ligada "circular" C
Publicado por: Evox4 en 9 Octubre 2016, 21:53 pm
La única diferencia, es que el nodo "siguiente" del nodo final, es el primero. Y el nodo "anterior" del primer nodo, es el último. Está conectada la cola con la cabeza.

Eso sí, hay q hacer ligeros cambios. por ejemplo, no puedes hacer un:
Código
  1. while(ptr->next != NULL){
  2.    // ...
  3.    ptr = ptr->next;
  4. }

Porque si la lsita tiene elementos, qerá un bucle infinito.
Quitando esos cambios, el resto es parecido.


Gracias por la notacion.