図アルゴリズム:1、隣接表実装図の深さ優先遍歴、広さ優先エルゴード


もう一つの記事:再帰的にdfsを実現するすべての記事です。bfs:http://blog.csdn.net/codeforme/article/details/6036864#この記事はメモリ漏れ問題があります。
私のbfsはキューを使って実現し、メモリ漏れ問題を解決しました。
Graph.h
/************************************************************************/  
/*                 
             :       ,             ,
              ,        */  
/************************************************************************/  
#ifndef GRAPH_H
#define GRAPH_H

#define MaxVertexNum 100  
#define QueueSize 30   
   
bool visited[MaxVertexNum];    
typedef char VertexType;  
typedef int EdgeType;  
typedef struct node     //      ,
{  
    int adjvex;         //     ,  ,                  
    struct node* next;  //    
    //         ,           
}EdgeNode;  

typedef struct vnode    //       
{  
	//VertexNode():firstedge(NULL){}//      
    VertexType vertex;  //     ,  :         ,         
    EdgeNode* firstedge;//       
	int index;
	//                 ,                          
	void setIndex(int iIndex)
	{
		index = iIndex;
	}
}VertexNode; 
 
typedef VertexNode AdjList[MaxVertexNum];   //AdjList        
typedef struct   
{  
    AdjList adjlist;    //     
    int n;				//       
	int e;				//        
}ALGraph;               //       ,       ,     AdjList    
ALGraph* initALGraph(); 
bool DFS(ALGraph* a, int i);
bool DFSTraverseM(ALGraph* a);
bool BFSTraverseM(ALGraph* a);
bool BFS(ALGraph* a, int i);

#endif
Graph.cpp
#include "Graph.h"
#include 
#include 
#include "MyQueue.h"
#include 
#include 

using namespace std;



ALGraph* initALGraph()     
{
	ALGraph* a = NULL;
	EdgeNode* e = NULL;
	int i, j, k;
	char v1, v2;    
	printf("         (     :   ,  ): ");
	scanf("%d %d", &i, &j);
	if(i<0 || j<0)
		return NULL;
	a = (ALGraph*)malloc(sizeof(ALGraph));
	if(a == NULL)
		return NULL;
	a->n = i;
	a->e = j;
	//               ,            
	for(int i = 0 ; i < MaxVertexNum ; i++)
	{
		//VertexNode vNode = a->adjlist[i];
		//   vNode    ,             
		//vNode.firstedge = NULL;//            next  ,           
		//vNode.vertex = 'd';//           ,-1           
		a->adjlist[i].firstedge = NULL;
	}

	     
	for(i=0;  in; i++)
	{
		printf("                   : ");
        fflush(stdin);
		scanf("%c",&(a->adjlist[i].vertex)); //         ,             
        a->adjlist[i].firstedge=NULL;            //             
		a->adjlist[i].setIndex(i);
	}
	for(k=0; ke; k++)
	{
		printf("       (     :i,j): ");
		fflush(stdin);
		scanf("%c,%c", &v1, &v2);
		for(i=0; v1!=a->adjlist[i].vertex; i++);	//           	
		for(j=0; v2!=a->adjlist[j].vertex; j++);//           
		
		e = (EdgeNode*)malloc(sizeof(EdgeNode));//               
		e->adjvex = i;//  :         ,         
		e->next = a->adjlist[j].firstedge;//       
		a->adjlist[j].firstedge = e;
		e = (EdgeNode*)malloc(sizeof(EdgeNode));
		e->adjvex = j;
		e->next = a->adjlist[i].firstedge;
		a->adjlist[i].firstedge = e;
	}
	return a;
}    
/************************************************************************/  
/*                                                                */  
/************************************************************************/   
bool DFS(ALGraph* a, int i)
{
	if(a == NULL)
		return false;
	//printf("DFS: node %c:
", a->adjlist[i].vertex); cout << a->adjlist[i].vertex << "," ; visited[i] = true; i = a->adjlist[i].firstedge->adjvex; if(!visited[i]) DFS(a, i); return true; } bool DFSTraverseM(ALGraph* a) { int i; if(a == NULL) return false; for(i=0; in; i++) visited[i] = false; for(i=0; in; i++) if(!visited[i]) DFS(a, i); return true; } // , , , // : , , void freeList(EdgeNode* pNode) { EdgeNode* pDel; // , while(pNode != NULL) { pDel = pNode; // if(pNode->next) { pNode = pNode->next;// free(pDel); pDel = NULL; } // , else { free(pDel); pDel = NULL; break; } } } // :1 , ( , ),2 void freeGraph(ALGraph* pGraph) { if(pGraph == NULL) { return ; } // for(int i = 0 ; i < MaxVertexNum ; i++) { VertexNode vNode = pGraph->adjlist[i]; // vNode , //if(vNode.vertex != 'd') //{ EdgeNode* pNode = vNode.firstedge; freeList(pNode); //} } // free(pGraph); } /************************************************************************/ /* ( ) */ /************************************************************************/ bool BFSTraverseM(ALGraph* a, int i) { if(a == NULL) return false; // for(int j=0; j n; j++) visited[j] = false; // , //visited[i] = true; // , , queue queueVertexNode; //VertexNode curNode = a->adjlist[i]; queueVertexNode.push(a->adjlist[i]); string sResult; while(! queueVertexNode.empty() ) { // VertexNode curNode = queueVertexNode.front(); // , , if(visited[curNode.index]) { queueVertexNode.pop();// continue; } // , EdgeNode* pNode = curNode.firstedge; // while(pNode) { int index = pNode->adjvex; if(!visited[index]) { queueVertexNode.push(a->adjlist[index]); } pNode = pNode->next; } // , , visited[curNode.index] = true; // ( ) queueVertexNode.pop(); // , cout << curNode.vertex << "," ; } return true; } /* : 7 7 : 1 2 3 4 5 6 7 : 1 2 2 3 3 1 3 4 3 5 3 7 4 6 : :1,3,7,2,4,6,5 :1,3,2,7,5,4,6 */ void process() { ALGraph* pGraph = initALGraph(); cout << " :" ; DFSTraverseM(pGraph); cout << endl << " :" ; BFSTraverseM(pGraph, 0); freeGraph(pGraph); } int main(int argc,char* argv[]) { process(); getchar(); system("pause"); return 0; }