C++邻接表实现无向图、有向图
来源:互联网 发布:手机比特币挖矿软件 编辑:程序博客网 时间:2024/05/16 07:58
/*********************邻接表实现有向图******************///邻接表实现有向图缺点:只有出度,没有入度;解决方法:逆邻接表、十字链表//边节点类template<class Type>class EdgeNode{public: int adjvex;//邻节点域,存储该节点对应的下标 //int weight;//用于存储权值,非网图不需要 EdgeNode *next;//链域,指向下一个邻节点 //EdgeNode(){} EdgeNode(int adj, EdgeNode *n = nullptr) :adjvex(adj), next(n){}};//顶点表节点类template<class Type>class VertexNode{public: Type data;//顶点表域,存储顶点信息 EdgeNode<Type> *firstedge;//边表头指针 VertexNode(){} VertexNode(Type d, EdgeNode<Type> *fir = nullptr) :data(d), firstedge(fir){}};//无向图类template<class Type>class UndirectedGraph{private: VertexNode<Type> AdjList[MAX];//顶点数组 int numVertexes, numEdges;//图中当前顶点数和边数 Type readType();//外界读取一个Type型数据 int getPosition(Type el);//获取el在邻接表表头节点的位置public: UndirectedGraph();//创建图(自己输入) ~UndirectedGraph();//析构函数 void print();//打印邻接表};//获取el在邻接表表头节点的位置template<class Type>int UndirectedGraph<Type>::getPosition(Type el){ for (int i = 0; i < this->numVertexes; i++) { if (this->AdjList[i].data == el) return i; } return -1;}//外界读取一个Type型数据template<class Type>Type UndirectedGraph<Type>::readType(){ cout << "input a data: "; Type el; cin >> el; return el;}//创建图template<class Type>UndirectedGraph<Type>::UndirectedGraph(){ Type c1, c2; int p1, p2; EdgeNode<Type> *enode1, *enode2; cout << "input vertex number: "; cin >> this->numVertexes; cout << "input edge number: "; cin >> this->numEdges; //判断输入节点数与边数是否合法 if (this->numVertexes < 1 || this->numEdges<1 || this->numEdges>this->numVertexes*(this->numVertexes - 1)) { cout << "input error: invalid input!" << endl; return; } //初始化顶点表 for (int i = 0; i < this->numVertexes; i++) { cout << "vertex(" << i << "): "; cin >> this->AdjList[i].data; this->AdjList[i].firstedge = nullptr; } //初始化边表 for (int i = 0; i < this->numEdges; i++) { cout << "edge " << i << endl; c1 = readType(); c2 = readType(); p1 = getPosition(c1); p2 = getPosition(c2); if (p1 == -1 || p2 == -1) { cout << "input error!" << endl; return; } enode1 = new EdgeNode<Type>(p2, nullptr); if (this->AdjList[p1].firstedge == nullptr) this->AdjList[p1].firstedge = enode1; else { EdgeNode<Type> *move = this->AdjList[p1].firstedge;//这里要注意 while (move->next != nullptr) move = move->next; move->next = enode1; } enode2 = new EdgeNode<Type>(p1, nullptr); if (this->AdjList[p2].firstedge == nullptr) this->AdjList[p2].firstedge = enode2; else { EdgeNode<Type> *move = this->AdjList[p2].firstedge;//这里要注意 while (move->next != nullptr) move = move->next; move->next = enode2; } }}//析构函数template<class Type>UndirectedGraph<Type>::~UndirectedGraph(){ cout << "析构函数" << endl;}//打印邻接表template<class Type>void UndirectedGraph<Type>::print(){ EdgeNode<Type> *p; for (int i = 0; i < this->numVertexes; i++) { cout << "vertexs[" << i << "].data= " << this->AdjList[i].data << " : "; p = this->AdjList[i].firstedge; while (p != nullptr) { cout << p->adjvex << " "; p = p->next; } cout << endl; }}//测试程序int main(){ UndirectedGraph<char> graph; graph.print(); return 0;}/*********************邻接表实现有向图******************///邻接表实现有向图缺点:只有出度,没有入度;解决方法:逆邻接表、十字链表//边节点类template<class Type>class EdgeNode{public: int adjvex;//边节点域,存储该节点对应的下标 //int weight;//用于存储权值,对应非网图可以不需要 EdgeNode *next;//链域,指向下一邻节点 EdgeNode(int adj, EdgeNode *n = nullptr) :adjvex(adj), next(n){}//构造函数};//顶点类template<class Type>class VertexNode{public: Type data;//顶点域,存储顶点信息 EdgeNode<Type> *firstedge;//边表头指针 VertexNode(){} VertexNode(Type d, EdgeNode<Type> *fir = nullptr) :data(d), firstedge(fir){}//构造函数};//有向图类template<class Type>class DirectedGraph{private: VertexNode<Type> AdjList[MAX];//顶点表 int numVertexes;//当前图顶点 int numEdges;//当前图边数 int getPosition(Type el);//返回el在顶点表中的位置public: DirectedGraph();//创建图 ~DirectedGraph();//构造函数 void print();//打印邻接表};//返回el在顶点表中的位置template<class Type>int DirectedGraph<Type>::getPosition(Type el){ for (int i = 0; i < this->numVertexes; i++) { if (this->AdjList[i].data == el) return i; } return -1;}//创建表template<class Type>DirectedGraph<Type>::DirectedGraph(){ Type c1, c2; int p1, p2; EdgeNode<Type> *enode; cout << "input vertex number: "; cin >> this->numVertexes; cout << "input edge number: "; cin >> this->numEdges; if (this->numVertexes < 1 || this->numEdges<1 || this->numEdges>this->numVertexes*(this->numVertexes - 1)) { cout << "input error!" << endl; return; } //初始化顶点 for (int i = 0; i < this->numVertexes; i++) { cout << "input a data: "; cin >> this->AdjList[i].data; this->AdjList[i].firstedge = nullptr; } //初始化弧 for (int i = 0; i < this->numEdges; i++) { cout << "input arcHead: ";//弧头 cin >> c1; cout << "input arcTail:";//弧尾 cin >> c2; p1 = getPosition(c1); p2 = getPosition(c2); if (p1 == -1 || p2 == -1) { cout << "input invalid!" << endl; return; } enode = new EdgeNode<Type>(p2, nullptr); if (this->AdjList[p1].firstedge == nullptr) this->AdjList[p1].firstedge = enode; else { EdgeNode<Type> *move = this->AdjList[p1].firstedge; while (move->next != nullptr) move = move->next; move->next = enode; } }}//析构函数template<class Type>DirectedGraph<Type>::~DirectedGraph(){ cout << "析构函数" << endl;}//打印邻接表template<class Type>void DirectedGraph<Type>::print(){ for (int i = 0; i < this->numVertexes; i++) { cout << "AdjList[" << i + 1 << "].data = " << this->AdjList[i].data << ": "; EdgeNode<Type> *move = this->AdjList[i].firstedge; while (move != nullptr) { cout << move->adjvex << " "; move = move->next; } cout << endl; }}//测试程序int main(){ DirectedGraph<char> graph; graph.print(); return 0;}
1 0
- C++邻接表实现无向图、有向图
- 邻接表-建立无向图、无向网、有向图、有向网
- c语言实现无向图的邻接表储存
- 用邻接表实现无向图
- Java实现无向图邻接表
- 有向图的邻接表实现
- 邻接表无向图
- 有向图-邻接表
- 邻接表无向图(一)之 C语言详解
- 邻接表无向图之 C语言详解
- 求割点(邻接表无向图)C~
- c语言无向图邻接表深度有限搜索
- 邻接链表实现的无向图
- 基于邻接表的图建立(有向图+无向图)
- 有向图与无向图的邻接矩阵,邻接表 计算出度与入度
- 用邻接链表存储无向图和有向图
- 利用邻接表存储结构创建一个图(有向、无向)
- 邻接表 有向图 是否有环 C实现 (dfs
- TX1刷机以及配置caffe全备忘(Jetpack2.3+)
- c++作业5——数组1
- SAP BO的路还有多长
- postgresql定位分析消耗CPU高的SQL语句
- 选择排序
- C++邻接表实现无向图、有向图
- 堆排序详讲
- scroll滚动事件在浏览器中无效
- 500强面试题
- 九度1019:简单计算器(Stack)
- 日常总结
- win10系统配置jdk
- java 求数组中重复元素最多的数 空间换时间解法 Map映射表法
- 进程(1)进程的内存映像