Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: d91 en 19 Octubre 2015, 01:29 am



Título: lista doblemente enlazada
Publicado por: d91 en 19 Octubre 2015, 01:29 am
hola a todos, estoy tratando de hacer una insercion en una lista enlazada doble pero tengo error en la asignacion del puntero del nodo hacia el anterior, alguien me podria ayudar
Código
  1.  
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. struct nodo{
  7.       int nro;
  8.       struct nodo *sgte;
  9.       struct nodo *anterior;
  10. };
  11.  
  12. typedef struct nodo *Tlista;
  13. /*
  14. ----------------------------------------------
  15. */
  16. void insertarFinal(Tlista &lista, int valor)
  17. {
  18.    Tlista t, q = new(struct nodo);
  19.  
  20.    q->nro  = valor;
  21.  
  22.    if(lista==NULL)
  23.    {
  24.        q->sgte = NULL;
  25.        q->anterior = lista;
  26.        lista = q;
  27.    }
  28.    else
  29.    {
  30.        t = lista;
  31.        while(t->sgte!=NULL)
  32.        {
  33.            t = t->sgte;
  34.        }
  35.        t->sgte = q;
  36.        q->anterior = t;
  37.    }
  38.  
  39. }
  40.  
  41.  


Título: Re: lista doblemente enlazada
Publicado por: d91 en 19 Octubre 2015, 04:06 am
ya encontre el error, y queda asi
Código
  1.  
  2. void insertarFinal(Tlista &lista, int valor)
  3. {
  4.    Tlista t, q = new(struct nodo);
  5.  
  6.    q->nro  = valor;
  7.    q->estado = 1;
  8.    q->sgte = NULL;
  9.  
  10.    if(lista==NULL)
  11.    {
  12.        q->anterior = lista;
  13.        lista = q;
  14.    }
  15.    else
  16.    {
  17.        t = lista;
  18.        while(t->sgte!=NULL)
  19.        {
  20.            t = t->sgte;
  21.        }
  22.        t->sgte = q;
  23.        q->anterior = t;
  24.    }
  25.  
  26. }
  27.