C++继承实现邻接矩阵和邻接表
1、图的父类
成都创新互联公司专注于绥江网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供绥江营销型网站建设,绥江网站制作、绥江网页设计、绥江网站官网定制、小程序设计服务,打造绥江网络公司原创品牌,更为您提供绥江网站排名全网营销落地服务。
是一个抽象类,不能实类化对象,应具有的是抽象方法,提供一个接口,在由子类继承,实现自己的方法,
应提供的共有抽象方法和保护的数据:
public: virtual bool insertVertex(const Type &v) = 0; //插入顶点 virtual bool insertEdge(const Type &v1, const Type &v2) = 0; //插入边 virtual bool removeVertex(const Type &v) = 0; //删除顶点 virtual bool removeEdge(const Type &v1, const Type &v2) = 0; //删除边 virtual int getFirstNeighbor(const Type &v) = 0; //得到第一个相邻顶点 virtual int getNextNeighbor(const Type &v, const Type &w) = 0; //得到下一个相邻顶点 public: virtual int getVertexIndex(const Type &v)const = 0; //得到顶点下标 virtual void showGraph()const = 0; //显示图 protected: int maxVertices; //最大顶点数 int curVertices; //当前顶点数 int curEdges; //当前边数
2、子类继承、实现自己的方法
C++实现,继承的体现,是为了实现多态
(1)Graph.h
#ifndef _GRAPH_H_ #define _GRAPH_H_ #includeusing namespace std; #define VERTEX_DEFAULT_SIZE 10 template class Graph{ public: bool isEmpty()const{ return curVertices == 0; } bool isFull()const{ if(curVertices >= maxVertices || curEdges >= curVertices*(curVertices-1)/2) return true; //图满有2种情况:(1)、当前顶点数超过了最大顶点数,存放顶点的空间已满 return false; //(2)、当前顶点数并没有满,但是当前顶点所能达到的边数已满 } int getCurVertex()const{ return curVertices; } int getCurEdge()const{ return curEdges; } public: virtual bool insertVertex(const Type &v) = 0; //插入顶点 virtual bool insertEdge(const Type &v1, const Type &v2) = 0; //插入边 virtual bool removeVertex(const Type &v) = 0; //删除顶点 virtual bool removeEdge(const Type &v1, const Type &v2) = 0; //删除边 virtual int getFirstNeighbor(const Type &v) = 0; //得到第一个相邻顶点 virtual int getNextNeighbor(const Type &v, const Type &w) = 0; //得到下一个相邻顶点 public: virtual int getVertexIndex(const Type &v)const = 0; //得到顶点下标 virtual void showGraph()const = 0; //显示图 protected: int maxVertices; //最大顶点数 int curVertices; //当前顶点数 int curEdges; //当前边数 }; ///////////////////////////////////////////////////下面先是邻接矩阵 template class GraphMtx : public Graph { //邻接矩阵继承父类矩阵 #define maxVertices Graph ::maxVertices //因为是模板,所以用父类的数据或方法都得加上作用域限定符 #define curVertices Graph ::curVertices #define curEdges Graph ::curEdges public: GraphMtx(int vertexSize = VERTEX_DEFAULT_SIZE){ //初始化邻接矩阵 maxVertices = vertexSize > VERTEX_DEFAULT_SIZE ? vertexSize : VERTEX_DEFAULT_SIZE; vertexList = new Type[maxVertices]; //申请顶点空间 for(int i = 0; i < maxVertices; i++){ //都初始化为0 vertexList[i] = 0; } edge = new int*[maxVertices]; //申请边的行 for(i = 0; i < maxVertices; i++){ //申请列空间 edge[i] = new int[maxVertices]; } for(i = 0; i < maxVertices; i++){ //赋初值为0 for(int j = 0; j < maxVertices; j++){ edge[i][j] = 0; } } curVertices = curEdges = 0; //当前顶点和当前边数 } GraphMtx(Type (*mt)[4], int sz){ //通过已有矩阵的初始化 int e = 0; //统计边数 maxVertices = sz > VERTEX_DEFAULT_SIZE ? sz : VERTEX_DEFAULT_SIZE; vertexList = new Type[maxVertices]; //申请顶点空间 for(int i = 0; i < maxVertices; i++){ //都初始化为0 vertexList[i] = 0; } edge = new int*[maxVertices]; //申请边的行 for(i = 0; i < maxVertices; i++){ //申请列空间 edge[i] = new Type[maxVertices]; } for(i = 0; i < maxVertices; i++){ //赋初值为矩阵当中的值 for(int j = 0; j < maxVertices; j++){ edge[i][j] = mt[i][j]; if(edge[i][j] != 0){ e++; //统计列的边数 } } } curVertices = sz; curEdges = e/2; } ~GraphMtx(){} public: bool insertVertex(const Type &v){ if(curVertices >= maxVertices){ return false; } vertexList[curVertices++] = v; return true; } bool insertEdge(const Type &v1, const Type &v2){ int maxEdges = curVertices*(curVertices-1)/2; if(curEdges >= maxEdges){ return false; } int v = getVertexIndex(v1); int w = getVertexIndex(v2); if(v==-1 || w==-1){ cout<<"edge no exit"< class Edge{ //边的存储结构 public: Edge(int num) : dest(num), link(NULL){} public: int dest; Edge *link; }; template class Vertex{ //顶点的存储结构 public: Type data; Edge *adj; }; template class GraphLnk : public Graph { #define maxVertices Graph ::maxVertices //因为是模板,所以用父类的数据或方法都得加上作用域限定符 #define curVertices Graph ::curVertices #define curEdges Graph ::curEdges public: GraphLnk(int sz = VERTEX_DEFAULT_SIZE){ maxVertices = sz > VERTEX_DEFAULT_SIZE ? sz : VERTEX_DEFAULT_SIZE; vertexTable = new Vertex [maxVertices]; for(int i = 0; i < maxVertices; i++){ vertexTable[i].data = 0; vertexTable[i].adj = NULL; } curVertices = curEdges = 0; } public: bool insertVertex(const Type &v){ if(curVertices >= maxVertices){ return false; } vertexTable[curVertices++].data = v; return true; } bool insertEdge(const Type &v1, const Type &v2){ int v = getVertexIndex(v1); int w = getVertexIndex(v2); if(v==-1 || w==-1){ return false; } Edge *p = vertexTable[v].adj; while(p != NULL){ //这里主要判断边是否已经存在 if(p->dest == w){ //无向图,判断一边即可; return false; } p = p->link; } //v1-->v2 //采用头插 Edge *s = new Edge (w); s->link = vertexTable[v].adj; vertexTable[v].adj = s; //v2-->v1 //采用头插 Edge *q = new Edge (v); q->link = vertexTable[w].adj; vertexTable[w].adj = q; curEdges++; return true; } bool removeVertex(const Type &v){ int i = getVertexIndex(v); if(i == -1){ return false; } Edge *p = vertexTable[i].adj; while(p != NULL){ vertexTable[i].adj = p->link; int k = p->dest; Edge *q = vertexTable[k].adj; if(q->dest == i){ vertexTable[k].adj = q->link; delete q; }else{ while(q->link != NULL && q->link->dest != i){ q = q->link; } Edge *t = q->link; q->link = t->link; delete t; } delete p; p = vertexTable[i].adj; curEdges--; } curVertices--; //下面实行覆盖 vertexTable[i].data = vertexTable[curVertices].data; vertexTable[i].adj = vertexTable[curVertices].adj; vertexTable[curVertices].adj = NULL; int k = curVertices; p = vertexTable[i].adj; while(p != NULL){ Edge *s = vertexTable[p->dest].adj; while(s != NULL){ if(s->dest == k){ s->dest = i; break; } s = s->link; } p = p->link; } return true; } bool removeEdge(const Type &v1, const Type &v2){ int i = getVertexIndex(v1); int j = getVertexIndex(v2); if(i==-1 || j==-1){ //保证顶点的保存在 return false; } //v1-->v2 Edge *p = vertexTable[i].adj; if(p == NULL){ //判断有没有边 return false; } if(p->link == NULL && p->dest == j){ //删除的是第一个边,其后没有边了; vertexTable[i].adj = NULL; delete p; }else if(p->dest == j){ //删除的是第一个边,并且其后还有边 vertexTable[i].adj = p->link; delete p; }else{ while(p->link != NULL){ if(p->link->dest == j){ Edge *q = p->link; p->link = q->link; delete q; } p = p->link; } } //v2-->v1 Edge *s = vertexTable[j].adj; if(s == NULL){ //判断有没有边 return false; } if(s->link == NULL && s->dest == i){ //删除的是第一个边,其后没有边了; vertexTable[j].adj = NULL; delete s; curEdges--; return false; }else if(s->dest == i){ //删除的是第一个边,并且其后还有边 vertexTable[j].adj = s->link; delete s; curEdges--; return true; }else{ while(s->link != NULL){ if(s->link->dest == i){ Edge *q = s->link; s->link = q->link; delete q; curEdges--; return true; } s = s->link; } } return true; } int getFirstNeighbor(const Type &v){ int i = getVertexIndex(v); if(i != -1){ Edge *p = vertexTable[i].adj; if(p != NULL){ return p->dest; } } return -1; } int getNextNeighbor(const Type &v, const Type &w){ int i = getVertexIndex(v); int j = getVertexIndex(w); if(i==-1 || j==-1){ return -1; } Edge *p = vertexTable[i].adj; while(p != NULL){ if(p->dest == j && p->link != NULL){ return p->link->dest; } p = p->link; } return -1; } public: int getVertexIndex(const Type &v)const{ for(int i = 0; i < curVertices; i++){ if(vertexTable[i].data == v){ return i; } } return -1; } void showGraph()const{ for(int i = 0; i < curVertices; i++){ cout< "; Edge *p = vertexTable[i].adj; while(p != NULL){ cout< dest<<"-->"; p = p->link; } cout<<"Nul. "< *vertexTable; //指向顶点的指针,是申请数组用的 }; #endif
(2)、Graph.cpp
#include"Graph.h" int main(void){ GraphMtxgm; //邻接矩阵 gm.insertVertex('A'); //插入顶点 gm.insertVertex('B'); gm.insertVertex('C'); gm.insertVertex('D'); gm.insertEdge('A','B'); //插入边 gm.insertEdge('A','D'); gm.insertEdge('B','C'); gm.insertEdge('C','D'); cout< gl; //邻接表 gl.insertVertex('A'); gl.insertVertex('B'); gl.insertVertex('C'); gl.insertVertex('D'); gl.insertEdge('A','B'); gl.insertEdge('A','D'); gl.insertEdge('B','C'); gl.insertEdge('C','D'); gl.showGraph(); cout< 3、邻接多重表
是一个十字交叉链的形式;在很大程度上节省了空间,还是要对链表的操作很熟悉;
分享名称:C++继承实现邻接矩阵和邻接表
网页路径:http://pwwzsj.com/article/jojegg.html