NicolaDelPrete

Grafo con matrice e grafi

May 29th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. typedef struct grafo
  6. {
  7.     char *vett;
  8.     int *mat;
  9. }grafo;
  10. typedef grafo *pgrafo;
  11. void genera_vettore(pgrafo *k,int n)
  12. {
  13.     (*k)->vett=calloc(n,sizeof(char));
  14.     int i;
  15.     for(i=0;i<n;i++)
  16.     {
  17.         printf("Inserire il %d elemento: ",i+1);
  18.         fflush(stdin);
  19.         scanf("%c",&(*k)->vett[i]);
  20.     }
  21. }
  22. void genera_grafo(pgrafo *k,int n)
  23. {
  24.     (*k)->mat=calloc(n*n,sizeof(int));
  25.     int i,j;
  26.     for(i=0;i<n;i++)
  27.     {
  28.         for(j=0;j<n;j++)
  29.         {
  30.             printf("Il nodo %c \x82 collegato a %c ? [Inserire 1 se si | 0 se no ]",(*k)->vett[i],(*k)->vett[j]);
  31.             scanf("%d",&(*k)->mat[i*n+j]);
  32.         }
  33.     }
  34. }
  35. void stampa (pgrafo k,int n)
  36. {
  37.     int i,j;
  38.     for(i=0;i<n;i++)
  39.     {
  40.         printf("\n %c",k->vett[i]);
  41.         for(j=0;j<n;j++)
  42.         {
  43.             if(k->mat[i*n+j]>0)
  44.                 printf("->%c",k->vett[j]);
  45.         }
  46.     }
  47.     printf("->NULL");
  48. }
  49. int main()
  50. {
  51.     pgrafo k;
  52.     int n;
  53.     printf("Inserire il numero di nodi: ");
  54.     scanf("%d",&n);
  55.     k=calloc(1,sizeof(grafo));
  56.     genera_vettore(&k,n);
  57.     genera_grafo(&k,n);
  58.     stampa(k,n);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment