# 数据结构：图的实现--邻接表

50 篇文章 0 订阅
49 篇文章 63 订阅

图的实现：邻接表

#include<iostream>
#include<iomanip>
using namespace std;
//最大权值
#define MAXWEIGHT 100
//边节点
typedef struct edgenode_tag
{
int weight;  //边的权值
struct edgenode_tag *next;
}EdgeNode;
//顶点节点
typedef struct vertex_tag
{
int vertex;   //顶点
EdgeNode *next;
}Vertex;
class Graph
{
private:
//是否带权
bool isWeighted;
//是否有向
bool isDirected;
//顶点数
int numV;
//边数
int numE;
//邻接表
public:
/*
构造方法
numV是顶点数，isWeighted是否带权值，isDirected是否有方向
*/
Graph(int numV, bool isWeighted = false, bool isDirected = false);
//建图
void createGraph();
//析构方法
~Graph();
//获取顶点数
int getVerNums()
{return numV;}
//获取边数
int getEdgeNums()
{return numE;}
//插入边
void insertEdge(int tail, int head, int weight = 1);
void insertedge(int tail, int head, int weight);
//设置指定边的权值
void setEdgeWeight(int tail, int head, int weight);
//打印邻接表
//检查输入
bool check(int tail, int head, int weight = 1);
};

/*

numV是顶点数，isWeighted是否带权值，isDirected是否有方向
*/
Graph::Graph(int numV, bool isWeighted, bool isDirected)
{
while (numV <= 0)
{
cout << "输入的顶点数不正确！，重新输入 ";
cin >> numV;
}
this->numV = numV;
this->isWeighted = isWeighted;
this->isDirected = isDirected;
//边数初始化为0
numE = 0;
for (int i = 0; i < numV; i++)
{
}
}
//建图
void Graph::createGraph()
{
//用一个新的变量表示边数，numE的修改则留到insertedge()中
int numEdge = 0;
cout << "输入边数 ";
while (cin >> numEdge && numEdge < 0)
cout << "输入有误！，重新输入 ";

int i, j, w;
if (!isWeighted)  //无权图
{
cout << "输入每条边的起点和终点：\n";
for (int k = 0; k < numEdge; k++)
{
cin >> i >> j;
while (!check(i, j))
{
cout << "输入的边不对！重新输入\n";
cin >> i >> j;
}
insertEdge(i, j);
}
}
else  //有权图
{
cout << "输入每条边的起点、终点和权值：\n";
for (int k = 0; k < numEdge; k++)
{
cin >> i >> j >> w;
while (!check(i, j, w))
{
cout << "输入的边不对！重新输入\n";
cin >> i >> j >> w;
}
insertEdge(i, j, w);
}
}
}
//析构方法
Graph::~Graph()
{
int i;
EdgeNode *p, *q;
for (i = 0; i < numV; i++)
{
{
while (p)
{
q = p->next;
delete p;
p = q;
}
}
}
}
//设置指定边的权值
void Graph::setEdgeWeight(int tail, int head, int weight)
{
if (!isWeighted)  //无权图
{
{
cout << "输入的边不对！重新输入\n";
}
}
else  //有权图
{
{
cout << "输入的边不对！重新输入\n";
cin >> tail >> head >> weight;
}
}
}
//插入边
void Graph::insertEdge(int vertex, int adjvex, int weight)
{
if (!isDirected)  //无向图
}
void Graph::insertedge(int vertex, int adjvex, int weight)
{
EdgeNode *p, *q, *r;
p = q = r = NULL;
{
//移动p到合适位置
{
q = p;
p = p->next;
}
p->weight = weight;
else
{
r = new EdgeNode;
r->weight = weight;
r->next = p;
//当加入的新节点位于表的第一个位置
else
q->next = r;
numE++;
}
}
else
{
p = new EdgeNode;
p->weight = weight;
p->next = NULL;
numE++;
}
}
//输入检查
bool Graph::check(int tail, int head, int weight)
{
if (tail >= 0 && tail < numV && head >= 0 && head < numV
&& weight > 0 && weight <= MAXWEIGHT)
return true;
else
return false;
}
//打印邻接表
{
int i;
EdgeNode *edge = NULL;
for (i = 0; i < numV; i++)
{
if (edge) //为什么加一个if判断？跟后面的换行有关。若某顶点无邻接点(无边)，则会空出一行
{
while (edge)
{
cout << "w(" << i << "," << edge->adjvex << ")=" << edge->weight << "  ";
edge = edge->next;
}
cout << endl;
}
}
}

int main(void)
{
cout << "******使用邻接表实现图结构***by David***" << endl;
bool isDirected, isWeighted;
int numV;
cout << "建图" << endl;
cout << "输入顶点数 ";
cin >> numV;
cout << "边是否带权值，0(不带) or 1(带) ";
cin >> isWeighted;
cout << "是否是有向图，0(无向) or 1(有向) ";
cin >> isDirected;
Graph graph(numV, isWeighted, isDirected);
cout << "这是一个";
isDirected ? cout << "有向、" : cout << "无向、";
isWeighted ? cout << "有权图" << endl : cout << "无权图" << endl;
graph.createGraph();
cout << "打印邻接表" << endl;
cout << endl;
cout << "修改指定边的权值" << endl;
if (isWeighted)  //针对有权图
{
cout << "输入边的起点、终点和权值 ";
cin >> tail >> head >> weight;
}
else  //针对无权图
{
cout << "输入边的起点、终点 ";
}
cout << "修改成功！" << endl;
cout << "打印邻接矩阵" << endl;
system("pause");
return 0;
}

• 7
点赞
• 4
评论
• 11
收藏
• 一键三连
• 扫一扫，分享海报

02-07 3万+
11-28 1万+

08-27 2万+
04-30 11万+
10-06 7万+
04-08 2689
01-09 300
11-15 573
04-16 816
08-19 6108
08-06 5709