Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: bash en 14 Octubre 2016, 20:53 pm



Título: [ayuda] punteros en linklist
Publicado por: bash en 14 Octubre 2016, 20:53 pm
Buenas a todos !!

estoy actualmete aprendiendo a implementar una lista enlazada simple y me gustaria saber como es que los punteros estan trabajando de manerar interna para poder comunicarse, si es algo sencillo en teoria pero tengo una duda que ronda en mi cabeza hace un par de minutos, estoy siguiendo un tutorial y he entendido algunas cosas como que cada nodo (usando una estructura ) tieene un puntero a otro nodo que esta en una "lista" y al final de la lista hay uno que apunta a NULL y para este aproach estoy usando c++ y plantillas , le explico un poco como tengo mi codigo:

tengo una clase llamada List esta contiene una estructura Node que contiene el dato T y el link al sugiente elemento de la estructura .... ahora en la clase tengo 2 puntero first y curr
estos contienen el dato de la cabeza (Inicio de la estructura) y curr que contiene el frente de la estructura (front )  bien en la funcion siguiente

 
Código
  1.  
  2.    void insert(T f)
  3.    {
  4.      Node<T> *temp= new Node<T>(f);
  5.       if(first == NULL)
  6.       {
  7.       first= temp;
  8.       curr = temp;
  9.       }
  10.       else
  11.       {
  12.       curr->next=temp;
  13.       curr = temp;
  14.       }
  15.    }
  16.  
el primer if esta bien  pero esta parte en else me hace perderme por ejemeplo
Código
  1.     void Print(){
  2.     if(first == NULL) return;
  3.         curr = first;
  4.     while( curr  )
  5.     {
  6.     cout << "Value is : "<< curr->date<<endl;
  7.     curr = curr->next;
  8.     }
  9.     }
  10.  

donde curr = first
toma el first pero en la funcion Insert la que se lleno fue curr por que first tiene el link de first, alguien podria explicarme eso por favor.


Código
  1. #include <iostream>
  2.  
  3. #include <string.h>
  4. using std::cout;
  5. using std::string;
  6.  
  7. template<typename T>
  8. class List
  9. {
  10. private:
  11. template<class R>
  12. struct Node{
  13. R  date;
  14. Node *next;
  15.    Node(T t){
  16.     date = t;
  17.     next = NULL;
  18.    }
  19. };
  20.  
  21. Node<T> *first;
  22. Node<T> *curr;
  23.  
  24. public:
  25.  
  26. List(){
  27. first = NULL;
  28. curr  = NULL;
  29. }
  30.    List(int d)
  31.    {
  32.     Node<T> *temp= new Node<T>(d);
  33.     first = temp;
  34.     curr  = temp;
  35.    }
  36.  
  37.    void insert(T f)
  38.    {
  39.      Node<T> *temp= new Node<T>(f);
  40.       if(first == NULL)
  41.       {
  42.       first= temp;
  43.       curr = temp;
  44.       }
  45.       else
  46.       {
  47.       curr->next=temp;
  48.       curr = temp;
  49.       }
  50.    }
  51.  
  52.     void Print(){
  53.     if(first == NULL) return;
  54.         curr = first;
  55.     while( curr  )
  56.     {
  57.     cout << "Value is : "<< curr->date<<endl;
  58.     curr = curr->next;
  59.     }
  60.     }
  61.  
  62.  
  63.     void DeleteData(int data)
  64.     {
  65.         Node<T> *delPtr = NULL;
  66.         Node<T> *temp   = first;
  67.         curr            = first;
  68.  
  69.         while(curr != NULL && curr->date != data)
  70.         {
  71.         temp = curr;
  72.         curr = curr->next;
  73.         }
  74.  
  75.         if(curr == NULL)
  76.         {
  77.         cout << "this data is not in the list;";
  78.         }
  79.         else
  80.         {
  81.         delPtr = curr;
  82.         curr = curr->next;
  83.          temp->next = curr;
  84.         if(delPtr == first)
  85.         {
  86.         first = first->next;
  87.          temp  = NULL;
  88.         }
  89.             delete delPtr;
  90.         }
  91.  
  92.     }
  93.  
  94.  
  95.  
  96.  
  97. };
  98.  
  99.  
  100.  
  101. int main()
  102. {
  103.   List<int > ls;
  104.   ls.insert(12);
  105.   ls.insert(345);
  106.   ls.insert(345);
  107.   ls.DeleteData(345);
  108.   ls.Print();
  109. }
  110.  
  111.  


Título: Re: [ayuda] punteros en linklist
Publicado por: ivancea96 en 15 Octubre 2016, 13:47 pm
Bien, un detalle.

Código
  1. void Print(){
  2. if(first == NULL) return;
  3. curr = first;
  4. while( curr  )
  5. {
  6. cout << "Value is : "<< curr->date<<endl;
  7. curr = curr->next;
  8. }
  9. }
Al acabar esa función, curr queda siendo igual a NULL. Eso te dará un error si luego tratas de hacer un insert, al tratar de hacer curr->next. Utiliza una variable a parte:
Código
  1. void Print(){
  2. if(first == NULL) return;
  3. Node<T> *temp = first;
  4. while( temp  )
  5. {
  6. cout << "Value is : "<< temp->date<<endl;
  7. temp = temp->next;
  8. }
  9. }
Y como dato, el if lo puedesobviar si quieres (si es NULL, simplemente no entrará al while)
Código
  1. void Print(){
  2. Node<T> *temp = first;
  3. while( temp  )
  4. {
  5. cout << "Value is : "<< temp->date<<endl;
  6. temp = temp->next;
  7. }
  8. }

Citar
toma el first pero en la funcion Insert la que se lleno fue curr por que first tiene el link de first, alguien podria explicarme eso por favor.
No se si estoy entendiendo muy bien. En la función Print, quieres recorrer toda la lista. Así que, empiezas desde el primer elemento (first), y vas avanzando al siguiente (temp->next) hasta que sea NULL (final de la lista).

Perdona si no entendí bien la pregunta.