图的构建(C++源代码)--贝叶斯网络编程的第一步
来源:互联网 发布:web前端图片 优化 编辑:程序博客网 时间:2024/05/05 11:14
今天在网上搜到《数据结构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);
}
- 图的构建(C++源代码)--贝叶斯网络编程的第一步
- 贝叶斯网络的构建-学习-推理(C++源代码)分析
- Java网络编程-用SSL构建安全的Socket
- Java网络编程-用SSL构建安全的Socket
- Java网络编程-用SSL构建安全的Socket
- 编程的第一步
- C的第一步
- C语言的第一步
- 编译第一步:去源代码(C)注释
- 证卷指数追踪 - 迈出构建投资理财辅助平台的第一步
- c的网络编程
- 踏入linux编程的第一步
- 我迈开的编程第一步
- 《unix网络编程》(1)源代码的使用方法
- 关于C语言的第一步
- 走上不归路的第一步-C
- 图论的第一步
- 构建基本的Linux C 编程环境
- 数据库设计方法,规范与技巧
- Struts 2 和Struts 1对比
- ADO.NET调用存储过程,过程比较全
- 程序员是如何喝酒的
- 基于ARM的嵌入式linux系统设计
- 图的构建(C++源代码)--贝叶斯网络编程的第一步
- ResultSet 对象getTables()的用法 获取表的相关信息
- C# 2.0中泛型编程初级入门教程
- 在Java中使用Oracle blob
- 常识(5)- 使用动态SQL语句(Using dynamic SQL),SQLSA,SQLDA
- 《青 春》 塞缪尔 厄尔曼
- 上海杰得宣布成功移植Android
- 开题报告 opening report verion 2
- 东张西望