6-4 图的深度遍历-邻接表实现 (10 分)
本文最后更新于1796天前,其中的信息可能已经有所发展或是发生改变。

本题要求实现邻接表存储图的深度优先遍历。

函数接口定义:

void DFS(ALGraph *G,int i);

其中ALGraph是邻接表存储的图,定义如下:

#define MAX_VERTEX_NUM 10      /*定义最大顶点数*/
typedef int Vertex;
typedef struct  ArcNode{       /*表结点*/
	int adjvex;               /*邻接点域*/
	struct  ArcNode *nextarc; /*指向下一个表结点*/
}ArcNode;
typedef struct VNode{           /*头结点*/
	Vertex data;                  /*顶点域*/
	ArcNode *firstarc;          /*指向第一个表结点*/
}VNode,AdjList[MAX_VERTEX_NUM]; /*AdjList是数组类型*/
typedef struct { 
	AdjList vertices;           /*邻接表中数组定义*/
	int vexnum,arcnum;          /*图中当前顶点数和边数*/
} ALGraph;                 /*图类型*/

裁判测试程序样例:

#include"stdio.h"
#include"stdlib.h"
#define MAX_VERTEX_NUM 10      /*定义最大顶点数*/
typedef int Vertex;
typedef struct  ArcNode{       /*表结点*/
	int adjvex;               /*邻接点域*/
	struct  ArcNode *nextarc; /*指向下一个表结点*/
}ArcNode;
typedef struct VNode{           /*头结点*/
	Vertex data;                  /*顶点域*/
	ArcNode *firstarc;          /*指向第一个表结点*/
}VNode,AdjList[MAX_VERTEX_NUM]; /*AdjList是数组类型*/
typedef struct { 
	AdjList vertices;           /*邻接表中数组定义*/
	int vexnum,arcnum;          /*图中当前顶点数和边数*/
} ALGraph;                 /*图类型*/
typedef enum{FALSE,TRUE} Boolean;
Boolean visited[MAX_VERTEX_NUM];/*定义标志向量,为全局变量*/
void CreatALGraph(ALGraph *G);/* 创建图并且将Visited初始化为false;裁判实现,细节不表 */
void DFS(ALGraph *G,int v);
int main()
{
	Vertex v;
	ALGraph G;
	CreatALGraph(&G);
	scanf("%d", &v);
	printf("DFS from %d:",v);
	DFS(&G,v); 	
	return 0;
}
/* 你的代码将被嵌在这里 */

对于给定图:

图的遍历样例.png

输入样例:

5

输出样例:

DFS from 5: 5 1 3 0 2 4 6
void DFS(ALGraph *G,int i) {
    printf(" %d",i);
    visited[i]=TRUE;
    for(ArcNode *p=G->vertices[i].firstarc;p!=NULL;p=p->nextarc) {
        if(visited[p->adjvex]==FALSE) 
            DFS(G,p->adjvex);
        }
}

点击数:255

    暂无评论

    发送评论 编辑评论

    
    				
    |´・ω・)ノ
    ヾ(≧∇≦*)ゝ
    (☆ω☆)
    (╯‵□′)╯︵┴─┴
     ̄﹃ ̄
    (/ω\)
    ∠( ᐛ 」∠)_
    (๑•̀ㅁ•́ฅ)
    →_→
    ୧(๑•̀⌄•́๑)૭
    ٩(ˊᗜˋ*)و
    (ノ°ο°)ノ
    (´இ皿இ`)
    ⌇●﹏●⌇
    (ฅ´ω`ฅ)
    (╯°A°)╯︵○○○
    φ( ̄∇ ̄o)
    ヾ(´・ ・`。)ノ"
    ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
    (ó﹏ò。)
    Σ(っ °Д °;)っ
    ( ,,´・ω・)ノ"(´っω・`。)
    ╮(╯▽╰)╭
    o(*////▽////*)q
    >﹏<
    ( ๑´•ω•) "(ㆆᴗㆆ)
    😂
    😀
    😅
    😊
    🙂
    🙃
    😌
    😍
    😘
    😜
    😝
    😏
    😒
    🙄
    😳
    😡
    😔
    😫
    😱
    😭
    💩
    👻
    🙌
    🖕
    👍
    👫
    👬
    👭
    🌚
    🌝
    🙈
    💊
    😶
    🙏
    🍦
    🍉
    😣
    Source: github.com/k4yt3x/flowerhd
    颜文字
    Emoji
    小恐龙
    花!
    上一篇
    下一篇