C++十字链表实现有向图
来源:互联网 发布:python 文件读写 删除 编辑:程序博客网 时间:2024/05/11 03:22
/*********************十字链表实现有向图******************///边表节点template<class Type>class EdgeNode{public: int tailvex;//弧起点在顶点表的下标 int headvex;//弧终点在顶点表的下标 //int weight;用于存储权值非网图不需要 EdgeNode *headlink;//入边表指针域,指向相同终点的下一条边 EdgeNode *taillink;//边表指针域,指向起点相同的下一条边 EdgeNode(int tailv, int headv, EdgeNode *headl = nullptr, EdgeNode *taill = nullptr) :tailvex(tailv), headvex(headv), headlink(headl), taillink(taill){}};//顶点表节点template<class Type>class VertexNode{public: Type data;//节点数据域 EdgeNode<Type> *firstin;//指向入边表节点 EdgeNode<Type> *firstout;//指向出边表节点 VertexNode(){} VertexNode(Type d, EdgeNode<Type> *fir_in = nullptr, EdgeNode<Type> *fir_out = nullptr) :data(d), firstin(fir_in), firstout(fir_out){}};//有向图类(十字链表)template<class Type>class DirectedGraph{private: VertexNode<Type> OrthogonalList[MAX];//有向图顶点集合 int numVertexe;//当前图顶点数 int numEdges;//当前图的弧数 int getPosition(Type el);//获得el在顶点表中的位置public: DirectedGraph();//构造图 ~DirectedGraph();//析构函数 void print();};//析构函数template<class Type>DirectedGraph<Type>::~DirectedGraph(){ cout << "析构函数" << endl;}//获得el在顶点表中的位置template<class Type>int DirectedGraph<Type>::getPosition(Type el){ int i; for (i = 0; i < this->numVertexe; i++) { if (this->OrthogonalList[i].data == el) return i; } return -1;}//创建图(使用十字链表法)template<class Type>DirectedGraph<Type>::DirectedGraph(){ Type c1, c2; int p1, p2; int i; EdgeNode<Type> *enode;//弧节点 cout << "Input vertexe number: "; cin >> this->numVertexe; cout << "Input edge number: "; cin >> this->numEdges; //初始化顶点 for (i = 0; i < this->numVertexe; i++) { cout << "Input a Type data(" << i + 1 << "): "; cin >> this->OrthogonalList[i].data; this->OrthogonalList[i].firstin = nullptr; this->OrthogonalList[i].firstout = nullptr; } //初始化弧 //输入各弧并构造十字链表 for (i = 0; i < this->numEdges; i++) { cout << "edge" << i << endl; cout << "Input arcHead: "; cin >> c1; cout << "Input arcTail: "; cin >> c2; p1 = getPosition(c1); p2 = getPosition(c2); enode = new EdgeNode<Type>(p1, p2, nullptr, nullptr);//边节点 if (this->OrthogonalList[p1].firstout==nullptr) this->OrthogonalList[p1].firstout = enode; else { EdgeNode<Type> *move = this->OrthogonalList[p1].firstout; while (move->taillink != nullptr) move = move->taillink; move->taillink = enode; } if (this->OrthogonalList[p2].firstin==nullptr) this->OrthogonalList[p2].firstin = enode; else { EdgeNode<Type> *move = this->OrthogonalList[p2].firstin; while (move->headlink != nullptr) move = move->headlink; move->headlink = enode; } }}//打印十字链表template<class Type>void DirectedGraph<Type>::print(){ EdgeNode<Type> *move; cout << "打印出边表:" << endl; for (int i = 0; i < this->numVertexe; i++) { cout << "Vertexe( " << i << " ): "; move = this->OrthogonalList[i].firstout; while (move != nullptr) { cout << move->headvex << " "; move = move->taillink; } cout << endl; } cout << "打印入边表:" << endl; for (int i = 0; i < this->numVertexe; i++) { cout << "Vertexe( " << i << " ): "; move = this->OrthogonalList[i].firstin; while (move != nullptr) { cout << move->tailvex << " "; move = move->headlink; } cout << endl; }}//测试程序int main(){ DirectedGraph<char> graph; graph.print(); return 0;}
0 0
- 数据结构:有向图的十字链表存储表示(c实现)
- c语言实现十字链表储存有向图(《数据结构》算法7.3)
- C++十字链表实现有向图
- 有向图的十字链表
- 十字链表存储有向图
- 基于Java实现的十字链表(用来表示有向图)
- 有向图的十字链表存储表式
- 建立一个有向图的十字链表
- 2011.6.18 图论,有向图的十字链表
- 有向图的十字链表存储
- 有向图的十字链表存储形式
- 有向图的十字链表存储结构
- 有向图的十字链表存储
- 有向图的十字链表存储方法
- 用十字链表表示的有向图
- 有向图的十字链表表示
- (学习java)用十字链表表示有向图
- 有向图的十字链表存储以及相关操作
- chatr.js 制作漂亮的图表
- vue环境搭建vue-cli,vue-router,webpack
- Jetty的安装、配置和使用
- 裁剪网格纸
- 用户注册时,Yii利用AJAX页面实时提示用户名已经存在
- C++十字链表实现有向图
- C发展史:K&R C/C89/C99/C11 以及 C++发展史: C++98/C++03/C++11
- 全面了解CPPUnit
- postgresql查询栅格数据范围(extent)
- Glide的使用
- OPTIONS 方法在跨域请求(CORS)中的应用
- 3GPP窄带物联网(NB-IoT)入门手册
- ajax
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结