源码+注释
# include <stdio.h>
# include <malloc.h>
# define MAXSIZE 1000
# define MAX_AMVNUMS 100
typedef enum {
FALSE,
TRUE
} Boolean;
typedef struct {
int * base;
int front;
int rear;
} SqQueue;
typedef struct {
char verxs[ MAX_AMVNUMS] ;
int arcs[ MAX_AMVNUMS] [ MAX_AMVNUMS] ;
int numVertexes, numEdges;
} AMGraph;
Boolean visited[ MAX_AMVNUMS] ;
void CreateAMGraph ( AMGraph * G) ;
void PrintAMatrix ( AMGraph G) ;
void DFSTraverse ( AMGraph G, int v) ;
void DFS_AM ( AMGraph G, int v) ;
void BFSTraverse ( AMGraph G, int v) ;
void BFS_AM ( AMGraph G, int v) ;
Boolean InitQueue ( SqQueue * queue) ;
Boolean EnQueue ( SqQueue * queue, int elem) ;
Boolean DeQueue ( SqQueue * queue, int * elem) ;
Boolean IsFull ( SqQueue * queue) ;
Boolean IsEmpty ( SqQueue * queue) ;
int main ( ) {
AMGraph * G;
G = ( AMGraph * ) malloc ( sizeof ( AMGraph) ) ;
CreateAMGraph ( G) ;
PrintAMatrix ( * G) ;
DFSTraverse ( * G, 1 ) ;
printf ( "\n" ) ;
BFSTraverse ( * G, 3 ) ;
getchar ( ) ;
}
void CreateAMGraph ( AMGraph * G) {
printf ( "输入无向图的顶点数和边数,用空格分开:" ) ;
scanf ( "%d %d" , & ( G-> numVertexes) , & ( G-> numEdges) ) ;
getchar ( ) ;
for ( int i = 0 ; i < G-> numVertexes; i++ ) {
G-> verxs[ i] = i;
}
for ( int i = 0 ; i < G-> numVertexes; i++ ) {
for ( int j = 0 ; j < G-> numVertexes; j++ ) {
G-> arcs[ i] [ j] = 0 ;
}
}
int arrSub1[ ] = { 0 , 0 , 1 , 1 , 2 , 2 , 3 , 4 , 5 } ;
int arrSub2[ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 7 , 6 } ;
for ( int i = 0 ; i < G-> numEdges; i++ ) {
G-> arcs[ arrSub1[ i] ] [ arrSub2[ i] ] = G-> arcs[ arrSub2[ i] ] [ arrSub1[ i] ] = 1 ;
}
}
void PrintAMatrix ( AMGraph G) {
for ( int i = 0 ; i < G. numVertexes; i++ ) {
for ( int j = 0 ; j < G. numVertexes; j++ ) {
printf ( "%d " , G. arcs[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
}
void DFSTraverse ( AMGraph G, int v) {
for ( int i = 0 ; i < G. numVertexes; i++ ) {
visited[ i] = FALSE;
}
for ( int i = 0 ; i < G. numVertexes; i++ ) {
if ( ! visited[ i] ) {
DFS_AM ( G, v) ;
}
}
}
void DFS_AM ( AMGraph G, int v) {
printf ( "V%d " , v) ;
visited[ v] = TRUE;
for ( int i = 0 ; i < G. numVertexes; i++ ) {
if ( ( G. arcs[ v] [ i] != 0 ) && ( ! visited[ i] ) ) {
DFS_AM ( G, i) ;
}
}
}
void BFSTraverse ( AMGraph G, int v) {
for ( int i = 0 ; i < G. numVertexes; i++ ) {
visited[ i] = FALSE;
}
for ( int i = 0 ; i < G. numVertexes; i++ ) {
if ( ! visited[ i] ) {
BFS_AM ( G, v) ;
}
}
}
void BFS_AM ( AMGraph G, int v) {
printf ( "V%d " , v) ;
visited[ v] = TRUE;
SqQueue queue;
InitQueue ( & queue) ;
EnQueue ( & queue, v) ;
int u;
while ( ! IsEmpty ( & queue) ) {
DeQueue ( & queue, & u) ;
for ( int i = 0 ; i < G. numVertexes; i++ ) {
if ( ( G. arcs[ u] [ i] != 0 ) && ( ! visited[ i] ) ) {
printf ( "V%d " , i) ;
visited[ i] = TRUE;
EnQueue ( & queue, i) ;
}
}
}
}
Boolean InitQueue ( SqQueue * queue) {
queue-> base = ( int * ) malloc ( sizeof ( int ) * MAXSIZE) ;
if ( ! ( queue-> base) ) {
return FALSE;
}
queue-> front = queue-> rear = 0 ;
return TRUE;
}
Boolean EnQueue ( SqQueue * queue, int elem) {
if ( IsFull ( queue) ) {
return FALSE;
}
queue-> base[ queue-> rear] = elem;
queue-> rear = ( queue-> rear + 1 ) % MAXSIZE;
return TRUE;
}
Boolean DeQueue ( SqQueue * queue, int * elem) {
if ( IsEmpty ( queue) ) {
return FALSE;
}
* elem = queue-> base[ queue-> front] ;
queue-> front = ( queue-> front + 1 ) % MAXSIZE;
return TRUE;
}
Boolean IsFull ( SqQueue * queue) {
return ( queue-> rear + 1 ) % MAXSIZE == queue-> front;
}
Boolean IsEmpty ( SqQueue * queue) {
return queue-> front == queue-> rear;
}
运行结果