图的邻接表实现:
Graph2.h 文件
#include<iostream.h>/////////////// 邻接表实现图
const Maxvertexes=4;
template<class VertexType,class ArcType>struct ArcNode{ // 弧节点
int adjex; // 相关连的接点箭头头部节点的序号
ArcType weight;
ArcNode<VertexType,ArcType> *nextarc;// 下条弧指针
};
template<class VertexType,class ArcType>struct VertexNode{// 顶点接点
VertexType data;
ArcNode<VertexType,ArcType> *firstarc;
};
template<class VertexType,class ArcType>class Graph{
private:
VertexNode<VertexType,ArcType>* vertextable;
int currentnumarc; // 当前边数目
int currentnumvertex; // 当前顶点数
public:
Graph(VertexType v[],int num=Maxvertexes);
~Graph();
int InsertVertex(VertexType v){
if (currentnumvertex<=Maxvertexes){
vertextable[currentnumvertex].data=v;
vertextable[currentnumvertex].firstarc=NULL;
currentnumvertex++;
return 1;
}
cout<<"Insert unsuccessful ,the num vertex is full"<<endl;
return 0;
}
int InsertArc(int tail,int head,ArcType weight){
//////////////////@@@@@@@@@@@@@@@@@@@@@@@@@ 注意这里的 tali head 是序列号是,数组的下标 晕死了 !!
if(tail>currentnumvertex-1){
cout<<"the vertex(tail) not exist"<<endl; return 0;}
if(head>currentnumvertex-1){
cout<<"the vertex(head) not exist"<<endl; return 0;}
ArcNode<VertexType,ArcType> *p=vertextable[tail].firstarc;
if(p==NULL){//p 插入的要为第一个边
ArcNode<VertexType,ArcType> *temp=new ArcNode<VertexType,ArcType>;
temp->weight=weight;
temp->nextarc=NULL;
temp->adjex=head;
//cout<<head<<" ";
vertextable[tail].firstarc=temp;
currentnumarc++;
return 1;
}
while(p->nextarc!=NULL) p=p->nextarc;
ArcNode<VertexType,ArcType> *temp=new ArcNode<VertexType,ArcType>;
temp->weight=weight;
temp->nextarc=NULL;
temp->adjex=head;
// cout<<head<<" ";
p->nextarc=temp;
currentnumarc++;
return 1;
}
int Getpos(VertexType v){
for(int i=0;i<currentnumvertex;i++){
if(vertextable[i].data==v){
//cout<<" 等于 "<<endl;
return i;
}
}
return -1;
}
void printvertex(){
for(int i=0;i<currentnumvertex;i++)
cout<<vertextable[i].data;
}
int Getcurrentvertex()
{ return currentnumvertex;}
int Getcurrentnumarc()
{ return currentnumarc;}
void visite(int i){
cout<<vertextable[i].data<<" ";
}
void DFTraverse(); // 深度优先遍历图
void DFS(const int v,int visited[]);
int Getnextneighbor(int v);// 取下标 V 的第一个邻接点下标的下标
int Getnextneighbor(int v,int w);// 取下标 V 的位于下标 W 后的邻接点的下标
void print1(){
ArcNode<VertexType,ArcType> *p;
for(int i=0;i<currentnumvertex;i++){
cout<<vertextable[i].data<<":";
p=vertextable[i].firstarc;
while(p!=NULL)
{ cout<<"->["<<p->adjex<<"]"<<vertextable[p->adjex].data;
p=p->nextarc;
}
cout<<endl;
}
}
};
template<class VertexType,class ArcType>Graph<VertexType,ArcType>:: Graph(VertexType v[],int num=Maxvertexes)
:currentnumarc(0),currentnumvertex(0)
{ int e,t,h;
VertexType tail,head;
ArcType w;
vertextable=new VertexNode<VertexType,ArcType>[Maxvertexes];
for(int i=0;i<Maxvertexes;i++)
InsertVertex(v[i]);
cout<<"please input the num of arc:";
cin>>e;
for(i=0;i<e;i++){
cin>>tail>>head>>w; // 逐条输入边和权值 tail 为开始接点, head 为结束点
if((t=Getpos(tail))==-1){
cout<<"the vertex(tail) not exist"<<endl;return ;}
else if((h=Getpos(head))==-1){
cout<<"the vertex(head) not exist"<<endl;return ;}
else InsertArc(t,h,w); //insert border from t ---->h the weight is w
}
}
template<class VertexType,class ArcType>Graph<VertexType,ArcType>:: ~Graph()
{ ArcNode<VertexType,ArcType> *p1,*p2;
for(int i=0;i<currentnumvertex;i++){
p1=vertextable[i].firstarc;
if(p1!=NULL){
if(p1->nextarc==NULL){
delete p1;
return ;
}
p2=p1->nextarc;
delete p1;
p1=p2;
p2=p2->nextarc;
}
}
}
template<class VertexType,class ArcType>void Graph<VertexType,ArcType>::DFTraverse(){
int i,n=currentnumvertex;
int *visited=new int[n];
for(i=0;i<n;i++)
visited[i]=0;
for(i=0;i<n;i++){
if(visited[i]==0) DFS(i,visited);
}
delete []visited;
}
template<class VertexType,class ArcType>void Graph<VertexType,ArcType>::DFS(const int v,int visited[]){
visite(v);
visited[v]=1;
int w;
w=Getnextneighbor(v);// get first next vertex
while(w!=-1){
if(visited[w]==0) DFS(w,visited);
w=Getnextneighbor(v,w); // 取 v 的排在 w 后的下一个邻接点 ,now the w is visited and muset 回溯
}
}
template<class VertexType,class ArcType>int Graph<VertexType,ArcType>::Getnextneighbor(int v){
if(vertextable[v].firstarc!=NULL)
return (vertextable[v].firstarc->adjex);
else return -1;
}
template<class VertexType,class ArcType>int Graph<VertexType,ArcType>::Getnextneighbor(int v,int w){
ArcNode<VertexType,ArcType> *p=vertextable[v].firstarc;
while(p!=NULL){
if(p->adjex==w){
if(p->nextarc!=NULL) return p->nextarc->adjex;
else return -1;
}
p=p->nextarc;
}
return -1;
}
主测试函数 main.cpp
#include<iostream.h>
//#include"heap.h"
//#include"heap2.h"
//#include"graph.h"
#include"graph2.h"
//#include"huffmantree.h"
int main()
{/////////////////////////////////////////////test Graph<> 有向图邻接表实现 + 图的两种遍历算法
char v[4];
for(int i=0;i<4;i++)
cin>>v[i];
Graph<char,int> graph(v);
graph.printvertex(); cout<<endl;
cout<<"num of arc:"<<graph.Getcurrentnumarc()<<endl;
cout<<"num of vertex:"<<graph.Getcurrentvertex()<<endl;
cout<<"DFT is( 深度优先便历 ):";graph.DFTraverse();
cout<<endl;
cout<<"Vertextable:( 邻接表 )"<<endl;
graph.print1();
//cout<<endl;
//cout<<graph.Getnextneighbor(0,1)<<endl;
return 0;
}