﻿ 数据结构学习(C++)之图_天才写手 -代写CS作业_CS代写_Finance代写_Economic代写_Statistics代写_代写代码_作业代写_加急帮助
##### 当前位置 |首页 > 代写教程 > C语言/C++ 教程 >

```#ifndef Graphmem_H #define Graphmem_H #include <vector> #include <list> using namespace std; template <class name, class dist, class mem> class Network; const int maxV = 20;//最大节点数 template <class name, class dist> class AdjMatrix { friend class Network<name, dist, AdjMatrix<name, dist> >; public: AdjMatrix() : vNum(0), eNum(0) { vertex = new name[maxV]; edge = new dist*[maxV]; for (int i = 0; i < maxV; i++) edge[i] = new dist[maxV]; } ~AdjMatrix() { for (int i = 0; i < maxV; i++) delete []edge[i]; delete []edge; delete []vertex; } bool insertV(name v) { if (find(v)) return false; vertex[vNum] = v; for (int i = 0; i < maxV; i++) edge[vNum][i] = NoEdge; vNum++; return true; } bool insertE(name v1, name v2, dist cost) { int i, j; if (v1 == v2 || !find(v1, i) || !find(v2, j)) return false; if (edge[i][j] != NoEdge) return false; edge[i][j] = cost; eNum++; return true; } name& getV(int n) { return vertex[n]; } //没有越界查抄 int nextV(int m, int n)//返回m号极点的第n号极点后第一个连接极点号，无返回-1 { for (int i = n + 1; i < vNum; i++) if (edge[m][i] != NoEdge) return i; return -1; } private: int vNum, eNum; dist NoEdge, **edge; name *vertex; bool find(const name& v) { for (int i = 0; i < vNum; i++) if (v == vertex[i]) return true; return false; } bool find(const name& v, int& i) { for (i = 0; i < vNum; i++) if (v == vertex[i]) return true; return false; } }; template <class name, class dist> class LinkedList { friend class Network<name, dist, LinkedList<name, dist> >; public: LinkedList() : vNum(0), eNum(0) {} ~LinkedList() { for (int i = 0; i < vNum; i++) delete vertices[i].e; } bool insertV(name v) { if (find(v)) return false; vertices.push_back(vertex(v, new list<edge>)); vNum++; return true; } bool insertE(const name& v1, const name& v2, const dist& cost) { int i, j; if (v1 == v2 || !find(v1, i) || !find(v2, j)) return false; for (list<edge>::iterator iter = vertices[i].e->begin(); iter != vertices[i].e->end() && iter->vID < j; iter++); if (iter == vertices[i].e->end()) { vertices[i].e->push_back(edge(j, cost)); eNum++; return true; } if (iter->vID == j) return false; vertices[i].e->insert(iter, edge(j, cost)); eNum++; return true; } name& getV(int n) { return vertices[n].v; } //没有越界查抄 int nextV(int m, int n)//返回m号极点的第n号极点后第一个连接极点号，无返回-1 { for (list<edge>::iterator iter = vertices[m].e->begin(); iter != vertices[m].e->end(); iter++) if (iter->vID > n) return iter->vID; return -1; } private: bool find(const name& v) { for (int i = 0; i < vNum; i++) if (v == vertices[i].v) return true; return false; } bool find(const name& v, int& i) { for (i = 0; i < vNum; i++) if (v == vertices[i].v) return true; return false; } struct edge { edge() {} edge(int vID, dist cost) : vID(vID), cost(cost) {} int vID; dist cost; }; struct vertex { vertex() {} vertex(name v, list<edge>* e) : v(v), e(e) {} name v; list<edge>* e; }; int vNum, eNum; vector<vertex> vertices; }; #endif```