栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现有向图的邻接表表示

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现有向图的邻接表表示

本文实例为大家分享了C++有向图的邻接表表示,供大家参考,具体内容如下

一、思路:

有向图的插入有向边、删除边、删除顶点和无向图的有区别。其他的和无向图的类似。

1.插入有向边

只需要插入边就行,不需要插入对称边

2.删除边

 只需要删除边就行,不需要仔找对称边进行删除。

3.删除顶点v:

首先,要在邻接表中删除以v为头的边;

同时,也要在邻接表中删除以v为尾的边, 不能通过对称边来找,只能一个个顶点找,浪费时间。

二、实现程序

1.DirectedGraph.h:有向图

#ifndef DirectedGraph_h
#define DirectedGraph_h
#include 
using namespace std;
 
const int DefaultVertices = 30;
 
template 
struct Edge { // 边结点的定义
 int dest; // 边的另一顶点位置
 E cost; // 表上的权值
 Edge *link; // 下一条边链指针
};
 
template 
struct Vertex { // 顶点的定义
 T data; // 顶点的名字
 Edge *adj; // 边链表的头指针
};
 
template 
class Graphlnk {
public:
 const E maxValue = 100000; // 代表无穷大的值(=∞)
 Graphlnk(int sz=DefaultVertices); // 构造函数
 ~Graphlnk(); // 析构函数
 void inputGraph(); // 建立邻接表表示的图
 void outputGraph(); // 输出图中的所有顶点和边信息
 T getValue(int i); // 取位置为i的顶点中的值
 E getWeight(int v1, int v2); // 返回边(v1, v2)上的权值
 bool insertVertex(const T& vertex); // 插入顶点
 bool insertEdge(int v1, int v2, E weight); // 插入边
 bool removeVertex(int v); // 删除顶点
 bool removeEdge(int v1, int v2); // 删除边
 int getFirstNeighbor(int v); // 取顶点v的第一个邻接顶点
 int getNextNeighbor(int v,int w); // 取顶点v的邻接顶点w的下一邻接顶点
 int getVertexPos(const T vertex); // 给出顶点vertex在图中的位置
 int numberOfVertices(); // 当前顶点数
private:
 int maxVertices; // 图中最大的顶点数
 int numEdges; // 当前边数
 int numVertices; // 当前顶点数
 Vertex * nodeTable; // 顶点表(各边链表的头结点)
};
 
// 构造函数:建立一个空的邻接表
template 
Graphlnk::Graphlnk(int sz) {
 maxVertices = sz;
 numVertices = 0;
 numEdges = 0;
 nodeTable = new Vertex[maxVertices]; // 创建顶点表数组
 if(nodeTable == NULL) {
  cerr << "存储空间分配错误!" << endl;
  exit(1);
 }
 for(int i = 0; i < maxVertices; i++)
  nodeTable[i].adj = NULL;
}
 
// 析构函数
template 
Graphlnk::~Graphlnk() {
 // 删除各边链表中的结点
 for(int i = 0; i < numVertices; i++) {
  Edge *p = nodeTable[i].adj; // 找到其对应链表的首结点
  while(p != NULL) { // 不断地删除第一个结点
   nodeTable[i].adj = p->link;
   delete p;
   p = nodeTable[i].adj;
  }
 }
 delete []nodeTable; // 删除顶点表数组
}
 
// 建立邻接表表示的图
template 
void Graphlnk::inputGraph() {
 int n, m; // 存储顶点树和边数
 int i, j, k;
 T e1, e2; // 顶点
 E weight; // 边的权值
 
 cout << "请输入顶点数和边数:" << endl;
 cin >> n >> m;
 cout << "请输入各顶点:" << endl;
 for(i = 0; i < n; i++) {
  cin >> e1;
  insertVertex(e1); // 插入顶点
 }
 
 cout << "请输入图的各边的信息:" << endl;
 i = 0;
 while(i < m) {
  cin >> e1 >> e2 >> weight;
  j = getVertexPos(e1);
  k = getVertexPos(e2);
  if(j == -1 || k == -1)
   cout << "边两端点信息有误,请重新输入!" << endl;
  else {
   insertEdge(j, k, weight); // 插入边
   i++;
  }
 } // while
}
 
// 输出有向图中的所有顶点和边信息
template 
void Graphlnk::outputGraph() {
 int n, m, i;
 T e1, e2; // 顶点
 E weight; // 权值
 Edge *p;
 
 n = numVertices;
 m = numEdges;
 cout << "图中的顶点数为" << n << ",边数为" << m << endl;
 for(i = 0; i < n; i++) {
  p = nodeTable[i].adj;
  while(p != NULL) {
   e1 = getValue(i); // 有向边dest>
   e2 = getValue(p->dest);
   weight = p->cost;
   cout << "<" << e1 << ", " << e2 << ", " << weight << ">" << endl;
   p = p->link; // 指向下一个邻接顶点
  }
 }
}
 
// 取位置为i的顶点中的值
template 
T Graphlnk::getValue(int i) {
 if(i >= 0 && i < numVertices)
  return nodeTable[i].data;
 return NULL;
}
 
// 返回边(v1, v2)上的权值
template 
E Graphlnk::getWeight(int v1, int v2) {
 if(v1 != -1 && v2 != -1) {
  Edge *p = nodeTable[v1].adj; // v1的第一条关联的边
  while(p != NULL && p->dest != v2) { // 寻找邻接顶点v2
   p = p->link;
  }
  if(p != NULL)
   return p->cost;
 }
 return maxValue; // 边(v1, v2)不存在,就存放无穷大的值
}
 
// 插入顶点
template 
bool Graphlnk::insertVertex(const T& vertex) {
 if(numVertices == maxVertices) // 顶点表满,不能插入
  return false;
 nodeTable[numVertices].data = vertex; // 插入在表的最后
 numVertices++;
 return true;
}
 
// 插入边
template 
bool Graphlnk::insertEdge(int v1, int v2, E weight) {
 if(v1 >= 0 && v1 < numVertices && v2 >= 0 && v2 < numVertices) {
  Edge *p = nodeTable[v1].adj; // v1对应的边链表头指针
  while(p != NULL && p->dest != v2) // 寻找邻接顶点v2
   p = p->link;
  if(p != NULL) // 已存在该边,不插入
   return false;
  p = new Edge; // 创建新结点
  p->dest = v2;
  p->cost = weight;
  p->link = nodeTable[v1].adj; // 链入v1边链表
  nodeTable[v1].adj = p;
  numEdges++;
  return true;
 }
 return false;
}
 
// 有向图删除顶点较麻烦
template 
bool Graphlnk::removeVertex(int v) {
 if(numVertices == 1 || v < 0 || v > numVertices)
  return false; // 表空或顶点号超出范围
 
 Edge *p, *s;
 // 1.清除顶点v的边链表结点w 边
 while(nodeTable[v].adj != NULL) {
  p = nodeTable[v].adj;
  nodeTable[v].adj = p->link;
  delete p;
  numEdges--; // 与顶点v相关联的边数减1
 } // while结束
 // 2.清除,与v有关的边
 for(int i = 0; i < numVertices; i++) {
  if(i != v) { // 不是当前顶点v
   s = NULL;
   p = nodeTable[i].adj;
   while(p != NULL && p->dest != v) {// 在顶点i的链表中找v的顶点
    s = p;
    p = p->link; // 往后找
   }
   if(p != NULL) { // 找到了v的结点
    if(s == NULL) { // 说明p是nodeTable[i].adj
     nodeTable[i].adj = p->link;
    } else {
     s->link = p->link; // 保存p的下一个顶点信息
    }
    delete p; // 删除结点p
    numEdges--; // 与顶点v相关联的边数减1
   }
  }
 }
 numVertices--; // 图的顶点个数减1
 nodeTable[v].data = nodeTable[numVertices].data; // 填补,此时numVertices,比原来numVertices小1,所以,这里不需要numVertices-1
 nodeTable[v].adj = nodeTable[numVertices].adj;
 // 3.要将填补的顶点对应的位置改写
 for(int i = 0; i < numVertices; i++) {
  p = nodeTable[i].adj;
  while(p != NULL && p->dest != numVertices) // 在顶点i的链表中找numVertices的顶点
   p = p->link; // 往后找
  if(p != NULL) // 找到了numVertices的结点
   p->dest = v; // 将邻接顶点numVertices改成v
 }
 return true;
}
 
// 删除边
template 
bool Graphlnk::removeEdge(int v1, int v2) {
 if(v1 != -1 && v2 != -1) {
  Edge * p = nodeTable[v1].adj, *q = NULL;
  while(p != NULL && p->dest != v2) { // v1对应边链表中找被删除边
   q = p;
   p = p->link;
  }
  if(p != NULL) { // 找到被删除边结点
   if(q == NULL) // 删除的结点是边链表的首结点
    nodeTable[v1].adj = p->link;
   else
    q->link = p->link; // 不是,重新链接
   delete p;
   return true;
  }
 }
 return false; // 没有找到结点
}
 
// 取顶点v的第一个邻接顶点
template 
int Graphlnk::getFirstNeighbor(int v) {
 if(v != -1) {
  Edge *p = nodeTable[v].adj; // 对应链表第一个边结点
  if(p != NULL) // 存在,返回第一个邻接顶点
   return p->dest;
 }
 return -1; // 第一个邻接顶点不存在
}
 
// 取顶点v的邻接顶点w的下一邻接顶点
template 
int Graphlnk::getNextNeighbor(int v,int w) {
 if(v != -1) {
  Edge *p = nodeTable[v].adj; // 对应链表第一个边结点
  while(p != NULL && p->dest != w) // 寻找邻接顶点w
   p = p->link;
  if(p != NULL && p->link != NULL)
   return p->link->dest; // 返回下一个邻接顶点
 }
 return -1; // 下一个邻接顶点不存在
}
 
// 给出顶点vertex在图中的位置
template 
int Graphlnk::getVertexPos(const T vertex) {
 for(int i = 0; i < numVertices; i++)
  if(nodeTable[i].data == vertex)
   return i;
 return -1;
}
 
// 当前顶点数
template 
int Graphlnk::numberOfVertices() {
 return numVertices;
}
 
#endif 

2.main.cpp


 
#include "DirectedGraph.h"
 
int main(int argc, const char * argv[]) {
 Graphlnk st; // 声明对象
 bool finished = false;
 int choice;
 char e1, e2, next;
 int weight;
 
 while(!finished) {
  cout << "[1]创建基于邻接表的有向图" << endl;
  cout << "[2]输出图的所有顶点和边信息" << endl;
  cout << "[3]取顶点v的第一个邻接顶点" << endl;
  cout << "[4]取v的邻接顶点w的下一个邻接顶点" << endl;
  cout << "[5]插入顶点" << endl;
  cout << "[6]插入边" << endl;
  cout << "[7]删除顶点" << endl;
  cout << "[8]删除边" << endl;
  cout << "[9]退出" << endl;
  cout << "请输入选择[1-9]:";
  cin >> choice;
  switch(choice) {
   case 1:
    st.inputGraph();
    break;
   case 2:
    st.outputGraph();
    break;
   case 3:
    cout << "请输入顶点:";
    cin >> e1;
    e2 = st.getValue(st.getFirstNeighbor(st.getVertexPos(e1)));
    if(e2)
     cout << "顶点" << e1 << "的第一个邻接顶点为:" << e2 << endl;
    else
     cout << "顶点" << e1 << "没有邻接顶点!" << endl;
    break;
   case 4:
    cout << "请输入顶点v和邻接顶点w:";
    cin >> e1 >> e2;
    next = st.getValue(st.getNextNeighbor(st.getVertexPos(e1), st.getVertexPos(e2)));
    if(next)
     cout << "顶点" << e1 << "的邻接顶点" << e2 << "的下一个邻接顶点为:" << next << endl;
    else
     cout << "顶点" << e1 << "的邻接顶点" << e2 << "没有下一个邻接顶点!" << endl;
    break;
   case 5:
    cout << "请输入要插入的顶点:";
    cin >> e1;
    if(st.insertVertex(e1))
     cout << "插入成功!" << endl;
    else
     cout << "表已满,插入失败!" << endl;
    break;
   case 6:
    cout << "请输入要插入的边的信息:" << endl;
    cin >> e1 >> e2 >> weight;
    st.insertEdge(st.getVertexPos(e1), st.getVertexPos(e2), weight);
    break;
   case 7:
    cout << "请输入要删除的顶点:";
    cin >> e1;
    if(st.removeVertex(st.getVertexPos(e1)))
     cout << "顶点" << e1 << "已删除!" << endl;
    else
     cout << "顶点" << e1 << "不在图中!" << endl;
    break;
   case 8:
    cout << "请输入要删除的边的两个端点:" << endl;
    cin >> e1 >> e2;
    st.removeEdge(st.getVertexPos(e1), st.getVertexPos(e2));
    break;
   case 9:
    finished = true;
    break;
   default:
    cout << "选择输入错误,请重新输入!" << endl;
  }
 }
 return 0;
}

测试结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号