###### 深度优先遍历与广度优先遍历
代码一修改就乱了，CSDN这方面还有等提高啊~~~~~~~
//zhangjun03402@163.com
#include <iostream>
#include <cstdlib>

#define MAX_NUM 4

using std::cout;
using std::cin;
using std::endl;

typedef struct ArcNode{
struct ArcNode *next;
}ArcNode;

typedef struct VexNode{
int data;
ArcNode * FirstArc;
}VexNode;

int visited[MAX_NUM];

typedef struct QNode{
int data;
struct QNode *next;
}QNode, *QueuePtr;

typedef struct{
QueuePtr front;
QueuePtr rear;

q->front=q->rear=(QueuePtr)malloc(sizeof(struct QNode));
if(!q->front)
exit(0);
q->front->next=NULL;
}

return q->front==q->rear;
}

QueuePtr node = (QueuePtr)malloc(sizeof(QNode));
if( !node )
exit(0);
node->data = e;
node->next=NULL;
q->rear->next=node;
q->rear = node;
}

QueuePtr node;
if(empty(q))
return;
node = q->front->next;
e = node->data;
q->front->next=node->next;
if( !node->next )   //这个地方要注意，队列空时，尾指针要指向“头结点”
q->rear = q->front;
free(node);
}

void CreatGraph(VexNode G[], int n)
{
int i, e;
ArcNode *p, *q = NULL;
cout<<"Input "<<MAX_NUM<<" vertexs' value:"<<endl;
for(i=0; i<n; i++){
cin>>G[i].data; //用scanf("%d",G[i])居然也可以
G[i].FirstArc = NULL;
}
for(i=0; i<n; i++){
cout<<"Creat the edges for the NO."<<i<<" vertex, input its adjvex..."<<endl;
cin>>e;
while(e != 1111){ //为什么以-1为结束判断标志不行...
p=(ArcNode *)malloc(sizeof(struct ArcNode));
p->next = NULL;
if(G[i].FirstArc == NULL)
G[i].FirstArc = p;
else
q->next = p;
q = p;
cin>>e;
}
}
}

if(G[v].FirstArc != NULL)
return -1;
}

ArcNode *p;
p = G[v].FirstArc;
while( p!= NULL ){
else
p = p->next;
}
return -1;
}

void DFS(VexNode G[], int v){
int w;

visited[v]=1;
cout<<G[v].data<<"->";

{
if( visited[w]==0 )
DFS(G, w);
}
}

void Travel_DFS(VexNode G[], int n)
{
for(int i=0;i<n;i++){
visited[i] = 0;
}
for(int i=0; i<n; i++)
if(visited[i] == 0)
DFS(G,i);
}

void BFS(VexNode G[], int v)
{
int flag, w;

InitQueue(q);
cout<<G[v].data<<"->";
visited[v]=1;
enqueue(q, v);

while(!empty(q)){
dequeue(q, v);
while(w != -1){
if(visited[w]==0){
cout<<G[w].data<<"->";
enqueue(q,w);
visited[w]=1;
}
}
}
}

void Travel_BFS(VexNode G[], int n)
{
for(int i=0;i<n;i++){
visited[i] = 0;
}
for(int i=0; i<n; i++)
if(visited[i] == 0)
BFS(G,i);
}

int main(){
VexNode G[MAX_NUM];
CreatGraph(G, MAX_NUM);
cout<<"DFS:"<<endl;
//DFS(G, 0);
Travel_DFS(G,MAX_NUM);  //深度优先遍历
cout<<"NULL"<<endl;

cout<<"BFS:"<<endl;
//BFS(G, 0);  //此时调用，之前深度遍历时已将visited[]全变为1
Travel_BFS(G,MAX_NUM);  //广度优先遍历

cout<<"NULL"<<endl;

system("PAUSE");
return 0;
}


#### 图的基本概念表示方法以及两种搜索方式——深度优先遍历和广度优先遍历

2017-06-10 22:19:31

#### 图的深度优先遍历和广度优先遍历代码实现

2015-11-04 17:07:32

#### 数据结构——深度优先遍历和广度优先遍历

2017-02-16 19:24:21

#### 无向图的深度优先遍历和广度优先遍历（递归）

2016-05-23 20:43:01

#### 深度优先遍历和广度优先遍历思考

2015-07-03 11:38:38

#### 数据结构之图的深度优先遍历和广度优先遍历

2016-07-23 10:21:53

#### 用java语言实现图的广度优先遍历和深度优先遍历

2015-10-27 15:30:43

#### 图：存储结构、深度优先遍历以及广度优先遍历

2017-04-09 16:00:46

#### c语言实现图的深度优先遍历和广度优先遍历

2016-06-11 20:55:42

#### 记数据结构之有向图的深度优先遍历，广度优先遍历

2016-11-26 19:42:30