Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct edge{
- int key;
- struct edge *next;
- }edge;
- typedef struct graph{
- int nv;
- edge **adj;
- }graph;
- graph *inizializza_grafo(int n)
- {
- graph *G;
- int i;
- G=(graph *) malloc (sizeof(graph));
- if (G==NULL)//controlla se la memoria è stata allocata
- printf("\nERRORE! Non è possibile allocare memoria\n");
- else
- {
- G->adj=(edge**) malloc (n*sizeof(edge));
- if ((G->adj==NULL) && (n>0))//controlla se la memoria è stata allocata
- {
- printf("\nERRORE! Non è possibile allocare memoria\n");
- free(G);
- G=NULL;//annulla anche l'allocazione di sopra
- }
- else
- {
- G->nv=n;
- for (i=0;i<n;i++)
- G->adj[i]=NULL;//tutti i vertici del grafo puntano a NULL
- }//end else
- }//end else
- return G;
- }
- void stampa(graph *G)
- {
- int i,ne=0;
- edge *e;
- if (G!=NULL)
- {
- printf("\nIl grafo ha %d vertici\n",G->nv);
- for (i=0;i<G->nv;i++)
- {
- e=G->adj[i];
- printf("\n%d",i);
- while (e!=NULL)
- {
- printf(" --> %d ",e->key);
- e=e->next;
- ne++;
- }
- }
- printf("\n\nIl grafo ha %d archi\n",ne);
- }
- }
- int main()
- {
- graph *G,*Int = NULL;
- int n;
- do
- {
- printf("Inserire il num di nodi dei grafi:");
- scanf("%d",&n);
- G=inizializza_grafo(n);
- stampa(Int);
- printf("\n premere 0 per uscire \n");
- scanf("%d",&n);
- }
- while(n!=0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement