www.pudn.com > Bird4.rar > Bird4.c


#include  
#include  
 
 struct arcnode 
 { 
   int adjvex; 
   struct arcnode *nextarc; 
 }; 
 
 struct vnode 
 { 
   int data,color,degree; 
   struct arcnode *firstarc; 
  }; 
 
 struct graph 
  { 
   struct vnode list[50]; 
   int vexnum,arcnum; 
   }*algraph; 
 
creat(struct graph *g) 
{ 
      int i,j,k,v1,v2; 
      struct arcnode *p; 
 
      printf("Input the number of the vertex \n"); 
      scanf("%d",&g->vexnum); 
      printf("Input the number of the arc\n"); 
      scanf("%d",&g->arcnum); 
 
       for(i=0;ivexnum;i++) 
	  { 
	    printf(" Vertex %d:",i+1); 
	    scanf("%d",&g->list[i].data); 
	    g->list[i].color=1; 
	    g->list[i].degree=0; 
	    g->list[i].firstarc=NULL; 
	    } 
 
      for(k=0;karcnum;k++) 
	  { 
	    printf(" Arc %d (tail head):",k+1); 
	    scanf("%d %d",&v1,&v2); 
	    for(i=0;ivexnum;i++) 
	     if(g->list[i].data==v1) 
	      { 
		g->list[i].degree++; 
		break; 
		  } 
 
	   for(j=0;jvexnum;j++) 
	    if(g->list[j].data==v2) 
	     { 
	       g->list[j].degree++; 
	       break; 
	       } 
 
     p=(struct arcnode*)malloc(sizeof(struct arcnode)); 
     p->adjvex=j; 
     p->nextarc=g->list[i].firstarc; 
     g->list[i].firstarc=p; 
 
     p=(struct arcnode*)malloc(sizeof(struct arcnode)); 
     p->adjvex=i; 
     p->nextarc=g->list[j].firstarc; 
     g->list[j].firstarc=p; 
   } 
} 
 
 color(struct graph *g) 
 { 
    int i,j,c,max; 
    struct arcnode *p; 
    c=0; 
    j=0; 
 
   while(cvexnum) 
    { 
      for(i=0,max=0;ivexnum;i++) 
	if(maxlist[i].degree) 
	 { 
	   max=g->list[i].degree; 
	   j=i; 
	   } 
	 if(max==0) break; 
 
     g->list[j].degree=0; 
     p=g->list[j].firstarc; 
 
     while(p!=NULL) 
      { 
	  (g->list[p->adjvex].degree)--; 
 
	   if(g->list[p->adjvex].color==g->list[j].color) 
	      (g->list[p->adjvex].color)++; 
	   p=p->nextarc; 
      }/*end while*/ 
 
      c++; 
 } 
} 
 
 
watch(struct graph *algraph ) 
{ 
    int i; 
    printf("number\tdata\tcolor\n"); 
     for(i=1;i<=algraph->vexnum;i++) 
       { 
	 printf("%d\t%d\t",i,algraph->list[i-1].data); 
	 switch(algraph->list[i-1].color) 
	 {  case 1: printf("RED\n");  break; 
	    case 2: printf("YELLOW\n");  break; 
	    case 3: printf("BLUE\n");  break; 
	    case 4: printf("GREEN\n");  break; 
	  } 
       } 
} 
 
main() 
{ 
  int sel; 
 
  while (1) /*ÉèÖò˵¥*/ 
  { clrscr(); /*ÇåÆÁ*/ 
    printf("1. Intput  a new graph.\n"); 
    printf("2. Exit\n"); 
    printf("please input a number(1/2)?"); 
    scanf("%d",&sel); 
    switch(sel) 
    { case 1: clrscr(); 
	      printf("Input a new graph.\n"); 
	      printf("create a graph.\n"); 
	      algraph=(struct graph*)malloc(sizeof(struct graph)); 
 
	      creat(algraph); 
	      color(algraph); 
	      watch(algraph); 
	      getch(); 
	      free(algraph); 
	      break; 
 
      case 2: 
              break; 
 
      } 
      if (sel==2) break; 
      printf("Press any continue..\n"); 
      getch(); 
   } 
 
}