今天在网上搜到《数据结构C++》里面关于构建图的源程序,虽然图类的成员函数定义不全,不过帮助已经是很大的了。
晚上好好分析分析。本科的时候学的,全还给老师了,呵呵
----------------------------------------------------------------------------------------------------------------------------------------------
源代码
#include <iostream>
using namespace std;
const int DefaultVertex=10;
template<class NameType,class DistType> class Graph;
template <class DistType> struct Edge
{
friend class Graph<class NameType,DistType>;
int dest;
DistType cost;
Edge <DistType> *link;
Edge(){}
Edge(int D,DistType C):dest(D),cost(C),link(NULL){}
int operator!=(const Edge<DistType>&E)const{return dest!=E.dest;}
};
template <class NameType,class DistType>struct Vertex
{
friend class Edge<DistType>;
friend class Graph<NameType,DistType>;
NameType data;
Edge<DistType> *adj;
};
/图类定义//
template <class NameType,class DistType>
class Graph
{
private:
Vertex<NameType,DistType> *NodeTable;
int NumVertices;
int MaxNumVertices;
int NumEdges;
int GetVertexPos(const NameType &vertex)
{
for(int i=0;i<NumVertices;i++)
{
if(NodeTable[i].data==vertex)return i;
}
return -1;
}
public:
int NumberOfVertiecs(){ return NumVertices; }
int NumberOfEdges(){ return NumEdges; }
void InsertVertex(const NameType &Vertex);
void InsertEdge(int, int, DistType);
void RemoveVertex(int a);
void RemoveEdge(int,int);
//template<class NameType,class DistType>
Graph(int sz=DefaultVertex):NumVertices(0),MaxNumVertices(sz),NumEdges(0)
{
int n,e,k,j;
NameType name,tail,head;
DistType weight;
NodeTable=new Vertex<NameType, DistType>[MaxNumVertices];
cout<<"cin>>n/n";
cin>>n;
for(int i=0;i<n;i++){
cout<<"cin>>name/n";
cin>>name;InsertVertex(name);
}
cout<<"cin>>e;";
cin>>e;
for(int i=0;i<e;i++)
{
cout<<"cin>>tail>>head>>weight; ";
cin>>tail>>head>>weight;
k=GetVertexPos(tail);j=GetVertexPos(head);
InsertEdge(k,j,weight);
}
}
int GetFirstNeighbor(int v)
{
if(v!=-1)
{
Edge<DistType> *p=NodeTable[v].adj;
if(!p=NULL)return p->dest;
}
return -1;
}
//template<class NameType,class DistType>
int GetNextNeighbor(int v1,int v2)
{
if(v1!=-1)
{
Edge<DistType> *p=NodeTable[v1].adj;
while(!p=NULL)
{
if(p->dest==v2&&p->link!=NULL)return p->link->dest;
else p=p->link;
}
}
return -1;
}
//template<class NameType,class DistType>
DistType GetWeight(int,int);
};
template<class NameType,class DistType>
DistType Graph<NameType,DistType>::GetWeight(int v1,int v2)
{
if(v1!=-1&&v2!=-1)
{
Edge<DistType> *p=NodeTable[v1].adj;
while(p!=NULL)
{
if(p->dest==v2)return p->cost;
else
p=p->link;
}
}
return 0;
}
template<class NameType,class DistType>//插入顶点
void Graph<NameType,DistType> ::InsertVertex(const NameType &Vertex)
{
if(NumVertices==MaxNumVertices)return;
NumVertices++;
NodeTable[NumVertices].data=Vertex;
NodeTable[NumVertices].adj=NULL;
}
template<class NameType,class DistType>//删除顶点
void Graph<NameType,DistType> ::RemoveVertex(int v)
{
if(v<0||v>=NumVertices) return ;
Edge<DistType> *p=NodeTable[i].adj;
while(p!=NULL)
{ RemoveEdge(p->Dest,v);
RemoveEdge(v,p->Dest);
p=p->link;
}
for(int i=v;i<NumVertices-1;i++)
{
NodeTable[i].data=NodeTable[i+1].data;
NodeTable[i].adj=NodeTable[i+1].adj;
}
NodeTable[NumVertices-1].adj=NULL;
}
template<class NameType,class DistType>//插入边
void Graph<NameType,DistType> ::InsertEdge(int v1,int v2,DistType weight)
{ Edge<DistType> *p=new Edge<DistType>(v2,weight);//?????????????
p->link=NodeTable[v1].adj;
NodeTable[v1].adj=p;
}
template<class NameType,class DistType>//删除边
void Graph<NameType,DistType> ::RemoveEdge(int v1,int v2)
{
Edge<DistType> *p=NodeTable[v1].adj;
Edge<DistType> *q=p;
while(p!=NULL&&p->Dest==v2)
{q=p;p=p->link;}
if(p!=NULL)
{q->link=p->link;delete p;}
}
#include "stdafx.h"
#include "./graph.h"
int _tmain(int argc, _TCHAR* argv[])
{
Graph<int,int> g(20);
}