Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: bash en 14 Febrero 2017, 23:07 pm



Título: lista enlazada opiniones [CODIGO FUENTE]
Publicado por: bash en 14 Febrero 2017, 23:07 pm
un saludo a todos , ahora mismo me encuentro estudiando estructura de datos y estoy tratando de aprender a  crear lista enlazada !!

Código
  1.  
  2.  
  3. class Link{
  4. struct node{
  5. node *next;
  6. int vl;
  7. };
  8.  
  9. node *head;
  10. node *curr;
  11. node *temp;
  12.  
  13. public:
  14.  
  15.  
  16. Link(){
  17. head = curr = temp = NULL;
  18. }
  19.  
  20. void Add(int d){
  21.  
  22. node *n = new node;
  23. n-> next = NULL;
  24. n->vl = d;
  25.  
  26.  
  27.   if( head == NULL)
  28.   {
  29.   head = n;
  30.   }
  31.   else
  32.   {
  33.      curr = head ;
  34.      while(curr->next){
  35.      curr = curr->next;
  36.      }
  37.      curr->next = n;
  38.      curr = n;
  39.   }
  40.  
  41. }
  42.  
  43. void Delete(int vl){
  44. temp = head;
  45. curr = head;
  46. node *temp_;
  47. if(head != NULL){
  48.  
  49. if( head->vl == vl )
  50. {
  51.   if(head->next != NULL)
  52.   {
  53.   temp  = head->next;
  54.  
  55.   temp_ = head;
  56.  
  57.   head = temp;
  58.  
  59.   delete temp_;
  60.   temp_ = NULL;
  61.   return;
  62.   }
  63.  
  64. }
  65. }
  66.  
  67.        while(curr != NULL && curr->vl != vl){
  68.         temp = curr;
  69.         curr = curr->next;
  70.        }
  71. if(curr == NULL)
  72. return;
  73. else{
  74. temp_ = curr;
  75. curr = curr->next;
  76. temp->next = curr;
  77.    delete temp_;
  78.    temp_ = NULL;
  79. }
  80.  
  81.  
  82. }
  83.  
  84.  void Print(){
  85.  node *temp = head;
  86.  if(temp != NULL){
  87.  while(temp != NULL)
  88.  {
  89.  cout << temp->vl <<endl;
  90.  temp = temp->next;
  91.  }
  92.  }
  93.  }
  94.  
  95. };
  96.  
  97.  
  98.  
  99.  
  100. int main(){
  101.  
  102. Link li;
  103. li.Add(0);
  104.    li.Add(1);
  105.    li.Add(2);
  106.    li.Add(3);
  107.    li.Add(4);
  108.    li.Add(5);
  109.    li.Add(6);
  110.    li.Add(7);
  111.    li.Add(8);
  112.    li.Add(9);
  113.    li.Delete(5);
  114.    li.Delete(0);
  115.    li.Delete(8);
  116.    li.Print();
  117.  
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  



 me gustaria opiniones.  gracias de antemano...