1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <stdio.h>
#include<stdlib.h>
#define N 5
struct element{
int numSommet;
struct element *suivant;
};
typedef struct element * liste;
void creerSommet(liste ls[]){
liste tmp;
int i;
for (i=0;i<N;i++){
tmp=malloc(sizeof(struct element));
tmp->numSommet=i;
tmp->suivant=NULL;
ls[i]=tmp;
}
}
void ajouterArc(liste ls[],int ori,int desti){
liste tmp,ptr;
tmp=ls[ori];
ptr=malloc(sizeof(struct element));
ptr->numSommet=desti;
ptr->suivant=tmp->suivant;
tmp->suivant=ptr;
}
void supprimerArc(liste ls[],int ori,int desti){
liste tmp,tmp2;
tmp=ls[ori];
if (tmp->suivant!=NULL){
while(tmp->numSommet!=desti){
tmp=tmp->suivant;
}
tmp2=tmp;
tmp=tmp->suivant;
free(tmp2);
}
}
void afficherGraphe(liste ls[]){
int i;
liste tmp;
for(i=0;i<N;i++){
tmp=ls[i];
while(tmp!=NULL){
printf("%d ",tmp->numSommet);
tmp=tmp->suivant;
}
printf("\n");
}
}
int main(){
liste ls[N];
creerSommet(ls);
ajouterArc(ls,1,2);
ajouterArc(ls,1,3);
ajouterArc(ls,2,4);
ajouterArc(ls,4,1);
ajouterArc(ls,4,0);
ajouterArc(ls,0,0);
ajouterArc(ls,0,3);
afficherGraphe(ls);
printf("--------------------\n");
supprimerArc(ls,1,3);
afficherGraphe(ls);
return 1;
} |
Partager