document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  
  3.   Name: Representacion enlazada de un grafo
  4.   Author:   Joel Fernandez
  5.   Date: 01/12/12 23:43
  6.   Web: http://codebotic.blogspot.com
  7.   Description: programa que muestra un grafo en su forma enlazada
  8. */
  9. #include<iostream.h>
  10. #include<conio.h>
  11.  
  12. struct nodo{
  13.             char nombre;//nombre del vertice o nodo
  14.             struct nodo *sgte;
  15.             struct arista *ady;//puntero hacia la primera arista del nodo
  16.             };
  17. struct arista{
  18.               struct nodo *destino;//puntero al nodo de llegada
  19.               struct arista *sgte;
  20.               };
  21. typedef struct nodo *Tnodo;//  Tipo Nodo
  22. typedef struct arista *Tarista; //Tipo Arista
  23.  
  24. Tnodo p;//puntero cabeza
  25.  
  26. void menu();
  27. void insertar_nodo();
  28. void agrega_arista(Tnodo &, Tnodo &, Tarista &);
  29. void insertar_arista();
  30. void vaciar_aristas(Tnodo &);
  31. void eliminar_nodo();
  32. void eliminar_arista();
  33. void mostrar_grafo();
  34. void mostrar_aristas();
  35.  
  36.  
  37. /*                        Funcion Principal
  38. ---------------------------------------------------------------------*/
  39. int main(void)
  40. {   p=NULL;
  41.     int op;     // opcion del menu
  42.  
  43.  
  44.     system("color 0b");
  45.  
  46.     do
  47.     {
  48.         menu();
  49.         cin>>op;
  50.  
  51.         switch(op)
  52.         {
  53.             case 1:
  54.                     insertar_nodo();
  55.                     break;
  56.             case 2: insertar_arista();
  57.                     break;
  58.             case 3: eliminar_nodo();
  59.                     break;
  60.             case 4: eliminar_arista();
  61.                     break;
  62.             case 5: mostrar_grafo();
  63.                     break;
  64.             case 6: mostrar_aristas();
  65.                     break;
  66.  
  67.             default: cout<<"OPCION NO VALIDA...!!!";
  68.                      break;
  69.  
  70.  
  71.         }
  72.  
  73.         cout<<endl<<endl;
  74.         system("pause");  system("cls");
  75.  
  76.     }while(op!=7);
  77.     getch();
  78.     return 0;
  79. }
  80.  
  81. /*                          Menu
  82. ---------------------------------------------------------------------*/
  83. void menu()
  84. {
  85.     cout<<"\\n\\tREPRESENTACION DE GRAFOS DIRIGIDOS\\n\\n";
  86.     cout<<" 1. INSERTAR UN NODO                 "<<endl;
  87.     cout<<" 2. INSERTAR UNA ARISTA              "<<endl;
  88.     cout<<" 3. ELIMINAR UN NODO                 "<<endl;
  89.     cout<<" 4. ELIMINAR UNA ARISTA              "<<endl;
  90.     cout<<" 5. MOSTRAR  GRAFO                   "<<endl;
  91.     cout<<" 6. MOSTRAR ARISTAS DE UN NODO       "<<endl;
  92.     cout<<" 7. SALIR                            "<<endl;
  93.  
  94.     cout<<"\\n INGRESE OPCION: ";
  95. }
  96.  
  97. /*                      INSERTAR NODO AL GRAFO
  98. ---------------------------------------------------------------------*/
  99. void insertar_nodo()
  100. {
  101.     Tnodo t,nuevo=new struct nodo;
  102.     cout<<"INGRESE VARIABLE:";
  103.     cin>>nuevo->nombre;
  104.     nuevo->sgte = NULL;
  105.     nuevo->ady=NULL;
  106.  
  107.     if(p==NULL)
  108.      {
  109.         p = nuevo;
  110.         cout<<"PRIMER NODO...!!!";
  111.       }
  112.     else
  113.      {
  114.         t = p;
  115.         while(t->sgte!=NULL)
  116.          {
  117.             t = t->sgte;
  118.           }
  119.         t->sgte = nuevo;
  120.         cout<<"NODO INGRESADO...!!!";
  121.       }
  122.  
  123.  }
  124.  
  125. /*                      AGREGAR ARISTA
  126.     funcion que utilizada para agregar la arista a dos nodos
  127. ---------------------------------------------------------------------*/
  128. void agrega_arista(Tnodo &aux, Tnodo &aux2, Tarista &nuevo)
  129. {
  130.     Tarista q;
  131.     if(aux->ady==NULL)
  132.     {   aux->ady=nuevo;
  133.         nuevo->destino=aux2;
  134.         cout<<"PRIMERA ARISTA....!";
  135.     }
  136.     else
  137.     {   q=aux->ady;
  138.         while(q->sgte!=NULL)
  139.             q=q->sgte;
  140.         nuevo->destino=aux2;
  141.         q->sgte=nuevo;
  142.         cout<<"ARISTA AGREGADA...!!!!";
  143.     }
  144.  
  145. }
  146. /*                      INSERTAR ARISTA
  147.     funcion que busca las posiciones de memoria de los nodos
  148.     y hace llamado a agregar_arista para insertar la arista
  149. ---------------------------------------------------------------------*/
  150. void insertar_arista()
  151. {   char ini, fin;
  152.     Tarista nuevo=new struct arista;
  153.     Tnodo aux, aux2;
  154.  
  155.     if(p==NULL)
  156.      {
  157.          cout<<"GRAFO VACIO...!!!!";
  158.          return;
  159.      }
  160.     nuevo->sgte=NULL;
  161.     cout<<"INGRESE NODO DE INICIO:";
  162.     cin>>ini;
  163.     cout<<"INGRESE NODO FINAL:";
  164.     cin>>fin;
  165.     aux=p;
  166.     aux2=p;
  167.     while(aux2!=NULL)
  168.     {
  169.         if(aux2->nombre==fin)
  170.         {
  171.             break;
  172.         }
  173.  
  174.         aux2=aux2->sgte;
  175.     }
  176.     while(aux!=NULL)
  177.     {
  178.         if(aux->nombre==ini)
  179.         {
  180.             agrega_arista(aux,aux2, nuevo);
  181.             return;
  182.         }
  183.  
  184.         aux = aux->sgte;
  185.     }
  186. }
  187.  
  188. /*          FUNCION PARA BORRAR TODAS LAS ARISTAS DE UN NODO
  189.     esta funcion es utilizada al borrar un nodo pues si tiene aristas
  190.     es nesesario borrarlas tambien y dejar libre la memoria
  191. ---------------------------------------------------------------------*/
  192. void vaciar_aristas(Tnodo &aux)
  193. {
  194.     Tarista q,r;
  195.     q=aux->ady;
  196.     while(q->sgte!=NULL)
  197.     {
  198.         r=q;
  199.         q=q->sgte;
  200.         delete(r);
  201.     }
  202. }
  203. /*                      ELIMINAR NODO
  204.     funcion utilizada para eliminar un nodo del grafo
  205.     pero para eso tambien tiene que eliminar sus aristas por lo cual
  206.     llama a la funcion vaciar_aristas para borrarlas
  207. ---------------------------------------------------------------------*/
  208. void eliminar_nodo()
  209. {   char var;
  210.     Tnodo aux,ant;
  211.     aux=p;
  212.     cout<<"ELIMINAR UN NODO\\n";
  213.     if(p==NULL)
  214.      {
  215.          cout<<"GRAFO VACIO...!!!!";
  216.          return;
  217.      }
  218.     cout<<"INGRESE NOMBRE DE VARIABLE:";
  219.     cin>>var;
  220.  
  221.     while(aux!=NULL)
  222.     {
  223.         if(aux->nombre==var)
  224.         {
  225.             if(aux->ady!=NULL)
  226.                 vaciar_aristas(aux);
  227.  
  228.             if(aux==p)
  229.             {
  230.  
  231.                     p=p->sgte;
  232.                     delete(aux);
  233.                     cout<<"NODO ELIMINADO...!!!!";
  234.                     return;
  235.  
  236.  
  237.  
  238.             }
  239.             else
  240.             {
  241.                 ant->sgte = aux->sgte;
  242.                 delete(aux);
  243.                 cout<<"NODO ELIMINADO...!!!!";
  244.                 return;
  245.             }
  246.         }
  247.         else
  248.         {
  249.             ant=aux;
  250.             aux=aux->sgte;
  251.          }
  252.     }
  253.  
  254. }
  255.  
  256. /*                      ELIMINAR ARISTA
  257.     funcion utilizada para eliminar una arista
  258. ---------------------------------------------------------------------*/
  259. void eliminar_arista()
  260. {
  261. char ini, fin;
  262.     Tnodo aux, aux2;
  263.     Tarista q,r;
  264.     cout<<"\\n ELIMINAR ARISTA\\n";
  265.     cout<<"INGRESE NODO DE INICIO:";
  266.     cin>>ini;
  267.     cout<<"INGRESE NODO FINAL:";
  268.     cin>>fin;
  269.     aux=p;
  270.     aux2=p;
  271.     while(aux2!=NULL)
  272.     {
  273.         if(aux2->nombre==fin)
  274.         {
  275.             break;
  276.         }
  277.         else
  278.         aux2=aux2->sgte;
  279.     }
  280.      while(aux!=NULL)
  281.     {
  282.         if(aux->nombre==ini)
  283.         {
  284.             q=aux->ady;
  285.             while(q!=NULL)
  286.             {
  287.                 if(q->destino==aux2)
  288.                 {
  289.                     if(q==aux->ady)
  290.                         aux->ady=aux->ady->sgte;
  291.                     else
  292.                         r->sgte=q->sgte;
  293.                     delete(q);
  294.                     cout<<"ARISTA  "<<aux->nombre<<"----->"<<aux2->nombre<<" ELIMINADA.....!!!!";
  295.                     return;
  296.                 }
  297.             }
  298.             r=q;
  299.             q=q->sgte;
  300.         }
  301.         aux = aux->sgte;
  302.     }
  303. }
  304. /*                      MOSTRAR GRAFO
  305.     funcion que imprime un grafo en su forma enlazada
  306. ---------------------------------------------------------------------*/
  307. void mostrar_grafo()
  308. {   Tnodo ptr;
  309.     Tarista ar;
  310.     ptr=p;
  311.     cout<<"NODO|LISTA DE ADYACENCIA\\n";
  312.  
  313.     while(ptr!=NULL)
  314.     {   cout<<"   "<<ptr->nombre<<"|";
  315.         if(ptr->ady!=NULL)
  316.         {
  317.             ar=ptr->ady;
  318.             while(ar!=NULL)
  319.             {   cout<<" "<<ar->destino->nombre;
  320.                 ar=ar->sgte;
  321.              }
  322.  
  323.         }
  324.         ptr=ptr->sgte;
  325.         cout<<endl;
  326.     }
  327. }
  328.  
  329. /*                      MOSTRAR ARISTAS
  330.     funcion que muestra todas las aristas de un nodo seleccionado
  331. ---------------------------------------------------------------------*/
  332. void mostrar_aristas()
  333. {
  334.     Tnodo aux;
  335.     Tarista ar;
  336.     char var;
  337.     cout<<"MOSTRAR ARISTAS DE NODO\\n";
  338.     cout<<"INGRESE NODO:";
  339.     cin>>var;
  340.     aux=p;
  341.     while(aux!=NULL)
  342.     {
  343.         if(aux->nombre==var)
  344.         {
  345.             if(aux->ady==NULL)
  346.             {   cout<<"EL NODO NO TIENE ARISTAS...!!!!";
  347.                 return;
  348.              }
  349.             else
  350.             {
  351.                 cout<<"NODO|LISTA DE ADYACENCIA\\n";
  352.                 cout<<"   "<<aux->nombre<<"|";
  353.                 ar=aux->ady;
  354.  
  355.                 while(ar!=NULL)
  356.                 {
  357.                     cout<<ar->destino->nombre<<" ";
  358.                     ar=ar->sgte;
  359.                 }
  360.                 cout<<endl;
  361.                 return;
  362.             }
  363.         }
  364.         else
  365.         aux=aux->sgte;
  366.     }
  367. }
');