数据结构图的实验报告Word文档格式.docx

上传人:b****2 文档编号:14111582 上传时间:2022-10-18 格式:DOCX 页数:14 大小:45.29KB
下载 相关 举报
数据结构图的实验报告Word文档格式.docx_第1页
第1页 / 共14页
数据结构图的实验报告Word文档格式.docx_第2页
第2页 / 共14页
数据结构图的实验报告Word文档格式.docx_第3页
第3页 / 共14页
数据结构图的实验报告Word文档格式.docx_第4页
第4页 / 共14页
数据结构图的实验报告Word文档格式.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

数据结构图的实验报告Word文档格式.docx

《数据结构图的实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构图的实验报告Word文档格式.docx(14页珍藏版)》请在冰豆网上搜索。

数据结构图的实验报告Word文档格式.docx

friendistream&

operator>

>

(istream&

in,Graphmtx<

T,E>

&

G);

friendostream&

operator<

<

(ostream&

out,Graphmtx<

//输出

public:

Graphmtx(intsz=30,Emax=0);

//构造函数

~Graphmtx()//析构函数

{delete[]VerticesList;

delete[]Edge;

}

TgetValue(inti){

//取顶点i的值,i不合理返回0

returni>

=0&

i<

=numVertices?

VerticesList[i]:

NULL;

EgetWeight(intv1,intv2){//取边(v1,v2)上权值

returnv1!

=-1&

v2!

=-1?

Edge[v1][v2]:

0;

intNumberOfEdges(){returnnumEdges;

}//返回当前边数

intNumberOfVertices(){returnnumVertices;

}//返回当前顶点

intgetFirstNeighbor(intv);

//取顶点v的第一个邻接顶点

intgetNextNeighbor(intv,intw);

//取v的邻接顶点w的下一邻接顶点

boolinsertVertex(constT&

vertex);

//插入顶点vertex

boolinsertEdge(intv1,intv2,Ecost);

//插入边(v1,v2),权值为cost

boolremoveVertex(intv);

//删去顶点v和所有与它相关联的边

boolremoveEdge(intv1,intv2);

//在图中删去边(v1,v2)

intgetVertexPos(Tvertex){

//给出顶点vertex在图中的位置

for(inti=0;

numVertices;

i++)

if(VerticesList[i]==vertex)returni;

return-1;

//intnumVertexPos(Tvertex);

private:

intmaxVertices;

intnumEdges;

intnumVertices;

T*VerticesList;

//顶点表

E**Edge;

//邻接矩阵

constEmaxWeight;

};

(四)详细设计

函数通过调用图类中的函数实现一些功能。

头文件:

#include<

iostream.h>

assert.h>

constintmaxSize=50;

constintDefaultVertices=30;

//最大顶点数(=n)

constintmaxWeight=50;

其中顺序队列的实现:

template<

classT>

classSeqQueue

{

//循环队列的类的定义

public:

SeqQueue(intsz=10);

~SeqQueue(){delete[]elements;

}//析构函数

boolEnQueue(constT&

x);

//若队列不满,则将X进队,否则队溢出处理

boolDeQueue(T&

//若队列不为空,则函数返回TRUE及对头元素的值,否则返回FALSE

voidmakeEmpty(){front=rear=0;

}

//置空操作:

对头指针和队尾指针置0

boolIsEmpty()const{return(front==rear)?

true:

false;

//判队列空否,若队列空,则函数返回TRUE,否则返回FALSE

boolIsFull()const

{return((rear+1)%maxSize==front)?

//判队列满否,若队列满,则函数返回TRUE,否则返回FALSE

protected:

intrear,front;

//对头和队尾指针

T*elements;

//存放队列元素的数组

intmaxSize;

//队列最大可容纳元素个数

};

SeqQueue<

T>

:

SeqQueue(intsz):

front(0),rear(0),maxSize(sz)

//建立最大具有Maxsize个元素的空队列

elements=newT[maxSize];

//创建队列空间

assert(elements!

=NULL);

//断言:

动态存储分配成功与否

}

boolSeqQueue<

EnQueue(constT&

x)

//若队列不满,则将元素X插入到该队列的队尾,否则出错处理

if(IsFull()==true)returnfalse;

//队列满则插入失败,返回

elements[rear]=x;

//按照队尾指针指示位置插入

rear=(rear+1)%maxSize;

//队尾指针加1

returntrue;

//插入成功

DeQueue(T&

//若队列不空则函数推掉一个对头元素并返回TRUE,否则函数返回FALSE

if(IsEmpty()==true)returnfalse;

//若队列空则删除失败,返回

x=elements[front];

front=(front+1)%maxSize;

//对头指针加1

//删除成功

类的实现:

classT,classE>

Graphmtx<

Graphmtx(intsz,Emax):

maxWeight(max){//构造函数

maxVertices=sz;

numVertices=0;

numEdges=0;

inti,j;

VerticesList=newT[maxVertices];

//创建顶点表

Edge=(int**)newint*[maxVertices];

for(i=0;

maxVertices;

Edge[i]=newint[maxVertices];

//邻接矩阵

for(i=0;

i++)//矩阵初始化

for(j=0;

j<

j++)

Edge[i][j]=(i==j)?

0:

maxWeight;

intGraphmtx<

getFirstNeighbor(intv){

//给出顶点位置为v的第一个邻接顶点的位置,

//如果找不到,则函数返回-1

if(v!

=-1){

for(intcol=0;

col<

col++)

if(Edge[v][col]&

Edge[v][col]<

maxWeight)

returncol;

}

getNextNeighbor(intv,intw){

//给出顶点v的某邻接顶点w的下一个邻接顶点

w!

for(intcol=w+1;

col++)

界面:

cout<

"

=================================="

endl;

cout<

|1、输入一个图2、插入一个顶点|"

|3、插入一个边4、删除一个顶点|"

|5、删除一个边6、深度优先遍历|"

|7、广度优先遍历8、退出|"

然后进入循环进行功能操作

Case1中,输入一个图:

其中有操作符的重载,使图的信息直接输入:

istream&

operator>

(istream&

T,E>

G){

//通过从输入流in输入n个顶点信息和e条无向边的信息建立用邻接矩阵的图G。

//邻接矩阵初始化的工作已经在构造函数中完成、

inti,j,k,n,m;

Te1,e2;

Eweight;

in>

n>

m;

for(i=0;

i<

n;

i++){

in>

e1;

G.insertVertex(e1);

i=0;

while(i<

m){

e1>

e2>

weight;

j=G.getVertexPos(e1);

k=G.getVertexPos(e2);

if(j==-1||k==-1)

cout<

边两端信息有误,重新输入!

else{

G.insertEdge(j,k,weight);

i++;

returnin;

请输入图的信息:

cin>

g1;

break;

Case2中,是插入顶点,需要调用的函数有:

boolGraphmtx<

insertVertex(constT&

vertex){//插入顶点vertex

if(numVertices==maxVertices)returnfalse;

//顶点表满,不插入

VerticesList[numVertices++]=

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

当前位置:首页 > 总结汇报 > 工作总结汇报

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

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