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

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  [ayuda] punteros en linklist
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [ayuda] punteros en linklist  (Leído 1,478 veces)
bash

Desconectado Desconectado

Mensajes: 258



Ver Perfil
[ayuda] punteros en linklist
« 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.  


En línea

gracias por responder mis dudas
ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: [ayuda] punteros en linklist
« Respuesta #1 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.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Ayuda]con punteros en asm
ASM
ny0x 4 6,751 Último mensaje 12 Mayo 2009, 21:45 pm
por ny0x
Ayuda punteros. [c++]
Programación C/C++
ninto33 3 3,803 Último mensaje 19 Enero 2011, 01:46 am
por ninto33
Duda Punteros Dobles/Array de punteros
Programación C/C++
MisterJava 2 4,901 Último mensaje 30 Diciembre 2012, 20:19 pm
por MisterJava
ayuda con punteros de C
Programación C/C++
LeoJ24 1 1,711 Último mensaje 11 Enero 2013, 18:13 pm
por durasno
Ayuda con punteros (en C++) (Actualizacion constante con nuevas preguntas)
Programación C/C++
DarkSorcerer 5 2,327 Último mensaje 18 Septiembre 2013, 07:12 am
por eferion
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines