Ver Mensaje Individual
  #2 (permalink)  
Antiguo 02/02/2010, 11:01
Avatar de fradve
fradve
 
Fecha de Ingreso: abril-2009
Mensajes: 157
Antigüedad: 15 años, 1 mes
Puntos: 7
Respuesta: problemas con nodos

Creo que te refieres al nodo de una lista.

un nodo podemos definirlo como el elemento que forma parte de una lista enlazada, se usan ya sea para listas simples, dobles, circulares o una pilas, con ellos puedes realizar diversas operaciones, como crear, eliminar, modificar, presentarlos, moverlos, etc...


Un ejemplo sencillo de una lista simplemente enlazada:

Código C++:
Ver original
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. struct lista
  5. {
  6.   int dato;
  7.   struct lista *sig;
  8. };
  9.  
  10. lista *cab=NULL,*nuevo=NULL,*fin=NULL;
  11. int c=0;
  12.  
  13. void ingreso()
  14. {
  15.   nuevo=new lista;
  16.  
  17.   cout << "NUMERO: "; cin >> nuevo->dato;
  18.   if(cab==NULL)
  19.   {
  20.     nuevo->sig=NULL;
  21.     cab=nuevo;
  22.     fin=nuevo;
  23.   }
  24.   else
  25.   {
  26.     nuevo->sig=NULL;
  27.     fin->sig=nuevo;
  28.     fin=nuevo;
  29.   }
  30.   c++;
  31. }
  32.  
  33. void ordenar()
  34. {
  35.   lista *aux,*aux1;
  36.   int temp;
  37.  
  38.   if(cab!=NULL)
  39.   {
  40.     aux=cab;
  41.     do
  42.     {
  43.       aux1=aux;
  44.       do
  45.       {
  46.     if(aux->dato<aux1->dato)
  47.     {
  48.       temp=aux->dato;
  49.       aux->dato=aux1->dato;
  50.       aux1->dato=temp;
  51.     }
  52.     aux1=aux1->sig;
  53.       }while(aux1!=NULL);
  54.       aux=aux->sig;
  55.     }while(aux!=NULL);
  56.   }
  57.   else
  58.    cout << "LISTA VACIA";
  59.   getch();
  60. }
  61.  
  62. void imprimir()
  63. {
  64.   lista *aux;
  65.  
  66.   if(cab!=NULL)
  67.   {
  68.     aux=cab;
  69.     while(aux!=NULL)
  70.     {
  71.       cout << aux->dato << endl;
  72.       aux=aux->sig;
  73.     }
  74.   }
  75.   else
  76.    cout << "LISTA VACIA";
  77.   getch();
  78. }
  79.  
  80. void eliminar()
  81. {
  82.   lista *an,*si;
  83.   int dat;
  84.  
  85.   if(cab!=NULL)
  86.   {
  87.     cout << "DATO A ELIMINAR: "; cin >> dat;
  88.     an=cab;
  89.     if(an->dato==dat)
  90.     {
  91.       cab=cab->sig;
  92.       delete an;
  93.     }
  94.     else
  95.     {
  96.       do
  97.       {
  98.     si=an->sig;
  99.     if(si->dato==dat)
  100.     {
  101.       an->sig=si->sig;
  102.       delete si;
  103.       break;
  104.     }
  105.     an=an->sig;
  106.       }while(si!=NULL);
  107.     }
  108.   }
  109.   else
  110.    cout << "LISTA VACIA";
  111.   getch();
  112. }
  113.  
  114. void insertar()
  115. {
  116.   lista *an,*si;
  117.   int p,co=1;
  118.  
  119.   cout << "POSICION : "; cin >> p;
  120.   nuevo=new lista;
  121.   cout << "DATO: "; cin >> nuevo->dato;
  122.   if(p<c)
  123.   {
  124.     an=cab;
  125.     if(p==1)
  126.     {
  127.       nuevo->sig=cab;
  128.       cab=nuevo;
  129.     }
  130.     else
  131.     {
  132.       si=an;
  133.       do
  134.       {
  135.     an=si;
  136.     si=si->sig;
  137.     co++;
  138.       }while(co<p);
  139.       nuevo->sig=si;
  140.       an->sig=nuevo;
  141.     }
  142.   }
  143.   else
  144.   {
  145.     nuevo->sig=NULL;
  146.     fin->sig=nuevo;
  147.     fin=nuevo;
  148.   }
  149. }
  150.  
  151. void main()
  152. {
  153.   char key;
  154.  
  155.   do
  156.   {
  157.     clrscr();
  158.     cout << "\n\t\t\t-= MENU PRINCIPAL =- ";
  159.     cout << "\n\n\t\t\t[1] INGRESAR";
  160.     cout << "\n\t\t\t[2] ORDENAR";
  161.     cout << "\n\t\t\t[3] IMPRIMIR";
  162.     cout << "\n\t\t\t[4] ELIMINAR";
  163.     cout << "\n\t\t\t[5] INSERTAR";
  164.     cout << "\n\t\t\t[6] SALIR";
  165.     key=getche();
  166.     clrscr();
  167.     switch(key)
  168.     {
  169.       case '1': ingreso(); break;
  170.       case '2': ordenar(); break;
  171.       case '3': imprimir(); break;
  172.       case '4': eliminar(); break;
  173.       case '5': insertar(); break;
  174.     }
  175.   }while(key!='6');
  176. }
__________________
En programación hay mil y un formas de hacer lo mismo...