实验六图基本操作的编程实现.docx

上传人:b****1 文档编号:2477768 上传时间:2022-10-30 格式:DOCX 页数:16 大小:17.76KB
下载 相关 举报
实验六图基本操作的编程实现.docx_第1页
第1页 / 共16页
实验六图基本操作的编程实现.docx_第2页
第2页 / 共16页
实验六图基本操作的编程实现.docx_第3页
第3页 / 共16页
实验六图基本操作的编程实现.docx_第4页
第4页 / 共16页
实验六图基本操作的编程实现.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

实验六图基本操作的编程实现.docx

《实验六图基本操作的编程实现.docx》由会员分享,可在线阅读,更多相关《实验六图基本操作的编程实现.docx(16页珍藏版)》请在冰豆网上搜索。

实验六图基本操作的编程实现.docx

实验六图基本操作的编程实现

实验六图基本操作的编程实现

【实验目的】

图基本操作的编程实现

要求:

图基本操作的编程实现(2学时,验证型),掌握图的建立、遍历、插入、删除等基本操作的编程实现,存储结构可以在顺序结构、链接结构、联合使用多种结构等中任选,也可以全部实现。

也鼓励学生利用基本操作进行一些应用的程序设计。

【实验性质】

验证性实验(学时数:

2H)

【实验内容】

编程对图进行存储(邻接矩阵或邻接表都可以,由学生自由选择),之后可以询问任何两个结点之间是否有通路和路径数。

设计一个将图形转成邻接链表的程序。

设计一个深度优先搜索法来查找图形的程序。

设计一个广度优先搜索法来查找一个图形的程序。

鼓励开发出难度更高的程序。

【思考问题】

1.图的定义和特性?

2.图的主要存储结构是什么?

是独立的某种还是多种数据结构的综合?

3.图的主要遍历思路是哪些?

4.举出图的应用范例?

【参考代码】

(一)将一个将图转成邻接矩阵的程序.

/*程序构思:

*/

/*用户输入结点与各个边,再将边转成邻接矩阵。

*/

#include

#defineMax6/*定义最大可输入的结点个数*/

intGraph[Max][Max];/*图形邻接数组*/

/*===============================================*/

/*输出邻接矩阵数据===============================*/

/*===============================================*/

voidprint_M_Graph()

{

inti,j;

printf("Vertice");

for(i=0;i

printf("%3d",i);

printf("\n");

for(i=0;i

{

printf("%4d",i);

for(j=0;j

printf("%3d");

printf("\n");

}

}

/*===============================================*/

/*以邻接矩阵建立图形=============================*/

/*===============================================*/

voidCreate_M_Graph(intVerticel,intVertice2)

{

Graph[Verticel][Vertice2]=1;/*将矩阵内容设为1*/

}

/*===============================================*/

/*主程序=========================================*/

/*===============================================*/

voidmain()

{

intSource;/*起始顶点*/

intDestination;/*终止顶点*/

inti,j;

for(i=0;i

for(j=0;j

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)/*出错:

超出范围*/

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*/

/*Pleaseinputthe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 职业规划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1