#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define maxSize 101
int INF = -2313;
int MINF = 6666;//定义图的图的无穷大量
typedef struct MGraph
{
int n,e;
int edges[maxSize][maxSize];
};
//邻接表
typedef struct ArcNode
{
int adjvex;//该点所指顶点的位置信息
ArcNode *nextarc;//指向下一条边
} ArcNode;
typedef struct
{
int data;
int count;//统计入度信息
ArcNode *firstarc;//指向第一条边
} VNode,AdjList;
typedef struct AGraph
{
AdjList adjlist[maxSize];
int n,e;
} AGraph;
int visit[1001];
//邻接矩阵转化为邻接表
void creatAGraph(AGraph *G,MGraph M)
{
G->e = M.e;
G->n = M.n;
for(int i=0; i<M.n; i++) //构造顶点表
{
G->adjlist[i].data = i;
G->adjlist[i].firstarc = NULL;
G->adjlist[i].count = 0;
}
ArcNode *p;
for(int i=0; i<M.n; i++) //头插法来构造
{
for(int j=0; j<M.n; j++)
if(M.edges[i][j]!=0)//当存在边的时候采用头插法来构造
{
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;
G->adjlist[j].count++;//对应的入度加1
}
}//end for
}
void Visit(int v)
{
printf("->%d",v);
}
void DFS(AGraph *G,int v)
{
ArcNode *p;//边结点
visit[v] = 1;//标记访问过了
Visit(v);
p = G->adjlist[v].firstarc;
while(p!=NULL)
{
if(visit[p->adjvex] == 0)
DFS(G,p->adjvex);
p = p->nextarc;
}
}
void BFS(AGraph *G,int v)
{
queue<int> Q;
ArcNode *p;
int i,j;
Visit(v);
visit[v] = 1;//访问改结点
Q.push(v);//结点入队列
while(!Q.empty())
{
j = Q.front();
Q.pop();
p = G->adjlist[j].firstarc;
while(p!=NULL)
{
if(visit[p->adjvex] == 0)//没有访问
{
Visit(p->adjvex);
visit[p->adjvex] = 1;
Q.push(p->adjvex);//入队列
}
p = p->nextarc;
}
}//end while
}