邻接表的实现,主要是一个顶点的表,其节点结构为VNode,同时各个VNode作为头节点,来链接起它的邻接点,它的邻接点为EdgeNode。
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();
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:
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;
}
main.cpp
#include"header.h"
int main()
{
ALGraph ag;
ag.DisPlay();
int x=9;
int y=10;
ag.InsertVex(x);
ag.InsertVex(y);
ag.InsertArc(x,y);
ag.DisPlay();
system("pause");
return 0;
}