/
Inicio :: Foros

 F.A.Q.F.A.Q.                  Conéctese para revisar sus mensajesConéctese para revisar sus mensajes   

estructura doblemente encadenada

 
      Índice del Foro elrincondelc.com -> Algoritmos
Ver tema anterior :: Ver siguiente tema  
AutorMensaje
mihina



Registrado: 21 Feb 2011
Mensajes: 9

MensajePublicado: 13/05/2011 1:55 pm
Título: estructura doblemente encadenada

Buenas estoy haciendo una lista doblemente encadenada; entiendo su funcionamiento pero no acabo de poder plasmarlo en el codigo. Haber si me podeis aconsegar de como hacerlo.

Gracias!!.

ESTRUCTURA:

Código:

struct Node {
            int dada;
            Node *seguent;
            Node *anterior;

        };
        Node *a_inici;




METODOS:
Código:

EstDinDE::EstDinDE()
{
    a_inici=NULL;
}

void EstDinDE::afegirInici(int e)
{
    Node *p;
    p=new Node();
    p->dada=e;
    p->seguent=a_inici;
    p->anterior=NULL;
    //a_inici->anterior=p;
    a_inici=p;
}

void EstDinDE::afegirFinal(int e)
{
    Node *p,*aux;
    p=new Node();
    aux=new Node();
    bool inserit=false;

    if(a_inici==NULL){
        p->dada=e;
        p->seguent=a_inici;
        p->anterior=NULL;
        a_inici=p;
    }
    else {
        p=a_inici;
        p=p->seguent;
        aux=a_inici;
        while(!inserit){
            if(p!=NULL){
                p=p->seguent;
                aux=aux->seguent;
            }
            else{
                Node *nou;
                nou=new Node();

                nou->dada=e;
                nou->seguent=NULL;
                nou->anterior=aux->seguent;
                p->seguent=nou;

                inserit=true;
            }
        }
    }
}

void EstDinDE::esborrar(int e)
{
    Node *p;
    p=new Node();

    bool trobat=false;

    trobat=existeix(e);
   
    if((p!=NULL)&&trobat)
    {
        if(p==a_inici)
        {
            a_inici=p->seguent;
            if(p->seguent != NULL)
                p->seguent->anterior =NULL;
        }
        else if  (p->seguent!= NULL)
        {
            p->anterior->seguent=p->seguent;
            p->seguent->anterior=p->anterior;
        }
        else {
            p->anterior->seguent=NULL;
        }
        delete p;
    }
}

bool EstDinDE::existeix(int e)
{
    Node *p;
    bool trobat=false;
    p=a_inici;

    while((p!=NULL)&&(!trobat))
    {
        if (p->dada==e) trobat=true;
        p=p->seguent;
    }
    return(trobat);
}

void EstDinDE::llistar()
{
    Node *p;
    p=a_inici;

    while(p->anterior) p=p->anterior;
    while(p) {
         cout << p->dada <<endl;
         p=p->seguent;
    }
}

Volver arriba
rir3760



Registrado: 01 Oct 2004
Mensajes: 6586
Ubicación: Mexico

MensajePublicado: 13/05/2011 3:09 pm
Título:

Hola mihina

Nos ayudarias mucho si, cuando tienes una duda, publicas esta junto con el codigo fuente en español ya que ese es el idioma de los foros.

Al parecer habia algunos errores, el fragmento "corregido":
Código:
EstDinDE::EstDinDE()
{
    a_inici = 0; // En C++ se prefiere 0 en lugar de NULL
}

void EstDinDE::afegirInici(int e)
{
   Node* p = new Node;

   p->dada = e;
   p->seguent = a_inici;
   p->anterior = 0;

   a_inici->anterior = p;
   a_inici = p;
}

void EstDinDE::afegirFinal(int e)
{
   Node* p = new Node;
   p->dada = e;
   p->seguent = 0;
   p->anterior = 0;

   if (a_inici == 0)
      a_inici = p;
   else {
      Node* q;
      for (q = a_inici; q->seguent != 0; q = q->seguent)
         ;

      p->anterior = q;
      q->seguent = p;
   }
}

void EstDinDE::esborrar(int e)
{
   if (a_inici != 0){
      if (a_inici->dada == e){
         Node* aux = a_inici;
         a_inici = a_inici->seguent;
         if (a_inici != 0)
            a_inici->anterior = 0;
         delete aux;
      }else {
         Node* q;
         for (q = a_inici; q->seguent != 0 && q->seguent->dada != e; q = q->seguent)
            ;
         if (q->seguent != 0){
            Node* aux = q->seguent;
            q->seguent = aux->seguent;
            if (aux->seguent != 0)
               aux->seguent->anterior = q;
            delete aux;
         }
      }
   }
}

bool EstDinDE::existeix(int e)
{
    Node *p = a_inici;

    while (p != 0 && p->dada != e)
        p = p->seguent;
   
    return p == 0;
}

void EstDinDE::llistar()
{
    for (Node* p = a_inici; p != 0; p = p->seguent)
      cout << p->dada <<endl;
}


Corregido entre comillas ya que para estar seguros todo este bien necesitamos el codigo fuente del programa completo.

Un saludo
_________________
Memory never recaptures reality. Memory reconstructs. All reconstructions change the original, becoming external frames of reference that inevitably fall short.
--
Mentat Handbook
Volver arriba
mihina



Registrado: 21 Feb 2011
Mensajes: 9

MensajePublicado: 15/05/2011 8:07 am
Título:

muchas gracias!!!! y lo siento por el idioma; se me escapo!!!! Embarassed realmente es mas facil de lo que me pensaba!
Gracias!!!
Volver arriba
      Índice del Foro elrincondelc.com -> Algoritmos
Página 1 de 1Todas las horas están en GMT - 8 Horas

 
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas

(c) ElRincondelC.com

Un proyecto de UrlanHeat.com