#include <stdio.h>
#include <stdlib.h>
#define MAXVERTEX 10
typedef char VertexType; //顶点类型
typedef int EdgeType; //边的类型
typedef int ElemType; //队列中元素类型
typedef struct EdgeNode
{
int adjvex;
EdgeType weight;
struct EdgeNode *next;
}EdgeNode; //在邻接表中存放邻接顶点下标值的结点
typedef struct VertexNode
{
int Flag;
int vertex;
VertexType data;
EdgeNode *firstedge;
}VertexNode,AdjList[MAXVERTEX]; //定义一个MAXVERTEX个顶点的邻接表
typedef struct GraphAdjList
{
AdjList adjList;
int numVertex;
int numEdge;
}GraphAdjList; //定义一个图 typedef struct QNode
{
ElemType qData;
struct QNode *nextNode;
}QNode; //定义队列的结点
typedef struct QueueList
{
QNode *front;
QNode *rear;
}QueueList,*queue; //定义一个队列 //初始化队列
void InitQueue(QueueList *Q)
{
Q->front = (QNode*)malloc(sizeof(QNode));
if( Q->front == NULL )
{
printf("Error!");
exit(0);
}
Q->rear = Q->front;
// Q->rear = NULL;
Q->front->nextNode = NULL;
} //插入一个结点到队列
void InsertQueue(QueueList *Q,ElemType *e)
{
QNode *p;
p = (QNode *)malloc(sizeof(QNode));
p->qData = *e;
p->nextNode = NULL;
Q->rear->nextNode = p;
Q->rear = p;
} //删除队列中的结点(出队列)
void DeleteQueue(QueueList *Q,ElemType *e)
{
QNode *s;
if(Q->front == Q->rear)
{
return;
}
s = Q->front->nextNode;
*e = s->qData;
Q->front->nextNode = s->nextNode;
if(Q->front->nextNode == NULL)
{
Q->rear = Q->front;
}
free(s);
return;
} void CreateGraph(GraphAdjList *G) //构建一个我们要遍历的图
{
int i = 0,j = 0,k = 0;
EdgeNode *s;
VertexType c;
printf("请输入图的顶点数和边数,中间用英文逗号隔开 :\n");
scanf("%d,%d",&G->numVertex,&G->numEdge);
printf("请输入各个顶点中存放的数据:\n");
fflush(stdin);
scanf("%c",&c);
while(i < G->numVertex)
{
if(c == '\n')
{
break;
}
G->adjList[i].data = c;
G->adjList[i].Flag = 0;
G->adjList[i].vertex = i;
G->adjList[i].firstedge = NULL;
i++;
scanf("%c",&c);
}
fflush(stdin);
for(k = 0;k < G->numEdge;k++)
{
printf("请输入边Vi~Vj依附的顶点下标 i 和 j :\n");
scanf("%d,%d",&i,&j);
s = (EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex = j;
s->next = G->adjList[i].firstedge;
G->adjList[i].firstedge = s;
s = (EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex = i;
s->next = G->adjList[j].firstedge;
G->adjList[j].firstedge = s;
}
}
//查看邻接表是否构建的正确,这个函数只是为了验证
void print(GraphAdjList *G)
{
int i = 0;
EdgeNode *p;
for(i = 0;i < G->numVertex;i++)
{
printf("\n %d->",i);
p = G->adjList[i].firstedge;
while(p)
{
printf("%d->",p->adjvex);
p = p->next;
}
printf("End\n");
}
} //DFS遍历
void DFSTraverse(GraphAdjList *G,int i)
{
int j = 0;
EdgeNode *p;
G->adjList[i].Flag = 1;
printf("%c->",G->adjList[i].data);
p = G->adjList[i].firstedge;
while(p != NULL)
{
if(G->adjList[p->adjvex].Flag == 0)
{
DFSTraverse(G,p->adjvex);
}
p = p->next;
}
}
//判断队列是否为空
int QueueEmpty(QueueList *Q)
{
if(Q->front == Q->rear)
return 0;
else
return 1;
}
//BFS遍历
void BFSTraverse(GraphAdjList *G)
{
int i = 0,k = 0,flag = 0;
EdgeNode *s;
QueueList Q;
InitQueue(&Q);
for(i = 0;i < G->numVertex;i++)
{
G->adjList[i].Flag = 0;
}
for(i = 0;i < G->numVertex;i++)
{
if(G->adjList[i].Flag == 0)
{
G->adjList[i].Flag = 1;
// printf("%c ",G->adjList[i].data);
InsertQueue(&Q,&i);
while(QueueEmpty(&Q))
{
DeleteQueue(&Q,&i);
printf("%c->",G->adjList[i].data);
s = G->adjList[i].firstedge;
while(s != NULL)
{
k = s->adjvex;
if(G->adjList[k].Flag == 0)
{
G->adjList[k].Flag = 1;
// printf("%c ",G->adjList[k].data);
InsertQueue(&Q,&(s->adjvex));
}
s = s->next;
} }
}
}
printf("End\n");
} int main()
{
int k = 0; //深度优先遍历从第1个顶点(按输入顺序)开始
GraphAdjList *G;
CreateGraph(G);
printf("\n顶点的邻接表为:\n");
print(G); //按照头插法得到的邻接表
printf("\nDFS的结果是:\n");
DFSTraverse(G,k); //深度优先遍历
printf("End\n");
printf("\nBFS的结果是:\n");
BFSTraverse(G); //广度优先遍历
return;
}

  

用邻接表实现DFS和BFS的更多相关文章

  1. PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)

    //采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include< ...

  2. JAVA实现图的邻接表以及DFS

    一:定义邻接表结构储存图 package 图的遍历; //邻接表实现图的建立 //储存边 class EdgeNode { int index; // 习惯了用index,其实标准写法是(adjVer ...

  3. All Roads Lead to Rome(30)(MAP【int,string】,邻接表,DFS,模拟,SPFA)(PAT甲级)

    #include<bits/stdc++.h>using namespace std;map<string,int>city;map<int,string>rcit ...

  4. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...

  5. 【数据结构】图的基本操作——图的构造(邻接矩阵,邻接表),遍历(DFS,BFS)

    邻接矩阵实现如下: /* 主题:用邻接矩阵实现 DFS(递归) 与 BFS(非递归) 作者:Laugh 语言:C++ ***************************************** ...

  6. 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS

    图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...

  7. 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)

    数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...

  8. 邻接表实现Dijkstra算法以及DFS与BFS算法

    //============================================================================ // Name : ListDijkstr ...

  9. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

随机推荐

  1. android——ObjectAnimator动画

    在新的android sdk中谷歌为我们提供了新的动画实现方式.化繁为简.将以前的animation动画进一步封装,使用起来更加方便. 先来看XML文件: <RelativeLayout xml ...

  2. JavaScript面向对象精要

    来自:淡忘~浅思. 链接:http://www.ido321.com/1585.html 和 http://www.ido321.com/1586.html 数据类型   在JavaScript中,数 ...

  3. 单击事件的处理方式及注册窗体的创建之(四)Intent实现界面跳转传值

    跳转开发步骤: 创建Intent对象  设置Intent对象的跳转路径  启动Intent //Register_Activity.java case R.id.btnRegister: Inte ...

  4. 【转】获取CID 和 LAC的方法

    原文地址:http://stackoverflow.com/questions/13399659/get-cellid-mcc-mnc-lac-and-network-in-ios-5-1 在iOS5 ...

  5. hdu2304Electrical Outlets

    Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is ...

  6. PDO扩展使用方法

    pdo扩展为php访问数据库提供了一个轻量级的一致接口,pdo提供了一个数据访问抽象层,这意味着不管使用哪种数据库,都可以使用相同的函数来查询和获取数据. $dbms='mysql'; //数据库类型 ...

  7. FreeImage裁剪示例

    //截图 int cropImage(const char* file, int left, int top, int right, int bottom, BYTE* &dstData, D ...

  8. 修改Windows系统的启动Shell

    前提:当前系统中有可用的shell文件   方法: 修改当前用户的系统默认shell(只对当前用户生效,且优先于本机默认的shell) 修改“HKCU\SOFTWARE\Microsoft\Windo ...

  9. [汇编语言]-第五章[bx]和loop指令

    1- [bx]和内存单元的描述 [0]表示内存单元, 他的偏移地址为0 mov ax,[0] 将一个内存单元的内容送入到ax.这个内存单元的长度为2字节(字单元),存放一个字,偏移地址为0,段地址在d ...

  10. TensorFlow深度学习笔记 文本与序列的深度模型

    Deep Models for Text and Sequence 转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎st ...