제7 장 실험: 그림 의 인접 행렬, 인접 표 표시 와 기본 조작

그림 의 인접 행렬 표시 및 기본 조작
#include
#define MAX_VERTEX_NUM 20//       
#define INFINITY 32768//      
#define ERROR -1

typedef enum{DG,DN,UDG,UDN
}GraphKind;
typedef char VertexData;
typedef char OtherInfo;
typedef struct ArcNode{
	int adj;//     ,      1 0      ;     ,      
	OtherInfo info; 
}ArcNode;
typedef struct{
	VertexData vertex[MAX_VERTEX_NUM];//     
	ArcNode arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//     
	int vexnum,arcnum;//         
	GraphKind kind;
}AdjMatrix;

//          
int LocateVertex(AdjMatrix *G,VertexData v)
{
	int j=ERROR,k;
	for(k=0;kvexnum;k++)
	 if(G->vertex[k]==v)
	 {
	 	j=k;
	 	break;
	 }
	 return(j);
} 

//   (      ) 
void CreateGraph(AdjMatrix *G)
{
	int i,j,k,weight;
	VertexData v1,v2;
	printf("           (     20):");
	scanf("%d %d",&G->vexnum,&G->arcnum);
	//       
	for(i=0;ivexnum;i++)
	 for(j=0;jvexnum;j++)
	  G->arcs[i][j].adj=INFINITY;
	fflush(stdin);
	printf("       (    ):");
	for(i=0;ivexnum;i++)
	 {
	 	scanf("%c",&G->vertex[i]); 
	    //fflush(stdin);
	 }
	 fflush(stdin);
	printf("              (  AB2):
"); for(k=0;kvexnum;k++) { scanf("%c %c %d",&v1,&v2,&weight); i=LocateVertex(G,v1); j=LocateVertex(G,v2); G->arcs[i][j].adj=weight; G->arcs[j][i].adj=weight; fflush(stdin); } } // i VertexData GetVertex(AdjMatrix *G,int i) { if(G->vexnum==0) return(ERROR); if(i>G->vexnum) return(ERROR); return(G->vertex[i-1]); } // v VertexData FirstAdjVertex(AdjMatrix *G,VertexData v) { if(G->vexnum==0||LocateVertex(G,v)==ERROR) return(ERROR); int i,j; i=LocateVertex(G,v); for(j=1;jvexnum;j++) { if(G->arcs[i][j].adj!=INFINITY) return(G->vertex[i]); } return(ERROR); } // v v VertexData NextAdjVertex(AdjMatrix *G,VertexData v,VertexData w) { if(G->vexnum==0||LocateVertex(G,v)==ERROR) return(ERROR); int i,j,k; i=LocateVertex(G,v); j=LocateVertex(G,w); for(k=j+1;kvexnum;k++) { if(G->arcs[i][k].adj!=INFINITY) return(G->vertex[k]); } return(ERROR); } // void print(AdjMatrix *G) { int i,j; for(i=0;ivexnum;i++) for(j=0;jvexnum;j++) { printf("%10d",G->arcs[i][j].adj); if((j+1)%G->vexnum==0) printf("
"); } } // int visited[MAX_VERTEX_NUM]={0}; // void DeepthFirstSearch(AdjMatrix G,int v0) { printf("%c ",G.vertex[v0]); visited[v0]=1; int vj; for(vj=0;vj
#include
#define MAX_VERTEX_NUM 20//       
#define ERROR -1

typedef char VertexData;
//         
typedef struct ArcNode{
	int adj;//          
	struct ArcNode *nextarc;//          
}ArcNode;
//         
typedef struct VertexNode{
	VertexData data;
	ArcNode *firstarc;//            ; 
	int order;
}VertexNode;
typedef struct{
	VertexNode vertex[MAX_VERTEX_NUM];
	int vexnum,arcnum;
}AdjList;

int LocateVertex(AdjList *G,VertexData v)
{
	int j=ERROR,k;
	for(k=1;kvexnum;k++)
	 if(G->vertex[k].data==v)
	 {
	 	j=G->vertex[k].order;
	 	break;
	 }
	 return(j);
} 

void CreateGraph(AdjList *G)
{
	printf("           (     20):");
	scanf("%d %d",&G->vexnum,&G->arcnum);
	printf("     :");
	int i,j,k;
	char v1,v2;
	for(i=0;ivexnum;i++)
	{
	 	scanf("%c",&(G->vertex[i].data)); 
	 	G->vertex[i].order=i+1;
	 	
	}
	printf("           (  AB):
"); for(k=0;kvexnum;k++) { scanf("%c %c",&v1,&v2); ArcNode *p,*q; p=(ArcNode*)malloc(sizeof(ArcNode)); q=(ArcNode*)malloc(sizeof(ArcNode)); p->nextarc=NULL; q->nextarc=NULL; i=LocateVertex(G,v1); j=LocateVertex(G,v2); G->vertex[i].firstarc=p; G->vertex[j].firstarc=q; fflush(stdin); } } void print(AdjList *G) { int i,j; for(i=0;ivexnum;i++) { printf("%c ",G->vertex[i].data); ArcNode *p; p=G->vertex[i].firstarc; while(p!=NULL) { printf("%d ",p->adj); p=p->nextarc; } } } void main() { AdjList G; CreateGraph(&G); print(&G); }

좋은 웹페이지 즐겨찾기