- // 
- #include <stdio.h> 
- #include <stdlib.h> 
- #include <string.h> 
- #define FALSO 0 
-   
- typedef struct grafo{ 
-         struct grafo *vertice; 
-         struct vertad *verticead; 
-         char nombre[10]; 
-         int vis; 
- }elemento_grafo; 
-   
- typedef struct vertad{ 
-         struct vertad *verticead; 
-         char nombre[10]; 
-         int vis; 
- }elemento_vert; 
-   
- typedef struct pilas{ 
-         struct pilas *sig; 
-         char nombre[10]; 
- }pila; 
-   
- void menu(int *op); 
- elemento_grafo *Crearnodo(); 
- elemento_grafo Creararco(); 
- elemento_grafo *Agregarvertice(elemento_grafo *cabeza); 
- elemento_grafo *buscar(elemento_grafo *cabeza,char nom[10]); 
- void Mostrargrafo(elemento_grafo *cabeza); 
- void Calculargrado(elemento_grafo *cabeza); 
- elemento_grafo  *Eliminarvertice(elemento_grafo *cabeza, char nom[10]); 
- void Recorrido(elemento_grafo *cabeza); 
- void Limpiar(elemento_grafo *cabeza); 
- void visitado(elemento_grafo *cabeza,char nom[10]); 
- pila *empilar(pila *inicio, elemento_vert *tem , char nom[10]); 
- pila *desempilar(pila *inicio, char nom[10]); 
-   
- FILE *arch; 
- int cont=0; 
- int main(){ 
-   elemento_grafo *cabeza; 
-   int op; 
-   char nom[10]; 
-   arch  = fopen("datos.txt","r+b");
-   cabeza = NULL; 
-   if(arch != NULL){ 
-   printf("\n Programa de grafos con listas de adyacencia"); 
-   do{ 
-     menu(&op); 
-     switch(op){ 
-       case 1: 
-         cabeza = Agregarvertice(cabeza); 
-         cont++; 
-         break; 
-       case 2: 
-         Creararco(cabeza); 
-         break; 
-       case 3: 
-         if(cabeza != NULL){ 
-           Mostrargrafo(cabeza); 
-         } 
-         else{ 
-         } 
-         break; 
-       case 4: 
-         if(cabeza == NULL) 
-           printf("\n No hay grafo que evaluar"); 
-         else 
-           Calculargrado(cabeza); 
-         break; 
-       case 5: 
-         if(cabeza != NULL){ 
-           printf("\n Ingrese el vertice que quiere eliminar:\t"); 
-           cabeza = Eliminarvertice(cabeza,nom); 
-         } 
-         else 
-         break; 
-       case 6: 
-         if(cabeza == NULL) 
-         else 
-           Recorrido(cabeza); 
-         break; 
-       default: 
-         break;  
-     } 
-    }while(op!=9); 
-   } 
- } 
-   
- void menu(int *op){ 
-     char    local; 
-     printf("\n1> Ingresar vertice"); 
-     printf("\n4> Mostrar grado de grafo"); 
-     printf("\n5> Eliminar vertice"); 
-     printf("\n6> Mostrar recorrido del grafo"); 
-     printf("\n7> Mostrar arbol de expansion minima"); 
-     printf("\n8> Mostrar arbol de camino mas corto"); 
-     printf("\n   Seleccione una opcion:\t"); 
-     do { 
-         if ((isdigit (- local ) ==-  FALSO ) && (- local  != '\n')){
 
-             printf ("\nIngrese una opcion valida.\n"); 
-             printf("\n1> Ingresar vertice"); 
-             printf("\n4> Mostrar grado de grafo"); 
-             printf("\n5> Eliminar vertice"); 
-             printf("\n6> Mostrar recorrido del grafo"); 
-             printf("\n7> Mostrar arbol de expansion minima"); 
-             printf("\n8> Mostrar arbol de camino mas corto"); 
-             printf("\n   Seleccione una opcion:\t"); 
-             //printf("\t"); 
-         } 
-     } while (isdigit ((unsigned char)-  local ) ==-  FALSO );
 
-         *op = (int) local - '0'; 
- } 
-   
- elemento_grafo *Crearnodo(){ 
-   elemento_grafo *nuevo; 
-   char nom[10]; 
-   printf("\n Ingrese el nombre del vertice:\t"); 
-   //getchar(); 
-   nuevo  = (struct-  grafo *)malloc(sizeof(- elemento_grafo ));
-   nuevo->vertice = NULL; 
-   nuevo->verticead = NULL; 
-   nuevo->vis = 0; 
-   return nuevo; 
- } 
-   
-   
- elemento_grafo *Agregarvertice(elemento_grafo *cabeza){ 
-   elemento_grafo *nuevo,*i; 
-   if (cabeza == NULL){ 
-     nuevo = Crearnodo(); 
-     fputs(- nuevo ->- nombre ,- arch );
 
-     return nuevo; 
-   } 
-   else{ 
-     nuevo = Crearnodo(); 
-     for (i = cabeza; i->vertice != NULL; i=i->vertice); 
-     i->vertice = nuevo; 
-     fputs(- nuevo ->- nombre ,- arch );
 
-     return cabeza; 
-   } 
- } 
-   
- elemento_grafo *buscar(elemento_grafo *cabeza, char nom[10]){ 
-   elemento_grafo *aux,*temp; 
-   //char nom[10]; 
-   int f=0; 
-   //printf("\n Ingrese los nodos:\n    "); 
-   //getchar(); 
-   //fgets(nom,10,stdin); 
-   //printf("\n %s",nom); 
-   temp= NULL; 
-   aux = cabeza; 
-   while(f==0){ 
-     if(strcmp(- aux ->- nombre ,- nom )==0){
 
-       //printf("/ Vertice encontrado"); 
-       temp = aux; 
-       f = 1; 
-     } 
-     else 
-       aux= aux->vertice; 
-     if(aux == NULL) 
-       f = 1; 
-   } 
-   return temp; 
- } 
-   
- elemento_grafo Creararco(elemento_grafo *cabeza){ 
-   elemento_grafo *ori,*des; 
-   elemento_vert *nuevo1,*nuevo2,*i; 
-   char nom[10]; 
-   int f=0; 
-   if(cabeza != NULL){ 
-     printf("\n Ingrese el nodo origen:\t"); 
-     ori = buscar(cabeza,nom); 
-     //printf("\n Si"); 
-     if(ori != NULL){ 
-       printf("\n Ingrese el nodo destino:\t"); 
-       des = buscar(cabeza,nom); 
-       if(des != NULL){ 
-         nuevo1  = (struct-  vertad  *)malloc(sizeof(- elemento_vert ));
-         nuevo1->verticead = NULL; 
-         nuevo1->vis = 1; 
-         nuevo2  = (struct-  vertad  *)malloc(sizeof(- elemento_vert ));
-         nuevo2->verticead = NULL; 
-         nuevo2->vis = 1; 
-         strcpy(- nuevo2 ->- nombre ,- ori ->- nombre );
 
-         strcpy(- nuevo1 ->- nombre ,- des ->- nombre );
 
-         if(ori->verticead == NULL) 
-           ori->verticead = nuevo1; 
-         else{ 
-           for(i = ori->verticead; i->verticead!= NULL; i=i->verticead); 
-           i->verticead = nuevo1; 
-         } 
-         if(des->verticead == NULL) 
-           des->verticead = nuevo2; 
-         else{ 
-           for(i= des->verticead; i->verticead != NULL; i=i->verticead); 
-           i->verticead = nuevo2; 
-         } 
-       } 
-       else{ 
-         printf("\n El nodo destino no existe"); 
-       } 
-     } 
-     else{ 
-       printf("El nodo origen no existe"); 
-     } 
-   } 
-   else{ 
-     printf("\n No hay suficientes vertices para crear arco"); 
-   } 
- } 
-   
- void Mostrargrafo(elemento_grafo *cabeza){ 
-   elemento_grafo *i,*aux; 
-   elemento_vert  *j,*tem; 
-   aux = cabeza; 
-   int f=0,b=0; 
-   while(f==0){ 
-     tem = aux->verticead; 
-     while(tem != NULL){ 
-       tem = tem->verticead; 
-     } 
-   aux = aux->vertice; 
-   if(aux == NULL) 
-     f=1; 
-   } 
- } 
- void Calculargrado(elemento_grafo *cabeza){ 
-   elemento_grafo *aux, *i; 
-   elemento_vert  *tem; 
-   int f=0,cont=0,b=0,grado=0; 
-   aux = cabeza; 
-   while(f == 0){ 
-     cont = 0; 
-     b=0; 
-     tem = aux->verticead; 
-     while(b == 0){ 
-       if(tem == NULL){ 
-         b=1; 
-       } 
-       else{ 
-         cont++; 
-         tem = tem->verticead; 
-       } 
-     } 
-     aux = aux->vertice; 
-     if(cont>=grado){ 
-      grado = cont;} 
-     if(aux == NULL){ 
-       f=1;} 
-   } 
-   printf("\n El grado del grafo es:  %d",- grado );
 
- } 
-   
- elemento_grafo *Eliminarvertice(elemento_grafo *cabeza, char nom[10]){ 
-   elemento_grafo *pos,*aux,*i; 
-   elemento_vert  *tem,*j; 
-   int f=0,b=0; 
-   pos = buscar(cabeza,nom); 
-   if(pos != NULL){ 
-     aux = cabeza; 
-     if(pos == aux){ 
-       aux =aux->vertice; 
-       cabeza = aux; 
-     } 
-     else 
-     if(pos->vertice == NULL){ 
-       while(aux->vertice != pos) 
-         aux = aux->vertice; 
-     aux->vertice = NULL; 
-     } 
-     else{ 
-       while(aux->vertice != pos) 
-         aux = aux->vertice; 
-       i = aux->vertice; 
-       i = i->vertice; 
-       aux->vertice = i; 
-       //return cabeza; 
-     } 
-   } 
-     aux = cabeza; 
-     while(aux != NULL){ 
-       b = 0; 
-       tem = aux->verticead; 
-       if(tem != NULL) 
-         while(tem != NULL){ 
-           if(strcmp(- tem ->- nombre ,- nom )== 0){
 
-             if(tem->verticead == NULL && aux->verticead == tem) 
-               aux->verticead = NULL; 
-             else 
-             if(tem->verticead == NULL){ 
-               j= aux->verticead; 
-               while(j->verticead != NULL) 
-                 j = j->verticead; 
-               j->verticead = NULL; 
-             } 
-             else 
-             if(aux->verticead == tem){ 
-               tem->verticead; 
-               aux->verticead = tem; 
-             } 
-             else 
-             if(tem->verticead == NULL){ 
-               j= aux->verticead; 
-               while(j->verticead != tem) 
-                 j = j->verticead; 
-               tem = tem->verticead; 
-               j->verticead = tem; 
-             } 
-           } 
-           tem = tem->verticead; 
-         } 
-       aux = aux->vertice; 
-     } 
-   return cabeza; 
- } 
-   
- void Recorrido(elemento_grafo *cabeza){ 
-   char nom[10]; 
-   int nodos=cont; 
-   elemento_grafo *aux,*final; 
-   elemento_vert  *tem,*j,*i; 
-   pila *dir,*inicio=NULL; 
-   printf("\n Desde que nodo desea realizar el recorrido:\t"); 
-   aux = buscar(cabeza,nom); 
-   if(aux == NULL){ 
-     printf("\n El vertice dado no existe");} 
-   else{ 
-     int f=0,b=0; 
-     while(f == 0 ){ 
-       if(nodos >= 0){ 
-       tem = aux->verticead; 
-       b = 0; 
-       while(b == 0){ 
-         if(tem->vis == 1) 
-           tem = tem->verticead; 
-         else{ 
-           j = tem; 
-           while(j != NULL){ 
-             if(strcmp(- nom ,- j ->- nombre ) > 0 &&-  j ->- vis  != 1){
 
-               printf("(%s)>(%s)",- nom ,- j ->- nombre );
 
-               i = j; 
-               j = j->verticead;} 
-             else 
-               j = j->verticead; 
-           } 
-            b=1; 
-         } 
-         //tem = tem->verticead; 
-         //if( tem->verticead == NULL || tem == NULL) 
-          //b = 1; 
-       } 
-       visitado(cabeza,nom); 
-       aux = buscar(cabeza,nom); 
-       nodos--; 
-       if(nodos == 0) 
-         f = 1; 
-     } 
-   } 
- } 
-   
-   
- pila *empilar(pila *inicio, elemento_vert *tem , char nom[10]){ 
-   
- } 
-   
- pila *desempilar(pila *inicio, char nom[10]){ 
- } 
-   
- void visitado(elemento_grafo *cabeza, char nom[10]){ 
-   elemento_grafo *aux; 
-   elemento_vert  *tem; 
-   int f=0,b=0; 
-   aux = cabeza; 
-   while( aux != NULL){ 
-     if(strcmp(- nom ,- aux ->- nombre ) == 0){
 
-       aux->vis = 1; 
-       aux = aux->vertice;} 
-     tem = aux->verticead; 
-     while( tem != NULL){ 
-       if(strcmp(- nom ,- tem ->- nombre ) == 0){
 
-         tem->vis = 1; 
-         tem = tem->verticead;} 
-       else 
-         tem = tem->verticead; 
-     } 
-     aux = aux->vertice; 
-   } 
- }