作业16.docx

上传人:b****5 文档编号:6705673 上传时间:2023-01-09 格式:DOCX 页数:21 大小:17.85KB
下载 相关 举报
作业16.docx_第1页
第1页 / 共21页
作业16.docx_第2页
第2页 / 共21页
作业16.docx_第3页
第3页 / 共21页
作业16.docx_第4页
第4页 / 共21页
作业16.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

作业16.docx

《作业16.docx》由会员分享,可在线阅读,更多相关《作业16.docx(21页珍藏版)》请在冰豆网上搜索。

作业16.docx

作业16

分别使用邻接矩阵和邻接表实现图数据结构及其相关操作:

(1)实现构造和析构函数;

(3)图的数据(顶点和边信息)从终端输入,实现相应的输入函数;

(4)统计每一个顶点的度(视为有向图时,分别给出入度和出度);

(5)给出图的DFS和BFS遍历序列;

(6)当此图为有向无环图时,给出拓扑排序序列;

template

classLLN

{

public:

LLN(Tx,LLN*l);

virtual~LLN();

Tdata;

LLN*link;

};

 

template

LLN:

:

LLN(Tx,LLN*l)

{

data=x;

link=l;

}

template

LLN:

:

~LLN()

{

}

#include"LLN.h"

template

classLS

{

public:

boolGetTop(T&x);

//TGetTop();

voidpush(Tx);

boolpop();

boolIsEmpty();

LS();

virtual~LS();

LLN*top;

};

template

LS:

:

LS()

{

top=NULL;

}

template

LS:

:

~LS()

{

while(top)

{

LLN*p=top->link;

deletetop;

top=p;

}

}

template

boolLS:

:

IsEmpty()

{

returntop==NULL?

true:

false;

}

template

boolLS:

:

pop()

{

if(IsEmpty())

returnfalse;

else

{

LLN*p=top;

top=top->link;

deletep;

returntrue;

}

}

template

voidLS:

:

push(Tx)

{

top=newLLN(x,top);

}

 

template

boolLS:

:

GetTop(T&x)

{

if(IsEmpty())

returnfalse;

else

{

x=top->data;

returntrue;

}

}

#include

#include

template

classQueC

{

public:

voidMakeEmpty();

boolIsFull();

boolIsEmpty();

TGetFront();

boolDeQ();

boolEnQ(Tx);

QueC(intsz=10);

virtual~QueC();

intrear,front;

T*elements;

intmaxSize;

};

template

QueC:

:

QueC(intsz):

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

{

elements=newT[maxSize];

assert(elements!

=NULL);

}

template

QueC:

:

~QueC()

{

delete[]elements;

}

template

boolQueC:

:

EnQ(Tx)

{

if(IsFull()==true)

returnfalse;

elements[rear]=x;

rear=(rear+1)%maxSize;

returntrue;

}

template

boolQueC:

:

DeQ()

{

if(IsEmpty()==true)

returnfalse;

front=(front+1)%maxSize;

returntrue;

}

template

TQueC:

:

GetFront()

{

if(IsEmpty()==true)

returnfalse;

returnelements[front];

}

template

boolQueC:

:

IsEmpty()

{

return(front==rear)?

true:

false;

}

template

boolQueC:

:

IsFull()

{

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

true:

false;

}

template

voidQueC:

:

MakeEmpty()

{

front=rear=0;

}

 

classGM

{

public:

voidTopoSt();

voidBFS();

voidDFS();

voidDFS(inti,bool*v);

intInRank(intn);

intRank(intm);

boolCreate();

int*edge;

char*ver;

intnumV;

intmaxV;

GM(intsz);

virtual~GM();

};

#include"stdafx.h"

#include"GM.h"

#include

#include"QueC.h"

#include"LS.h"

GM:

:

GM(intsz)

{

maxV=sz;

ver=newchar[sz];

edge=newint[sz*sz];

numV=0;

}

GM:

:

~GM()

{

delete[]edge;

delete[]ver;

}

boolGM:

:

Create()

{

cout<<"输入顶点个数(顶点个数不能大于"<

"<

cin>>numV;

if(numV>maxV)

{

cout<<"error!

"<

returnfalse;

}

else

{

cout<<"输入顶点信息数组:

"<

for(inti=0;i

{

cin>>ver[i];

}

cout<<"输入边信息数组(1有关,0无关):

"<

for(i=0;i

{

cin>>edge[i];

}

cout<<"边信息数组为:

"<

for(i=0;i

{

cout<

if((i+1)%numV==0)

cout<

}

returntrue;

}

}

intGM:

:

Rank(intm)

{

if((m<0)||(m>numV))

{

return-1;

}

else

{

intcount=0;

for(inti=m*numV;i<((m+1)*numV);i++)

{count=count+edge[i];}

returncount;

}

}

intGM:

:

InRank(intn)

{

if((n<0)||(n>numV))

{

return-1;

}

else

{

intcount=0;

for(intj=n;j

{

count=count+edge[j];

}

returncount;

}

}

voidGM:

:

DFS(inti,bool*v)

{

v[i]=true;

cout<

for(intj=i*numV;j<((i+1)*numV);j++)

{

if(!

v[j%numV]&&edge[j]==1)//j%numV是对应顶点的列号,

DFS(j%numV,v);//v[j%numV]判断顶点是否被访问过

}

}

voidGM:

:

DFS()

{

bool*v=newbool[numV];

for(inti=0;i

v[i]=false;

for(i=0;i

{

if(v[i])

continue;

DFS(i,v);

cout<

}

delete[]v;

}

 

voidGM:

:

BFS()

{

bool*v=newbool[numV];

for(inti=0;i

v[i]=false;

QueCq;

for(i=0;i

{

if(v[i])

continue;

else

{

q.EnQ(i);

cout<

v[i]=true;

while(!

q.IsEmpty())

{

intn=q.GetFront();

q.DeQ();

for(intj=n*numV;j<((n+1)*numV);j++)

{

if(!

v[j%numV]&&edge[j]==1)

{

v[j%numV]=true;

cout<

q.EnQ(j%numV);

}

}

}

cout<

}

}

}

voidGM:

:

TopoSt()

{

cout<<"拓扑排序:

";

LSls;

intIR[81];

for(inti=0;i

{

IR[i]=InRank(i);

}

for(i=0;i

{

if(IR[i]==0)

ls.push(i);

}

intcount=0;

while(!

ls.IsEmpty())

{

inttop;

ls.GetTop(top);

cout<

ls.pop();

count++;

for(intj=top*numV;j<((top+1)*numV);j++)

{

if(edge[j]==1)

{

IR[j%numV]--;

if(IR[j%numV]==0)

ls.push(j%numV);

}

}

}

if(count

{

cout<<"存在有向环。

"<

}

}

 

#include"stdafx.h"

#include"GM.h"

#include

intmain(intargc,char*argv[])

{

GMgm(10);

gm.Create();

inti;

cout<<"输入一个顶点号:

";

cin>>i;

cout<<"此顶点的出度:

"<

cout<<"此顶点的入度:

"<

cout<<"深度优先:

";

gm.DFS();

cout<<"广度优先:

";

gm.BFS();

gm.TopoSt();

cout<

return0;

}

 

classVertex

{

public:

Vertex();

virtual~Vertex();

chardata;

Edge*adj;

};

#include"stdafx.h"

#include"Vertex.h"

Vertex:

:

Vertex()

{

}

Vertex:

:

~Vertex()

{

}

classEdge

{

public:

Edge(intnum);

virtual~Edge();

intdest;

Edge*link;

};

 

#include"stdafx.h"

#include"Edge.h"

Edge:

:

Edge(intnum)

{

dest=num;

link=NULL;

}

Edge:

:

~Edge()

{

}

 

#include"Vertex.h"

classGL

{

public:

voidTopoSt();

voidBFS();

voidDFS();

intInRank(intj);

intRank(inti);

boolCreate();

GL(intsz);

virtual~GL();

intmaxV;

intnumV;

Vertex*NodeTable;

protected:

voidDFS(inti,bool*v);

};

 

#include"stdafx.h"

#include"GL.h"

#include

#include"QueC.h"

#include"LS.h"

GL:

:

GL(intsz)

{

maxV=sz;

NodeTable=newVertex[sz];

numV=0;

}

GL:

:

~GL()

{

for(inti=0;i

{

Edge*p=NodeTable[i].adj;

Edge*q;

while(p)

{

q=p->link;

deletep;

p=q;

}

}

}

boolGL:

:

Create()

{

cout<<"输入顶点个数(顶点个数不能大于"<

"<

cin>>numV;

if(numV>maxV)

{

cout<<"error!

"<

returnfalse;

}

else

{

cout<<"输入顶点的信息:

"<

for(inti=0;i

{

cin>>NodeTable[i].data;

NodeTable[i].adj=NULL;

}

cout<<"输入边的信息,输入中含-1时停止:

"<

while

(1)

{

intve,de;

cin>>ve>>de;

if((ve==-1)||(de==-1))

break;

Edge*p=newEdge(NULL);

p->dest=de;

p->link=NodeTable[ve].adj;

NodeTable[ve].adj=p;

}

returntrue;

}

}

intGL:

:

Rank(inti)

{

if((i<0)||(i>numV))

{

return-1;

}

else

{

Edge*p=NodeTable[i].adj;

intcount=0;

while(p)

{

++count;

p=p->link;

}

returncount;

}

}

intGL:

:

InRank(intj)

{

if((j<0)||(j>numV))

{

return-1;

}

else

{

intcount=0;

for(inti=0;i

{

Edge*p=NodeTable[i].adj;

while(p)

{

if(p->dest==j)

++count;

p=p->link;

}

}

returncount;

}

}

voidGL:

:

DFS(inti,bool*v)

{

v[i]=true;

cout<

for(Edge*p=NodeTable[i].adj;p;p=p->link)

{

if(!

v[p->dest])

DFS(p->dest,v);

}

}

voidGL:

:

DFS()

{

bool*v=newbool[numV];

for(inti=0;i

v[i]=false;

for(i=0;i

{

if(v[i])

continue;

DFS(i,v);

cout<

}

delete[]v;

}

 

voidGL:

:

BFS()

{

bool*v=newbool[numV];

for(inti=0;i

v[i]=false;

QueCq;

for(i=0;i

{

if(v[i])

continue;

else

{

q.EnQ(i);

cout<

v[i]=true;

while(!

q.IsEmpty())

{

intn=q.GetFront();

q.DeQ();

for(Edge*p=NodeTable[n].adj;p;p=p->link)

{

if(!

v[p->dest])

{

v[p->dest]=true;

cout<dest].data<<"";

q.EnQ(p->dest);

}

}

}

cout<

}

}

}

voidGL:

:

TopoSt()

{

cout<<"拓扑排序:

";

LSls;

intIR[81];

for(inti=0;i

{

IR[i]=InRank(i);

}

for(i=0;i

{

if(IR[i]==0)

ls.push(i);

}

intcount=0;

while(!

ls.IsEmpty())

{

inttop;

ls.GetTop(top);

cout<

ls.pop();

count++;

for(Edge*p=NodeTable[top].adj;p;p=p->link)

{

IR[p->dest]--;

if(IR[p->dest]==0)

ls.push(p->dest);

}

}

if(count

{

cout<<"存在有向环。

"<

}

}

LS.h/LLN.h/QueC.h是模板

#include"stdafx.h"

#include"GL.h"

#include

intmain(intargc,char*argv[])

{

GLgl(10);

gl.Create();

intm;

cout<<"输入一个顶点号:

";

cin>>m;

cout<<"此顶点的出度:

"<

cout<<"此顶点的入度:

"<

cout<<"深度优先:

";

gl.DFS();

cout<<"广度优先:

";

gl.BFS();

gl.TopoSt();

cout<

return0;

}

 

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

当前位置:首页 > 医药卫生 > 基础医学

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

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