viernes, 21 de octubre de 2016

Código para Grafo

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct nodo{
            char nombre[2];//nombre del vertice o nodo
            struct nodo *sgte;
            struct arista *ady;//puntero hacia la primera arista del nodo
            };
struct nodo *primero=NULL, *ultimo=NULL;
struct arista{
              struct nodo *destino;//puntero al nodo de llegada
              struct arista *sgte;
              };
void insertar_nodo();
void insertar_arista();
void vaciar_aristas();
void eliminar_nodo();
void eliminar_arista();
void mostrar_grafo();
void mostrar_aristas();
void visualizar();

main  (){
 int opcion;
 do{

  printf("\n\n\n");
  printf("\n****************************************");
  printf("\n*******************GRAFO****************");
  printf("\n\n");
  printf("\n1. Insertar un nodo");
  printf("\n2. Insertar un arista");
  printf("\n3. Eliminar un nodo");
  printf("\n4. Eliminar una arista");
  printf("\n5. Mostrar grafo");
  printf("\n6. Mostrar aristas de un nodo");
  printf("\n7. Salir");
  printf("\n8. visualizar nodo");
  printf("\n****************************************\n");
  printf("Ingresar opcion");
  scanf("%d", &opcion);

 
     switch(opcion){
    case 1: insertar_nodo();
                    break;
            case 2: insertar_arista();
                    break;
            case 3: eliminar_nodo();
                    break;
            case 4: eliminar_arista();
                    break;
            case 5: mostrar_grafo();
                    break;
            case 6: mostrar_aristas();
                    break;
            case 7: printf("Salir");
break;
case 8: visualizar ();
break;
default: system("pause");
break;
 
   }
  }
   while (opcion !=8);
}
void insertar_nodo(){
struct nodo *nuevo;
nuevo=(struct nodo*)malloc(sizeof(struct nodo));
if (nuevo == NULL)
printf("\n Memoria insuficiente");
else{
printf("\nINGRESE NODO: ");
    scanf("%s", nuevo->nombre);
       nuevo->sgte = NULL;
    nuevo->ady=NULL;}
 
    if(primero==NULL)
     {
        printf("PRIMER NODO...!!!");
primero = nuevo;
        ultimo = nuevo;
       
      }
    else
     {
      nuevo->sgte=NULL;
ultimo->sgte=nuevo;
ultimo=nuevo;
           while(ultimo->sgte!=NULL)
         {
            ultimo = ultimo->sgte;
          }
     
        printf("NODO INGRESADO...!!!");
      }

 }

 void visualizar(){

struct nodo *actual;
actual=primero;
if(actual==NULL){
printf("\nGrafo vacio");}
else{

while(actual!=NULL){
printf("%s\n",actual->nombre);
actual=actual->sgte;
}}
printf("\n\n\n");
system("pause");
}


/*INSERTAR ARISTA---------------------------------------------------------------------*/
void insertar_arista()
{   char ini[2], fin[2];
    struct arista *nuevo, *q;
    struct nodo *aux, *aux2;
 nuevo=(struct arista*)malloc(sizeof(struct arista));
    if(primero==NULL)
     {
         printf("GRAFO VACIO...!!!!");
         return;
     }else{

    nuevo->sgte=NULL;
    printf("\nINGRESE NODO DE INICIO:");
    scanf("%s", &ini);
    printf("\nINGRESE NODO FINAL:");
    scanf("%s", &fin);
    aux=primero;
    aux2=primero;
    }
    while(aux2!=NULL){
    if(strcmp(fin, aux2->nombre)==0)
        {
          break;
        }else{
         aux2=aux2->sgte;}
    }
    while(aux!=NULL)
    {
   
        if(strcmp(ini, aux->nombre)==0)
        {
    if(aux->ady==NULL)
    {   aux->ady=nuevo;
        nuevo->destino=aux2;
        printf("PRIMERA ARISTA....!");
}
    else
    {  
q=aux->ady;
  while(q->sgte!=NULL)
            q=q->sgte;
        nuevo->destino=aux2;
        q->sgte=nuevo;
        printf("ARISTA AGREGADA...!!!!");
             
    }
    return;
        }

        aux = aux->sgte;
    }
}
 
void eliminar_nodo(){
char var[2];
    struct nodo *aux,*ant;
    aux=primero;
    printf("ELIMINAR UN NODO\n");
    if(primero==NULL)
     {
         printf("GRAFO VACIO...!!!!");
         return;
     }
    printf("INGRESE NOMBRE DE VARIABLE:");
    scanf("%s",var);

    while(aux!=NULL)
    {
        if(strcmp(var, aux->nombre)==0)
        {
            if(aux->ady!=NULL)
                vaciar_aristas();

            if(aux==primero)
            {

                    primero=primero->sgte;
                    free(aux);
                    printf("NODO ELIMINADO...!!!!");
                    return;
             }
            else
            {
                ant->sgte = aux->sgte;
                free(aux);
                printf("NODO ELIMINADO...!!!!");
                return;
            }
        }
        else
        {
            ant=aux;
            aux=aux->sgte;
         }
    }
}
void eliminar_arista(){
char ini[2], fin[2];
    struct nodo *aux, *aux2;
    struct arista *q,*r;
    printf("\n ELIMINAR ARISTA\n");
    printf("INGRESE NODO DE INICIO:");
    scanf("%s",ini);
    printf("INGRESE NODO FINAL:");
    scanf("%s", fin);
    aux=primero;
    aux2=primero;
    while(aux2!=NULL)
    {
        if(strcmp(fin, aux2->nombre)==0)
        {
            break;
        }
        else
        aux2=aux2->sgte;
    }
     while(aux!=NULL)
    {
        if(strcmp(ini, aux->nombre)==0)
        {
            q=aux->ady;
            while(q!=NULL)
            {
                if(q->destino==aux2)
                {
                    if(q==aux->ady)
                        aux->ady=aux->ady->sgte;
                    else
                        r->sgte=q->sgte;
                    free(q);
                    printf("ARISTA  ", aux->nombre, "----->", aux2->nombre, " ELIMINADA.....!!!!");
                    return;
                }
            }
            r=q;
            q=q->sgte;
        }
        aux = aux->sgte;
    }
}
void mostrar_grafo(){
struct nodo *ap;
    struct arista *ca;
    ap=primero;
    printf("NODO|LISTA DE ADYACENCIA\n");
   while(ap!=NULL){
printf("%s| ", ap->nombre);
if(ap->ady!=NULL)
        {
ca=ap->ady;
while(ca!=NULL)
            {  
printf("%s ",ca->destino->nombre);
                ca=ca->sgte; }        }
        ap=ap->sgte;
        printf("\n");}
}
void mostrar_aristas(){
struct nodo *aux;
    struct arista *ar;
    char var[2];
    printf("MOSTRAR ARISTAS DE NODO\n");
    printf("INGRESE NODO:");
    scanf("%s",var);
  aux=primero;
    while(aux!=NULL)
    {
        if(strcmp(var, aux->nombre)==0)
        {
            if(aux->ady==NULL)
            {   printf("EL NODO NO TIENE ARISTAS...!!!!");
                return;
             }
            else
            {
                printf("NODO|LISTA DE ADYACENCIA\n");
                printf("%s  | ",aux->nombre);
                ar=aux->ady;
                while(ar!=NULL)
                {
                    printf(ar->destino->nombre,"  ");
                    ar=ar->sgte;
                                  }
             
                return;
            }
        }
        else
        aux=aux->sgte;
    }
}
void vaciar_aristas(){
struct arista *q,*r;
struct nodo *aux;
aux=primero;
    q=aux->ady;
    while(q->sgte!=NULL)
    {
        r=q;
        q=q->sgte;
        free(r);
    }

No hay comentarios:

Publicar un comentario