header.h
#include<iostream>
using namespace std;
const int MaxVertexNum=20;
struct EdgeNode{
int adjvex;
struct EdgeNode * next;
};
struct VNode{
int data;
EdgeNode * firstedge;
};
class ALGraph{
public:
ALGraph();
~ALGraph();
void DFS(int index);
int FirstAdjVex(int v);
int NextAdjVex(int v,int w);
bool DFSTraverse();
int LocateVex(int u);
ALGraph & InsertVex(int v);
ALGraph & DeleteVex(int v);
ALGraph & InsertArc(int v,int w);
ALGraph & DeleteArc(int v,int w);
void DisPlay();
VNode vertices[MaxVertexNum];
private:
bool visited[MaxVertexNum];
int vexnum;
int arcnum;
};
header。cpp
#include"header.h"
int ALGraph::LocateVex(int u)
{
for(int i=0;i<vexnum;i++)
if(vertices[i].data==u)
return i;
return -1;
}
ALGraph::ALGraph()
{
int i,j,k;
int v1,v2;
cout<<"请输入图的顶点数,边数:";
cin>>vexnum>>arcnum;
cout<<"请输入"<<vexnum<<"个顶点的值:";
for(i=0;i<vexnum;i++)
{
cin>>vertices[i].data;
vertices[i].firstedge=NULL;
}
for(k=0;k<arcnum;k++)
{
cout<<"请输入一条弧的弧尾、弧头:";
cin>>v1>>v2;
i=LocateVex(v1);
j=LocateVex(v2);
EdgeNode * p=new EdgeNode;
p->adjvex=j;
p->next=vertices[i].firstedge;
vertices[i].firstedge=p;
}
}
ALGraph & ALGraph::InsertVex(int u)
{
if(vexnum>MaxVertexNum)
{
cout<<"顶点数超出范围,无法插入!";
exit(1);
}
if(LocateVex(u)>=0)
{
cout<<"顶点已存在";
exit(1);
}
vertices[vexnum].data=u;
vertices[vexnum].firstedge=NULL;
vexnum++;
return *this;
}
ALGraph & ALGraph::DeleteVex(int v)
{
int i,j;
EdgeNode * p,* q;
i=LocateVex(v);
p=vertices[i].firstedge;
while(p)
{
q=p;
p=p->next;
delete q;
arcnum--;
}
vexnum--;
for(j=i;j<vexnum;j++)
vertices[j]=vertices[j+1];
for(j=0;j<vexnum;j++)
{
p=vertices[j].firstedge;
while(p)
{
if(p->adjvex==i)
{
if(p==vertices[j].firstedge)
{
vertices[j].firstedge=p->next;
delete p;
p=vertices[j].firstedge;
arcnum--;
}
else
{
q->next=p->next;
delete p;
p=q->next;
arcnum--;
}
}
else
{
if(p->adjvex>i)//由于节点少了一个,故对于》i的节点采取了--的操作。
p->adjvex--;
q=p;
p=p->next;
}
}
}
return *this;
}
ALGraph & ALGraph::InsertArc(int v,int w)
{
EdgeNode * p;
int i,j;
i=LocateVex(v);
j=LocateVex(w);
if(i<0||j<0)
{
cout<<"顶点不存在!";
exit(1);
}
arcnum++;
p=new EdgeNode;
p->adjvex=j;
p->next=NULL;
p->next=vertices[i].firstedge;
vertices[i].firstedge=p;
return *this;
}
ALGraph & ALGraph::DeleteArc(int v,int w)//注意此处删除的是弧,而非边
{
EdgeNode * p,* q;
int i,j;
i=LocateVex(v);
j=LocateVex(w);
if(i<0||j<0||i==j)
{
cout<<"边不存在!";
exit(1);
}
p=vertices[i].firstedge;
while(p&&p->adjvex!=j)
{
q=p;
p=p->next;
}
if(p&&p->adjvex==j)
{
if(p==vertices[i].firstedge)
vertices[i].firstedge=p->next;
else
q->next=p->next;
delete p;
arcnum--;
}
/*if(p&&p->adjvex==i)
{
if(p==vertices[j].firstedge)
vertices[j].firstedge=p->next;
else
q->next=p->next;
delete p;
}*/
return *this;
}
void ALGraph::DisPlay()
{
int i;
EdgeNode * p;
cout<<"有向图的"<<vexnum<<"个顶点:"<<endl;
for(i=0;i<vexnum;i++)
cout<<vertices[i].data<<" ";
cout<<endl;
cout<<"有向图的"<<arcnum<<"条弧:"<<endl;
for(i=0;i<vexnum;i++)
{
p=vertices[i].firstedge;
while(p!=NULL)
{
cout<<vertices[i].data<<"->"<<vertices[p->adjvex].data<<'\t';
cout<<endl;
p=p->next;
}
}
}
ALGraph::~ALGraph()
{
int i;
EdgeNode * p,* q;
for(i=0;i<vexnum;i++)
{
p=vertices[i].firstedge;
while(p)
{
q=p->next;
delete p;
p=q;
}
}
arcnum=0;
vexnum=0;
}
void ALGraph::DFS(int index)
{
int v1;
int i;
visited[index]=true;
v1=vertices[index].data;
cout<<v1<<" ";
for(i=FirstAdjVex(v1);i>=0;i=NextAdjVex(v1,vertices[i].data))
if(!visited[i])
DFS(i);
}
int ALGraph::FirstAdjVex(int v)
{
int i=LocateVex(v);
EdgeNode * p=vertices[i].firstedge;
if(p)
return p->adjvex;
else
return -1;
}
int ALGraph::NextAdjVex(int v,int w)
{
EdgeNode * p;
int i=LocateVex(v);
int j=LocateVex(w);
p=vertices[i].firstedge;
while(p&&(p->adjvex!=j))
{
p=p->next;
}
if(!p||!p->next)
return -1;
else
return p->next->adjvex;
}
bool ALGraph::DFSTraverse()
{
int i;
for(i=0;i<vexnum;i++)
visited[i]=false;
for(i=0;i<vexnum;i++)
{
if(!visited[i])
DFS(i);
}
cout<<endl;
return true;
}
main.cpp
#include"header.h"
int main()
{
ALGraph ag;
cout<<"深度优先搜索为:";
ag.DFSTraverse();
cout<<endl;
ag.DisPlay();
int x=9;
int y=10;
ag.InsertVex(x);
ag.InsertVex(y);
ag.InsertArc(x,y);
ag.DisPlay();
system("pause");
return 0;
}
算法思想与基于邻接矩阵的深度优先搜索一致