实验六图基本操作的编程实现.docx
《实验六图基本操作的编程实现.docx》由会员分享,可在线阅读,更多相关《实验六图基本操作的编程实现.docx(16页珍藏版)》请在冰豆网上搜索。
实验六图基本操作的编程实现
【实验目的】
图基本操作的编程实现
要求:
图基本操作的编程实现(2学时,验证型),掌握图的建立、遍历、插入、删除等基本操作的编程实现,存储结构可以在顺序结构、链接结构、联合使用多种结构等中任选,也可以全部实现。
也鼓励学生利用基本操作进行一些应用的程序设计。
【实验性质】
验证性实验(学时数:
2H)
【实验内容】
编程对图进行存储(邻接矩阵或邻接表都可以,由学生自由选择),之后可以询问任何两个结点之间是否有通路和路径数。
设计一个将图形转成邻接链表的程序。
设计一个深度优先搜索法来查找图形的程序。
设计一个广度优先搜索法来查找一个图形的程序。
鼓励开发出难度更高的程序。
【思考问题】
1.图的定义和特性?
2.图的主要存储结构是什么?
是独立的某种还是多种数据结构的综合?
3.图的主要遍历思路是哪些?
4.举出图的应用范例?
【参考代码】
(一)将一个将图转成邻接矩阵的程序.
/*程序构思:
*/
/*用户输入结点与各个边,再将边转成邻接矩阵。
#include
#defineMax6/*定义最大可输入的结点个数*/
intGraph[Max][Max];/*图形邻接数组*/
/*===============================================*/
/*输出邻接矩阵数据===============================*/
voidprint_M_Graph()
{
inti,j;
printf("Vertice");
for(i=0;iprintf("%3d",i);printf("\n");for(i=0;i{printf("%4d",i);for(j=0;jprintf("%3d");printf("\n");}}/*===============================================*//*以邻接矩阵建立图形=============================*//*===============================================*/voidCreate_M_Graph(intVerticel,intVertice2){Graph[Verticel][Vertice2]=1;/*将矩阵内容设为1*/}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;ifor(j=0;jGraph[i][i]=0;while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=Max||Destination>=Max)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接数组*/Create_M_Graph(Source,Destination);}printf("##Graph##\n");;/*调用输出邻接数组数据*/}/*希望的结果*//*pleaseinputtheEdge'ssource:0*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:0*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertice012345*//*0000010*//*1100010*//*2010000*//*3001000*//*4000100*//*5000000*/ (二)将一个将图转成邻接表的程序./*程序构思:*//*用户输入结点与各个边,再将边转成邻接链表。*/#include#include#definevertexnum6/*定义最大可输入的结点个数*/typedefstructnode/*定义图形的顶点结构*/{intvertex;structnode*next;}Graph;Graphhead[vertexnum];/*===============================================*//*以邻接链表建立图形=============================*//*===============================================*/voidCreate_l_Graph(intVertex1,intVertex2){Graph*searchP;/*结点声明*/Graph*New;/*新结点声明*/New=(Graph*)malloc(sizeof(structnode));if(New!=NULL){New->vertex=;New->next=NULL;searchP=&(head[Vertex1]);while(searchP->next!=NULL);searchP->next=New;}}/*===============================================*//*输出邻接链表的数据===============================*//*===============================================*/voidprint_l_graph(structnode*head){Graph*searchP;searchP=head->next;while(searchP!=NULL){printf("[%d]",searchP->vertex);searchP=searchP->next;}printf("\n");}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;i{head[i].vertex=i;head[i].next=NULL;}while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=vertexnum||Destination>=vertexnum)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接链表*/Create_l_Graph(Source,Destination);}printf("##Graph##\n");for(i=0;i<=vertexnum;i++){printf("vertex[%d]:",i);print_l_graph(&head[i]);/*调用输出邻接链表数据*/}}/*希望的结果*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:5*//*pleaseinputtheEdge'ssource:5*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertex[0]:*//*Vertex[1]:*//*Vertex[2]:[1][3]*//*Vertex[3]:[4]*//*Vertex[4]:[1][2][5]*//*Vertex[5]:[1]*/(三)图的深度优先遍历(搜索)。*//*程序构思:*//*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。*/#include#defineVertexNum9/*定义顶点数*/structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intVisited[VertexNum];/*查找记录*//*==========================================*//*深度优先搜索==============================*//*==========================================*/voidDFS(intVertex){GraphSearchP;/*结点声明*/Visited[Vertex]=1;/*已查找*/printf("[%d]==>",Vertex);SearchP=Head[Vertex].Next;while(SearchP!=NULL){if(Visited[SearchP->Vertex]==0);/*递归调用*/SearchP=SearchP->Next;/*下一个邻接点*/}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNew;/*新顶点声明*/New=(Graph)malloc(sizeof(structNode));/*配置内存*/if(New!=NULL)/*配置成功*/{New->Vertex=Vertex2;/*邻近顶点*/New->Next=NULL;/*下一个邻接顶点指针*//*SearchP指针设为顶点数组之首结点*/SearchP=&(Head[Vertex1]);while(SearchP->Next!=NULL)SearchP=SearchP->Next;/*往下一个结点*/SearchP->Next=New;/*串连在链接尾端*/}}/*==========================================*//*输出邻接列表内数据========================*//*==========================================*/voidPrint_L_Graph(structNode*Head){GraphSearchP;/*结点声明*/SearchP=Head->Next;/*SearchP指针设为首结点*/while(SearchP!=NULL)/*当结点为NULL结束循环*/{printf("[%d]",SearchP->Vertex);SearchP=SearchP->Next;/*往下一个结点*/}printf("\n");}/*==========================================*//*主程序====================================*//*==========================================*/voidmain(){inti;intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
printf("%3d",i);
printf("\n");
for(i=0;i{printf("%4d",i);for(j=0;jprintf("%3d");printf("\n");}}/*===============================================*//*以邻接矩阵建立图形=============================*//*===============================================*/voidCreate_M_Graph(intVerticel,intVertice2){Graph[Verticel][Vertice2]=1;/*将矩阵内容设为1*/}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;ifor(j=0;jGraph[i][i]=0;while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=Max||Destination>=Max)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接数组*/Create_M_Graph(Source,Destination);}printf("##Graph##\n");;/*调用输出邻接数组数据*/}/*希望的结果*//*pleaseinputtheEdge'ssource:0*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:0*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertice012345*//*0000010*//*1100010*//*2010000*//*3001000*//*4000100*//*5000000*/ (二)将一个将图转成邻接表的程序./*程序构思:*//*用户输入结点与各个边,再将边转成邻接链表。*/#include#include#definevertexnum6/*定义最大可输入的结点个数*/typedefstructnode/*定义图形的顶点结构*/{intvertex;structnode*next;}Graph;Graphhead[vertexnum];/*===============================================*//*以邻接链表建立图形=============================*//*===============================================*/voidCreate_l_Graph(intVertex1,intVertex2){Graph*searchP;/*结点声明*/Graph*New;/*新结点声明*/New=(Graph*)malloc(sizeof(structnode));if(New!=NULL){New->vertex=;New->next=NULL;searchP=&(head[Vertex1]);while(searchP->next!=NULL);searchP->next=New;}}/*===============================================*//*输出邻接链表的数据===============================*//*===============================================*/voidprint_l_graph(structnode*head){Graph*searchP;searchP=head->next;while(searchP!=NULL){printf("[%d]",searchP->vertex);searchP=searchP->next;}printf("\n");}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;i{head[i].vertex=i;head[i].next=NULL;}while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=vertexnum||Destination>=vertexnum)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接链表*/Create_l_Graph(Source,Destination);}printf("##Graph##\n");for(i=0;i<=vertexnum;i++){printf("vertex[%d]:",i);print_l_graph(&head[i]);/*调用输出邻接链表数据*/}}/*希望的结果*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:5*//*pleaseinputtheEdge'ssource:5*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertex[0]:*//*Vertex[1]:*//*Vertex[2]:[1][3]*//*Vertex[3]:[4]*//*Vertex[4]:[1][2][5]*//*Vertex[5]:[1]*/(三)图的深度优先遍历(搜索)。*//*程序构思:*//*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。*/#include#defineVertexNum9/*定义顶点数*/structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intVisited[VertexNum];/*查找记录*//*==========================================*//*深度优先搜索==============================*//*==========================================*/voidDFS(intVertex){GraphSearchP;/*结点声明*/Visited[Vertex]=1;/*已查找*/printf("[%d]==>",Vertex);SearchP=Head[Vertex].Next;while(SearchP!=NULL){if(Visited[SearchP->Vertex]==0);/*递归调用*/SearchP=SearchP->Next;/*下一个邻接点*/}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNew;/*新顶点声明*/New=(Graph)malloc(sizeof(structNode));/*配置内存*/if(New!=NULL)/*配置成功*/{New->Vertex=Vertex2;/*邻近顶点*/New->Next=NULL;/*下一个邻接顶点指针*//*SearchP指针设为顶点数组之首结点*/SearchP=&(Head[Vertex1]);while(SearchP->Next!=NULL)SearchP=SearchP->Next;/*往下一个结点*/SearchP->Next=New;/*串连在链接尾端*/}}/*==========================================*//*输出邻接列表内数据========================*//*==========================================*/voidPrint_L_Graph(structNode*Head){GraphSearchP;/*结点声明*/SearchP=Head->Next;/*SearchP指针设为首结点*/while(SearchP!=NULL)/*当结点为NULL结束循环*/{printf("[%d]",SearchP->Vertex);SearchP=SearchP->Next;/*往下一个结点*/}printf("\n");}/*==========================================*//*主程序====================================*//*==========================================*/voidmain(){inti;intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
printf("%4d",i);
for(j=0;jprintf("%3d");printf("\n");}}/*===============================================*//*以邻接矩阵建立图形=============================*//*===============================================*/voidCreate_M_Graph(intVerticel,intVertice2){Graph[Verticel][Vertice2]=1;/*将矩阵内容设为1*/}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;ifor(j=0;jGraph[i][i]=0;while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=Max||Destination>=Max)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接数组*/Create_M_Graph(Source,Destination);}printf("##Graph##\n");;/*调用输出邻接数组数据*/}/*希望的结果*//*pleaseinputtheEdge'ssource:0*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:0*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertice012345*//*0000010*//*1100010*//*2010000*//*3001000*//*4000100*//*5000000*/ (二)将一个将图转成邻接表的程序./*程序构思:*//*用户输入结点与各个边,再将边转成邻接链表。*/#include#include#definevertexnum6/*定义最大可输入的结点个数*/typedefstructnode/*定义图形的顶点结构*/{intvertex;structnode*next;}Graph;Graphhead[vertexnum];/*===============================================*//*以邻接链表建立图形=============================*//*===============================================*/voidCreate_l_Graph(intVertex1,intVertex2){Graph*searchP;/*结点声明*/Graph*New;/*新结点声明*/New=(Graph*)malloc(sizeof(structnode));if(New!=NULL){New->vertex=;New->next=NULL;searchP=&(head[Vertex1]);while(searchP->next!=NULL);searchP->next=New;}}/*===============================================*//*输出邻接链表的数据===============================*//*===============================================*/voidprint_l_graph(structnode*head){Graph*searchP;searchP=head->next;while(searchP!=NULL){printf("[%d]",searchP->vertex);searchP=searchP->next;}printf("\n");}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;i{head[i].vertex=i;head[i].next=NULL;}while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=vertexnum||Destination>=vertexnum)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接链表*/Create_l_Graph(Source,Destination);}printf("##Graph##\n");for(i=0;i<=vertexnum;i++){printf("vertex[%d]:",i);print_l_graph(&head[i]);/*调用输出邻接链表数据*/}}/*希望的结果*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:5*//*pleaseinputtheEdge'ssource:5*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertex[0]:*//*Vertex[1]:*//*Vertex[2]:[1][3]*//*Vertex[3]:[4]*//*Vertex[4]:[1][2][5]*//*Vertex[5]:[1]*/(三)图的深度优先遍历(搜索)。*//*程序构思:*//*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。*/#include#defineVertexNum9/*定义顶点数*/structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intVisited[VertexNum];/*查找记录*//*==========================================*//*深度优先搜索==============================*//*==========================================*/voidDFS(intVertex){GraphSearchP;/*结点声明*/Visited[Vertex]=1;/*已查找*/printf("[%d]==>",Vertex);SearchP=Head[Vertex].Next;while(SearchP!=NULL){if(Visited[SearchP->Vertex]==0);/*递归调用*/SearchP=SearchP->Next;/*下一个邻接点*/}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNew;/*新顶点声明*/New=(Graph)malloc(sizeof(structNode));/*配置内存*/if(New!=NULL)/*配置成功*/{New->Vertex=Vertex2;/*邻近顶点*/New->Next=NULL;/*下一个邻接顶点指针*//*SearchP指针设为顶点数组之首结点*/SearchP=&(Head[Vertex1]);while(SearchP->Next!=NULL)SearchP=SearchP->Next;/*往下一个结点*/SearchP->Next=New;/*串连在链接尾端*/}}/*==========================================*//*输出邻接列表内数据========================*//*==========================================*/voidPrint_L_Graph(structNode*Head){GraphSearchP;/*结点声明*/SearchP=Head->Next;/*SearchP指针设为首结点*/while(SearchP!=NULL)/*当结点为NULL结束循环*/{printf("[%d]",SearchP->Vertex);SearchP=SearchP->Next;/*往下一个结点*/}printf("\n");}/*==========================================*//*主程序====================================*//*==========================================*/voidmain(){inti;intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
printf("%3d");
}
/*以邻接矩阵建立图形=============================*/
voidCreate_M_Graph(intVerticel,intVertice2)
Graph[Verticel][Vertice2]=1;/*将矩阵内容设为1*/
/*主程序=========================================*/
voidmain()
intSource;/*起始顶点*/
intDestination;/*终止顶点*/
for(i=0;ifor(j=0;jGraph[i][i]=0;while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=Max||Destination>=Max)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接数组*/Create_M_Graph(Source,Destination);}printf("##Graph##\n");;/*调用输出邻接数组数据*/}/*希望的结果*//*pleaseinputtheEdge'ssource:0*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:0*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertice012345*//*0000010*//*1100010*//*2010000*//*3001000*//*4000100*//*5000000*/ (二)将一个将图转成邻接表的程序./*程序构思:*//*用户输入结点与各个边,再将边转成邻接链表。*/#include#include#definevertexnum6/*定义最大可输入的结点个数*/typedefstructnode/*定义图形的顶点结构*/{intvertex;structnode*next;}Graph;Graphhead[vertexnum];/*===============================================*//*以邻接链表建立图形=============================*//*===============================================*/voidCreate_l_Graph(intVertex1,intVertex2){Graph*searchP;/*结点声明*/Graph*New;/*新结点声明*/New=(Graph*)malloc(sizeof(structnode));if(New!=NULL){New->vertex=;New->next=NULL;searchP=&(head[Vertex1]);while(searchP->next!=NULL);searchP->next=New;}}/*===============================================*//*输出邻接链表的数据===============================*//*===============================================*/voidprint_l_graph(structnode*head){Graph*searchP;searchP=head->next;while(searchP!=NULL){printf("[%d]",searchP->vertex);searchP=searchP->next;}printf("\n");}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;i{head[i].vertex=i;head[i].next=NULL;}while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=vertexnum||Destination>=vertexnum)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接链表*/Create_l_Graph(Source,Destination);}printf("##Graph##\n");for(i=0;i<=vertexnum;i++){printf("vertex[%d]:",i);print_l_graph(&head[i]);/*调用输出邻接链表数据*/}}/*希望的结果*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:5*//*pleaseinputtheEdge'ssource:5*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertex[0]:*//*Vertex[1]:*//*Vertex[2]:[1][3]*//*Vertex[3]:[4]*//*Vertex[4]:[1][2][5]*//*Vertex[5]:[1]*/(三)图的深度优先遍历(搜索)。*//*程序构思:*//*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。*/#include#defineVertexNum9/*定义顶点数*/structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intVisited[VertexNum];/*查找记录*//*==========================================*//*深度优先搜索==============================*//*==========================================*/voidDFS(intVertex){GraphSearchP;/*结点声明*/Visited[Vertex]=1;/*已查找*/printf("[%d]==>",Vertex);SearchP=Head[Vertex].Next;while(SearchP!=NULL){if(Visited[SearchP->Vertex]==0);/*递归调用*/SearchP=SearchP->Next;/*下一个邻接点*/}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNew;/*新顶点声明*/New=(Graph)malloc(sizeof(structNode));/*配置内存*/if(New!=NULL)/*配置成功*/{New->Vertex=Vertex2;/*邻近顶点*/New->Next=NULL;/*下一个邻接顶点指针*//*SearchP指针设为顶点数组之首结点*/SearchP=&(Head[Vertex1]);while(SearchP->Next!=NULL)SearchP=SearchP->Next;/*往下一个结点*/SearchP->Next=New;/*串连在链接尾端*/}}/*==========================================*//*输出邻接列表内数据========================*//*==========================================*/voidPrint_L_Graph(structNode*Head){GraphSearchP;/*结点声明*/SearchP=Head->Next;/*SearchP指针设为首结点*/while(SearchP!=NULL)/*当结点为NULL结束循环*/{printf("[%d]",SearchP->Vertex);SearchP=SearchP->Next;/*往下一个结点*/}printf("\n");}/*==========================================*//*主程序====================================*//*==========================================*/voidmain(){inti;intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
for(j=0;jGraph[i][i]=0;while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=Max||Destination>=Max)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接数组*/Create_M_Graph(Source,Destination);}printf("##Graph##\n");;/*调用输出邻接数组数据*/}/*希望的结果*//*pleaseinputtheEdge'ssource:0*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:0*//*pleaseinputtheEdge'ssource:1*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertice012345*//*0000010*//*1100010*//*2010000*//*3001000*//*4000100*//*5000000*/ (二)将一个将图转成邻接表的程序./*程序构思:*//*用户输入结点与各个边,再将边转成邻接链表。*/#include#include#definevertexnum6/*定义最大可输入的结点个数*/typedefstructnode/*定义图形的顶点结构*/{intvertex;structnode*next;}Graph;Graphhead[vertexnum];/*===============================================*//*以邻接链表建立图形=============================*//*===============================================*/voidCreate_l_Graph(intVertex1,intVertex2){Graph*searchP;/*结点声明*/Graph*New;/*新结点声明*/New=(Graph*)malloc(sizeof(structnode));if(New!=NULL){New->vertex=;New->next=NULL;searchP=&(head[Vertex1]);while(searchP->next!=NULL);searchP->next=New;}}/*===============================================*//*输出邻接链表的数据===============================*//*===============================================*/voidprint_l_graph(structnode*head){Graph*searchP;searchP=head->next;while(searchP!=NULL){printf("[%d]",searchP->vertex);searchP=searchP->next;}printf("\n");}/*===============================================*//*主程序=========================================*//*===============================================*/voidmain(){intSource;/*起始顶点*/intDestination;/*终止顶点*/inti,j;for(i=0;i{head[i].vertex=i;head[i].next=NULL;}while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=vertexnum||Destination>=vertexnum)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接链表*/Create_l_Graph(Source,Destination);}printf("##Graph##\n");for(i=0;i<=vertexnum;i++){printf("vertex[%d]:",i);print_l_graph(&head[i]);/*调用输出邻接链表数据*/}}/*希望的结果*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:5*//*pleaseinputtheEdge'ssource:5*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertex[0]:*//*Vertex[1]:*//*Vertex[2]:[1][3]*//*Vertex[3]:[4]*//*Vertex[4]:[1][2][5]*//*Vertex[5]:[1]*/(三)图的深度优先遍历(搜索)。*//*程序构思:*//*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。*/#include#defineVertexNum9/*定义顶点数*/structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intVisited[VertexNum];/*查找记录*//*==========================================*//*深度优先搜索==============================*//*==========================================*/voidDFS(intVertex){GraphSearchP;/*结点声明*/Visited[Vertex]=1;/*已查找*/printf("[%d]==>",Vertex);SearchP=Head[Vertex].Next;while(SearchP!=NULL){if(Visited[SearchP->Vertex]==0);/*递归调用*/SearchP=SearchP->Next;/*下一个邻接点*/}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNew;/*新顶点声明*/New=(Graph)malloc(sizeof(structNode));/*配置内存*/if(New!=NULL)/*配置成功*/{New->Vertex=Vertex2;/*邻近顶点*/New->Next=NULL;/*下一个邻接顶点指针*//*SearchP指针设为顶点数组之首结点*/SearchP=&(Head[Vertex1]);while(SearchP->Next!=NULL)SearchP=SearchP->Next;/*往下一个结点*/SearchP->Next=New;/*串连在链接尾端*/}}/*==========================================*//*输出邻接列表内数据========================*//*==========================================*/voidPrint_L_Graph(structNode*Head){GraphSearchP;/*结点声明*/SearchP=Head->Next;/*SearchP指针设为首结点*/while(SearchP!=NULL)/*当结点为NULL结束循环*/{printf("[%d]",SearchP->Vertex);SearchP=SearchP->Next;/*往下一个结点*/}printf("\n");}/*==========================================*//*主程序====================================*//*==========================================*/voidmain(){inti;intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
Graph[i][i]=0;
while
(1)
printf("pleaseinputtheEdge'ssource:
");
scanf("%d",&Source);
if(Source==-1)
break;
printf("PleaseinputtheEdge'sDestination:
scanf("%d",&Destination);
if(Source==Destination)/*出错:
自身循环*/
printf("***Error***:
SelfLoop!
!
\n");
elseif(Source>=Max||Destination>=Max)/*出错:
超出范围*/
outofrange!
else/*调用建立邻接数组*/
Create_M_Graph(Source,Destination);
printf("##Graph##\n");
;/*调用输出邻接数组数据*/
/*希望的结果*/
/*pleaseinputtheEdge'ssource:
0*/
/*PleaseinputtheEdge'sDestination:
4*/
1*/
2*/
3*/
-1*/
/*##Graph##*/
/*Vertice012345*/
/*0000010*/
/*1100010*/
/*2010000*/
/*3001000*/
/*4000100*/
/*5000000*/
(二)将一个将图转成邻接表的程序.
/*用户输入结点与各个边,再将边转成邻接链表。
#definevertexnum6/*定义最大可输入的结点个数*/
typedefstructnode/*定义图形的顶点结构*/
intvertex;
structnode*next;
}Graph;
Graphhead[vertexnum];
/*以邻接链表建立图形=============================*/
voidCreate_l_Graph(intVertex1,intVertex2)
Graph*searchP;/*结点声明*/
Graph*New;/*新结点声明*/
New=(Graph*)malloc(sizeof(structnode));
if(New!
=NULL)
New->vertex=;
New->next=NULL;
searchP=&(head[Vertex1]);
while(searchP->next!
;
searchP->next=New;
/*输出邻接链表的数据===============================*/
voidprint_l_graph(structnode*head)
Graph*searchP;
searchP=head->next;
while(searchP!
printf("[%d]",searchP->vertex);
searchP=searchP->next;
for(i=0;i{head[i].vertex=i;head[i].next=NULL;}while(1){printf("pleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestination:");scanf("%d",&Destination);if(Source==Destination)/*出错:自身循环*/printf("***Error***:SelfLoop!!\n");elseif(Source>=vertexnum||Destination>=vertexnum)/*出错:超出范围*/printf("***Error***:outofrange!!\n");else/*调用建立邻接链表*/Create_l_Graph(Source,Destination);}printf("##Graph##\n");for(i=0;i<=vertexnum;i++){printf("vertex[%d]:",i);print_l_graph(&head[i]);/*调用输出邻接链表数据*/}}/*希望的结果*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:2*//*PleaseinputtheEdge'sDestination:3*//*pleaseinputtheEdge'ssource:3*//*PleaseinputtheEdge'sDestination:4*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:2*//*pleaseinputtheEdge'ssource:4*//*PleaseinputtheEdge'sDestination:5*//*pleaseinputtheEdge'ssource:5*//*PleaseinputtheEdge'sDestination:1*//*pleaseinputtheEdge'ssource:-1*//*##Graph##*//*Vertex[0]:*//*Vertex[1]:*//*Vertex[2]:[1][3]*//*Vertex[3]:[4]*//*Vertex[4]:[1][2][5]*//*Vertex[5]:[1]*/(三)图的深度优先遍历(搜索)。*//*程序构思:*//*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。*/#include#defineVertexNum9/*定义顶点数*/structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intVisited[VertexNum];/*查找记录*//*==========================================*//*深度优先搜索==============================*//*==========================================*/voidDFS(intVertex){GraphSearchP;/*结点声明*/Visited[Vertex]=1;/*已查找*/printf("[%d]==>",Vertex);SearchP=Head[Vertex].Next;while(SearchP!=NULL){if(Visited[SearchP->Vertex]==0);/*递归调用*/SearchP=SearchP->Next;/*下一个邻接点*/}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNew;/*新顶点声明*/New=(Graph)malloc(sizeof(structNode));/*配置内存*/if(New!=NULL)/*配置成功*/{New->Vertex=Vertex2;/*邻近顶点*/New->Next=NULL;/*下一个邻接顶点指针*//*SearchP指针设为顶点数组之首结点*/SearchP=&(Head[Vertex1]);while(SearchP->Next!=NULL)SearchP=SearchP->Next;/*往下一个结点*/SearchP->Next=New;/*串连在链接尾端*/}}/*==========================================*//*输出邻接列表内数据========================*//*==========================================*/voidPrint_L_Graph(structNode*Head){GraphSearchP;/*结点声明*/SearchP=Head->Next;/*SearchP指针设为首结点*/while(SearchP!=NULL)/*当结点为NULL结束循环*/{printf("[%d]",SearchP->Vertex);SearchP=SearchP->Next;/*往下一个结点*/}printf("\n");}/*==========================================*//*主程序====================================*//*==========================================*/voidmain(){inti;intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
head[i].vertex=i;
head[i].next=NULL;
elseif(Source>=vertexnum||Destination>=vertexnum)
/*出错:
else/*调用建立邻接链表*/
Create_l_Graph(Source,Destination);
for(i=0;i<=vertexnum;i++)
printf("vertex[%d]:
",i);
print_l_graph(&head[i]);/*调用输出邻接链表数据*/
5*/
/*Vertex[0]:
/*Vertex[1]:
/*Vertex[2]:
[1][3]*/
/*Vertex[3]:
[4]*/
/*Vertex[4]:
[1][2][5]*/
/*Vertex[5]:
[1]*/
(三)图的深度优先遍历(搜索)。
/*递归调用深度优先搜索法,往下一个顶点查找,直到查找到列表尾端为止。
#defineVertexNum9/*定义顶点数*/
structNode/*声明图形顶点结构*/
intVertex;/*邻接顶点数据*/
structNode*Next;/*下一个邻接顶点*/
};
typedefstructNode*Graph;/*定义图形结构*/
structNodeHead[VertexNum];/*顶点数组*/
intVisited[VertexNum];/*查找记录*/
/*==========================================*/
/*深度优先搜索==============================*/
voidDFS(intVertex)
GraphSearchP;/*结点声明*/
Visited[Vertex]=1;/*已查找*/
printf("[%d]==>",Vertex);
SearchP=Head[Vertex].Next;
while(SearchP!
if(Visited[SearchP->Vertex]==0)
;/*递归调用*/
SearchP=SearchP->Next;/*下一个邻接点*/
/*建立邻接顶点至邻接列表内==================*/
voidCreate_L_Graph(intVertex1,intVertex2)
GraphNew;/*新顶点声明*/
New=(Graph)malloc(sizeof(structNode));/*配置内存*/
=NULL)/*配置成功*/
New->Vertex=Vertex2;/*邻近顶点*/
New->Next=NULL;/*下一个邻接顶点指针*/
/*SearchP指针设为顶点数组之首结点*/
SearchP=&(Head[Vertex1]);
while(SearchP->Next!
SearchP=SearchP->Next;/*往下一个结点*/
SearchP->Next=New;/*串连在链接尾端*/
/*输出邻接列表内数据========================*/
voidPrint_L_Graph(structNode*Head)
SearchP=Head->Next;/*SearchP指针设为首结点*/
=NULL)/*当结点为NULL结束循环*/
printf("[%d]",SearchP->Vertex);
/*主程序====================================*/
inti;
intNode[20][2]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},
{2,5},{5,2},{3,6},{6,3},{3,7},{7,3},
{4,8},{8,4},{5,8},{8,5},{6,8,},{8,6},{7,8},{8,7}};
for(i=0;i{Head[i].Vertex=i;Head[i].Next=NULL;}for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
Head[i].Vertex=i;
Head[i].Next=NULL;
for(i=0;iVisited[i]=;for(i=0;i<20;i++)Create_L_Graph(Node[i][0],Node[i][1]);printf("##Graph##\n");for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
Visited[i]=;
for(i=0;i<20;i++)
Create_L_Graph(Node[i][0],Node[i][1]);
for(i=0;i{printf("Vertex[%d]:",i);Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/}printf("Depth-First-Search:\n");printf("[BEGIN]==>");DFS(1);printf("[END]\n");}/*希望的结果*//*##Graph##*//*Vertex[1]:[2][3]*//*Vertex[2]:[1][4][5]*//*Vertex[3]:[1][6][7]*//*Vertex[4]:[2][8]*//*Vertex[5]:[2][8]*//*Vertex[6]:[3][8]*//*Vertex[7]:[3][8]*//*Vertex[8]:[4][5][6][7]*//*Detph-First-Search:*//*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/(四)图的广度优先遍历(搜索)。/*程序构思:*//*查找顶点时,先将该顶点的邻接顶点皆存入队列中。 *//*(关于队列的运用,之前的章节已有介绍,在此不再重复)*//*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。*/#include#defineVertexNum9/*定义顶点数*/#defineQueueMax10structNode/*声明图形顶点结构*/{intVertex;/*邻接顶点数据*/structNode*Next;/*下一个邻接顶点*/};typedefstructNode*Graph;/*定义图形结构*/structNodeHead[VertexNum];/*顶点数组*/intQueue[QueueMax];intFront=-1;intRear=-1;intVisited[VertexNum];/*查找记录*//*==========================================*//*队列的存入================================*//*==========================================*/intEnqueue(intVertex){if(Rear>=QueueMax)/*队列已满*/return-1;else{Rear++;/*队列尾端指针后移*/Queue[Rear]=Vertex;/*将值存入队列中*/return1;}}/*==========================================*//*队列的取出================================*//*==========================================*/intDequeue(){if(Front>=Rear)/*队列已空*/return-1;else{Front++;/*队头指针后移*/returnQueue[Front];}}/*==========================================*//*广度优先搜索==============================*//*==========================================*/voidBFS(intVertex){GraphSearchP;/*结点声明*/Enqueue(Vertex);/*存入队列中*/Visited[Vertex]=;/*已查找*/printf("[%d]==>",Vertex);while(Front!=Rear)/*队列为空时,结束循环*/{Vertex=Dequeue();SearchP=Head[Vertex].Next;while(SearchP!=NULL)/*读入邻接列表所有顶点*/{if(Visited[SearchP->Vertex]==0){Enqueue(SearchP->Vertex);/*存入队列中*/Visited[SearchP->Vertex]=1;/*已查找过的顶点*/printf("[%d]==>",SearchP->Vertex);}SearchP=SearchP->Next;/*下一个邻接点*/}}}/*==========================================*//*建立邻接顶点至邻接列表内==================*//*==========================================*/voidCreate_L_Graph(intVertex1,intVertex2){GraphSearchP;/*结点声明*/GraphNe
printf("Vertex[%d]:
Print_L_Graph(&Head[i]);/*调用输出邻接列表数据*/
printf("Depth-First-Search:
printf("[BEGIN]==>");
DFS
(1);
printf("[END]\n");
[2][3]*/
[1][4][5]*/
[1][6][7]*/
[2][8]*/
/*Vertex[6]:
[3][8]*/
/*Vertex[7]:
/*Vertex[8]:
[4][5][6][7]*/
/*Detph-First-Search:
/*[BEGIN]==>[1]==>[2]==>[4]==>[8]==>[5]==>[6]==>[3]==>[7]==>[END]*/
(四)图的广度优先遍历(搜索)。
/*查找顶点时,先将该顶点的邻接顶点皆存入队列中。
/*(关于队列的运用,之前的章节已有介绍,在此不再重复)*/
/*如果邻接顶点已存在放队列中或已查找,则不存入队列中,直到队列为空才结束查找工作。
#defineQueueMax10
intQueue[QueueMax];
intFront=-1;
intRear=-1;
/*队列的存入================================*/
intEnqueue(intVertex)
if(Rear>=QueueMax)/*队列已满*/
return-1;
else
Rear++;/*队列尾端指针后移*/
Queue[Rear]=Vertex;/*将值存入队列中*/
return1;
/*队列的取出================================*/
intDequeue()
if(Front>=Rear)/*队列已空*/
Front++;/*队头指针后移*/
returnQueue[Front];
/*广度优先搜索==============================*/
voidBFS(intVertex)
Enqueue(Vertex);/*存入队列中*/
Visited[Vertex]=;/*已查找*/
while(Front!
=Rear)/*队列为空时,结束循环*/
Vertex=Dequeue();
=NULL)/*读入邻接列表所有顶点*/
Enqueue(SearchP->Vertex);/*存入队列中*/
Visited[SearchP->Vertex]=1;/*已查找过的顶点*/
printf("[%d]==>",SearchP->Vertex);
GraphNe
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1