Graphtest.h
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/*
c1.h (程序名)
*/
#include < string .h >
#include < ctype.h >
#include < malloc.h > /* malloc()等 */
#include < limits.h > /* INT_MAX等 */
#include < stdio.h > /* EOF(=^Z或F6),NULL */
#include < stdlib.h > /* atoi() */
#include < io.h > /* eof() */
#include < math.h > /* floor(),ceil(),abs() */
#include < process.h > /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
#define MAX_NAME 5 /*顶点字符串的最大长度+1 */
typedef int VRType;
typedef char InfoType;
typedef char VertexType[MAX_NAME];
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
typedef enum {DG,DN,AG,AN}GraphKind;
typedef struct ArcCell{
VRType adj; // 顶点关系类型
InfoType * info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
#include < string .h >
#include < ctype.h >
#include < malloc.h > /* malloc()等 */
#include < limits.h > /* INT_MAX等 */
#include < stdio.h > /* EOF(=^Z或F6),NULL */
#include < stdlib.h > /* atoi() */
#include < io.h > /* eof() */
#include < math.h > /* floor(),ceil(),abs() */
#include < process.h > /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
#define MAX_NAME 5 /*顶点字符串的最大长度+1 */
typedef int VRType;
typedef char InfoType;
typedef char VertexType[MAX_NAME];
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
typedef enum {DG,DN,AG,AN}GraphKind;
typedef struct ArcCell{
VRType adj; // 顶点关系类型
InfoType * info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
fun.h
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
Status LocateVex(MGraph G,VertexType u);
Status CreateFAG(MGraph * G);
void Display(MGraph G);
Status Visit(VertexType v);
int FirstAdjVex(MGraph G, int v);
int NextAdjVex(MGraph G, int v, int w);
Status Visit(VertexType v);
void DFSTraverse(MGraph G,Status ( * Visit)(VertexType v));
void DFS(MGraph G, int v);
void BFSTraverse(MGraph G, Status( * Visit)(VertexType v));
Status CreateFAG(MGraph * G);
void Display(MGraph G);
Status Visit(VertexType v);
int FirstAdjVex(MGraph G, int v);
int NextAdjVex(MGraph G, int v, int w);
Status Visit(VertexType v);
void DFSTraverse(MGraph G,Status ( * Visit)(VertexType v));
void DFS(MGraph G, int v);
void BFSTraverse(MGraph G, Status( * Visit)(VertexType v));
Queue.h
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
typedef
int
QElemType;
typedef struct QNode
{
QElemType data;
struct QNode * next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
Status InitQueue(LinkQueue * Q)
{
( * Q).front = ( * Q).rear = (QueuePtr)malloc( sizeof (QNode));
if ( ! ( * Q).front)
exit(OVERFLOW);
( * Q).front -> next = NULL;
return OK;
}
Status EnQueue(LinkQueue * Q,QElemType e)
{
QueuePtr p = (QueuePtr)malloc( sizeof (QNode));
if ( ! p)
exit(OVERFLOW);
p -> data = e;
p -> next = NULL;
( * Q).rear -> next = p;
( * Q).rear = p;
return OK;
}
Status DeQueue(LinkQueue * Q,QElemType * e)
{
QueuePtr p;
if (( * Q).front == ( * Q).rear)
return ERROR;
p = ( * Q).front -> next;
* e = p -> data;
( * Q).front -> next = p -> next;
if (( * Q).rear == p)
( * Q).rear = ( * Q).front;
free(p);
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
typedef struct QNode
{
QElemType data;
struct QNode * next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
Status InitQueue(LinkQueue * Q)
{
( * Q).front = ( * Q).rear = (QueuePtr)malloc( sizeof (QNode));
if ( ! ( * Q).front)
exit(OVERFLOW);
( * Q).front -> next = NULL;
return OK;
}
Status EnQueue(LinkQueue * Q,QElemType e)
{
QueuePtr p = (QueuePtr)malloc( sizeof (QNode));
if ( ! p)
exit(OVERFLOW);
p -> data = e;
p -> next = NULL;
( * Q).rear -> next = p;
( * Q).rear = p;
return OK;
}
Status DeQueue(LinkQueue * Q,QElemType * e)
{
QueuePtr p;
if (( * Q).front == ( * Q).rear)
return ERROR;
p = ( * Q).front -> next;
* e = p -> data;
( * Q).front -> next = p -> next;
if (( * Q).rear == p)
( * Q).rear = ( * Q).front;
free(p);
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
Traverse.cpp
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//
#include "Graphtest.h"
#include " stdafx.h "
#include " Graphtest.h "
#include " fun.h "
Boolean visited[MAX_VERTEX_NUM];
Status ( * VisitFunc)(VertexType v);
int LocateVex(MGraph G,VertexType u)
{
int i;
for (i = 0 ;i < G.vexnum;i ++ )
if (strcmp(u,G.vexs[i]) == 0 )
return i;
return - 1 ;
}
Status CreateFAG(MGraph * G)
{
int i,j,k;
char filename[ 13 ];
VertexType va,vb;
FILE * graphlist;
printf( " please input data filename(f7-1.dat):\n " );
scanf( " %s " ,filename);
graphlist = fopen(filename, " r " );
fscanf(graphlist, " %d " , & ( * G).vexnum);
fscanf(graphlist, " %d " , & ( * G).arcnum);
for (i = 0 ;i < ( * G).vexnum;i ++ )
{
fscanf(graphlist, " %s " ,( * G).vexs[i]);
printf( " %s\t " ,( * G).vexs[i]);
}
for (i = 0 ;i < ( * G).vexnum;i ++ )
for (j = 0 ;j < ( * G).vexnum;j ++ )
{
( * G).arcs[i][j].adj = 0 ;
( * G).arcs[i][j].info = NULL;
}
for (k = 0 ;k < ( * G).arcnum;k ++ )
{
fscanf(graphlist, " %s%s " ,va,vb);
i = LocateVex( * G,va);
j = LocateVex( * G,vb);
( * G).arcs[i][j].adj = ( * G).arcs[j][i].adj = 1 ;
}
fclose(graphlist);
( * G).kind = AG;
return OK;
}
void Display(MGraph G)
{
int i,j;
char s[ 7 ],s1[ 3 ];
switch (G.kind)
{
case DG:strcpy(s, " 有向图\0 " );
strcpy(s1, " 弧\0 " );
break ;
case DN: strcpy(s, " 有向网\0 " );
strcpy(s1, " 弧\0 " );
break ;
case AG: strcpy(s, " 无向图\0 " );
strcpy(s1, " 边\0 " );
break ;
case AN: strcpy(s, " 无向网\0 " );
strcpy(s1, " 边\0 " );
}
printf( " %d个顶点%d条%s的%s\n " ,G.vexnum,G.arcnum,s1,s);
for (i = 0 ;i < G.vexnum; ++ i)
printf( " G.vexs[%d]=%s\n " ,i,G.vexs[i]);
printf( " G.arcs.adj:\n " );
for (i = 0 ;i < G.vexnum;i ++ )
{
for (j = 0 ;j < G.vexnum;j ++ )
printf( " %6d " ,G.arcs[i][j].adj);
printf( " \n " );
}
printf( " G.arcs.info:\n " );
printf( " 顶点1(弧尾) 顶点2(弧头) 该%s信息:\n " ,s1);
if (G.kind < 2 )
for (i = 0 ;i < G.vexnum;i ++ )
for (j = 0 ;j < G.vexnum;j ++ )
{
if (G.arcs[i][j].info)
printf( " %5s %11s %s\n " ,G.vexs[i],G.vexs[j],G.arcs[i][j].info);
}
else
{
for (i = 0 ;i < G.vexnum;i ++ )
for (j = i + 1 ;j < G.vexnum;j ++ )
if (G.arcs[i][j].info)
printf( " %5s %11s %s\n " ,G.vexs[i],G.vexs[j],G.arcs[i][j].info);
}
}
int FirstAdjVex(MGraph G, int v)
{
int i;
for (i = 0 ;i < G.vexnum;i ++ )
if (G.arcs[v][i].adj != 0 )
return i;
return - 1 ;
}
int NextAdjVex(MGraph G, int v, int w)
{
int i;
for (i = w + 1 ;i < G.vexnum;i ++ )
if (G.arcs[v][i].adj != 0 )
return i;
return - 1 ;
}
Status Visit(VertexType v)
{
printf( " visit %s\n " ,v);
return OK;
}
void DFSTraverse(MGraph G,Status ( * Visit)(VertexType v))
{
int v;
VisitFunc = Visit;
for (v = 0 ;v < G.vexnum;v ++ )
visited[v] = FALSE;
for (v = 0 ;v < G.vexnum;v ++ )
if ( ! visited[v])
DFS(G,v);
}
void DFS(MGraph G, int v)
{
int w;
visited[v] = true ;
VisitFunc(G.vexs[v]);
// Visit(G.vexs[v]);
for (w = FirstAdjVex(G,v);w >= 0 ;w = NextAdjVex(G,v,w))
if ( ! visited[w])
DFS(G,w);
}
#include " Queue.h "
void BFSTraverse(MGraph G, Status( * Visit)(VertexType v))
{
int v,u,w;
LinkQueue Q;
for (v = 0 ;v < G.vexnum;v ++ )
visited[v] = FALSE;
InitQueue( & Q);
for (v = 0 ;v < G.vexnum;v ++ )
{
if ( ! visited[v])
{
visited[v] = TRUE;
Visit(G.vexs[v]);
EnQueue( & Q,v);
while ( ! QueueEmpty(Q))
{
DeQueue( & Q, & u);
for (w = FirstAdjVex(G,u);w >= 0 ;w = NextAdjVex(G,u,w))
{
if ( ! visited[w])
{
visited[w] = true ;
Visit(G.vexs[w]);
EnQueue( & Q,w);
}
}
}
}
}
}
#include " stdafx.h "
#include " Graphtest.h "
#include " fun.h "
Boolean visited[MAX_VERTEX_NUM];
Status ( * VisitFunc)(VertexType v);
int LocateVex(MGraph G,VertexType u)
{
int i;
for (i = 0 ;i < G.vexnum;i ++ )
if (strcmp(u,G.vexs[i]) == 0 )
return i;
return - 1 ;
}
Status CreateFAG(MGraph * G)
{
int i,j,k;
char filename[ 13 ];
VertexType va,vb;
FILE * graphlist;
printf( " please input data filename(f7-1.dat):\n " );
scanf( " %s " ,filename);
graphlist = fopen(filename, " r " );
fscanf(graphlist, " %d " , & ( * G).vexnum);
fscanf(graphlist, " %d " , & ( * G).arcnum);
for (i = 0 ;i < ( * G).vexnum;i ++ )
{
fscanf(graphlist, " %s " ,( * G).vexs[i]);
printf( " %s\t " ,( * G).vexs[i]);
}
for (i = 0 ;i < ( * G).vexnum;i ++ )
for (j = 0 ;j < ( * G).vexnum;j ++ )
{
( * G).arcs[i][j].adj = 0 ;
( * G).arcs[i][j].info = NULL;
}
for (k = 0 ;k < ( * G).arcnum;k ++ )
{
fscanf(graphlist, " %s%s " ,va,vb);
i = LocateVex( * G,va);
j = LocateVex( * G,vb);
( * G).arcs[i][j].adj = ( * G).arcs[j][i].adj = 1 ;
}
fclose(graphlist);
( * G).kind = AG;
return OK;
}
void Display(MGraph G)
{
int i,j;
char s[ 7 ],s1[ 3 ];
switch (G.kind)
{
case DG:strcpy(s, " 有向图\0 " );
strcpy(s1, " 弧\0 " );
break ;
case DN: strcpy(s, " 有向网\0 " );
strcpy(s1, " 弧\0 " );
break ;
case AG: strcpy(s, " 无向图\0 " );
strcpy(s1, " 边\0 " );
break ;
case AN: strcpy(s, " 无向网\0 " );
strcpy(s1, " 边\0 " );
}
printf( " %d个顶点%d条%s的%s\n " ,G.vexnum,G.arcnum,s1,s);
for (i = 0 ;i < G.vexnum; ++ i)
printf( " G.vexs[%d]=%s\n " ,i,G.vexs[i]);
printf( " G.arcs.adj:\n " );
for (i = 0 ;i < G.vexnum;i ++ )
{
for (j = 0 ;j < G.vexnum;j ++ )
printf( " %6d " ,G.arcs[i][j].adj);
printf( " \n " );
}
printf( " G.arcs.info:\n " );
printf( " 顶点1(弧尾) 顶点2(弧头) 该%s信息:\n " ,s1);
if (G.kind < 2 )
for (i = 0 ;i < G.vexnum;i ++ )
for (j = 0 ;j < G.vexnum;j ++ )
{
if (G.arcs[i][j].info)
printf( " %5s %11s %s\n " ,G.vexs[i],G.vexs[j],G.arcs[i][j].info);
}
else
{
for (i = 0 ;i < G.vexnum;i ++ )
for (j = i + 1 ;j < G.vexnum;j ++ )
if (G.arcs[i][j].info)
printf( " %5s %11s %s\n " ,G.vexs[i],G.vexs[j],G.arcs[i][j].info);
}
}
int FirstAdjVex(MGraph G, int v)
{
int i;
for (i = 0 ;i < G.vexnum;i ++ )
if (G.arcs[v][i].adj != 0 )
return i;
return - 1 ;
}
int NextAdjVex(MGraph G, int v, int w)
{
int i;
for (i = w + 1 ;i < G.vexnum;i ++ )
if (G.arcs[v][i].adj != 0 )
return i;
return - 1 ;
}
Status Visit(VertexType v)
{
printf( " visit %s\n " ,v);
return OK;
}
void DFSTraverse(MGraph G,Status ( * Visit)(VertexType v))
{
int v;
VisitFunc = Visit;
for (v = 0 ;v < G.vexnum;v ++ )
visited[v] = FALSE;
for (v = 0 ;v < G.vexnum;v ++ )
if ( ! visited[v])
DFS(G,v);
}
void DFS(MGraph G, int v)
{
int w;
visited[v] = true ;
VisitFunc(G.vexs[v]);
// Visit(G.vexs[v]);
for (w = FirstAdjVex(G,v);w >= 0 ;w = NextAdjVex(G,v,w))
if ( ! visited[w])
DFS(G,w);
}
#include " Queue.h "
void BFSTraverse(MGraph G, Status( * Visit)(VertexType v))
{
int v,u,w;
LinkQueue Q;
for (v = 0 ;v < G.vexnum;v ++ )
visited[v] = FALSE;
InitQueue( & Q);
for (v = 0 ;v < G.vexnum;v ++ )
{
if ( ! visited[v])
{
visited[v] = TRUE;
Visit(G.vexs[v]);
EnQueue( & Q,v);
while ( ! QueueEmpty(Q))
{
DeQueue( & Q, & u);
for (w = FirstAdjVex(G,u);w >= 0 ;w = NextAdjVex(G,u,w))
{
if ( ! visited[w])
{
visited[w] = true ;
Visit(G.vexs[w]);
EnQueue( & Q,w);
}
}
}
}
}
}
Graphtest.cpp
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include
"
stdafx.h
"
#include " Graphtest.h "
#include " fun.h "
int _tmain( int argc, _TCHAR * argv[])
{
MGraph g;
CreateFAG( & g);
Display(g);
printf( " now DFS the Graph:\n " );
DFSTraverse(g,Visit);
printf( " now BFS the Graph:\n " );
BFSTraverse(g,Visit);
while ( 1 );
return 0 ;
}
#include " Graphtest.h "
#include " fun.h "
int _tmain( int argc, _TCHAR * argv[])
{
MGraph g;
CreateFAG( & g);
Display(g);
printf( " now DFS the Graph:\n " );
DFSTraverse(g,Visit);
printf( " now BFS the Graph:\n " );
BFSTraverse(g,Visit);
while ( 1 );
return 0 ;
}