29 邻接表:添加一个顶点
作者: 冯向阳时间限制: 1S章节: DS:图
截止日期: 2022-06-30 23:55:00
问题描述 :
目的:使用C++模板设计并逐步完善图的邻接表抽象数据类型(ADT)。
内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接表ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)
(2)设计并实现一个算法,向已存在的图中添加一个顶点。图的存储结构采用邻接表。添加的顶点的位序在顶点集的末端。将其加入到ADT中。
注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
参考函数原型:
//往G中添加一个顶点
template<class TypeOfVer, class TypeOfEdge>
bool adjlist_graph<TypeOfVer, TypeOfEdge>::InsertVer(const TypeOfVer &data);
图的邻接表模板类原型参考如下:
/* 边表的结点定义 */
template<class TypeOfEdge>
struct edgeNode
{
int data;
TypeOfEdge weight;
edgeNode<TypeOfEdge> *next;
edgeNode(const int &d, edgeNode<TypeOfEdge> *ptr = NULL) //构造函数,用于构造其他结点(无权图)
//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
{
next = ptr;
data = d;
}
edgeNode(const int &d, const TypeOfEdge &w, edgeNode<TypeOfEdge> *ptr = NULL) //构造函数,用于构造其他结点(带权图)
//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
{
next = ptr;
data = d;
weight = w;
}
int getData(){ return data;} //取得结点的序号(顶点集)
TypeOfEdge getWeight(){ return weight;} //取得边集中对应边的权值
void SetLink( edgeNode<TypeOfEdge> *link ){ next = link; } //修改结点的next域
void SetData( int value ){ data = value; } //修改结点的序号(顶点集)
void SetWeight(TypeOfEdge value ){ weight = value; } //修改边集中对应边的权值
};
//图的邻接表类
template<class TypeOfVer, class TypeOfEdge>
struct verNode
{
TypeOfVer ver;
edgeNode<TypeOfEdge> *head;
verNode(edgeNode<TypeOfEdge> *h = NULL){head = h;}
TypeOfVer getVer(){ return ver;} //取得结点值(顶点集)
edgeNode<TypeOfEdge> *getHead(){ return head;} //取得对应的边表的头指针
void setVer(TypeOfVer value){ ver = value;} //设置结点值(顶点集)
void setHead(edgeNode<TypeOfEdge> *value){ head = value;} //设置对应的边表的头指针
};
template <class TypeOfVer, class TypeOfEdge>
class adjlist_graph{
private:
int Vers; //顶点数
int Edges; //边数
verNode<TypeOfVer,TypeOfEdge> *verList;
string GraphKind; //图的种类标志
bool Delete_Edge( int u, int v );
bool DFS(int u, int &num, int visited[]); //DFS遍历(递归部分)
public:
adjlist_graph( const string &kd, int vSize, const TypeOfVer d[]); //构造函数构造一个只有结点没有边的图。
adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e); 构造函数构造一个无权图。5个参数的含义:图的类型、结点数、边数、结点集和边集
adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e, const TypeOfEdge w[]); //构造函数构造一个有权图。
bool GraphisEmpty() { return Vers == 0; } //判断图空否
string GetGraphKind(){ return GraphKind; }
bool GetVer(int u, TypeOfVer &data); //取得G中指定顶点的值
int GetFirstAdjVex(int u, int &v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1
int GetNextAdjVex(int u, int v, int &w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回false
bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值
bool InsertVer(const TypeOfVer &data); //往G中添加一个顶点
int LocateVer(TypeOfVer data); //返回G中指定顶点的位置
bool ExistEdge(int u, int v);
bool PrintVer(); //输出顶点集
bool PrintAdjList(); //输出邻接矩阵
int GetVerNum(){ return Vers;} //取得当前顶点数
int GetEdgeNum(){ return Edges;} //取得当前边数
bool Insert_Edge(int u, int v); //无权图插入一条边
bool Insert_Edge(int u, int v, TypeOfEdge w); //有权图插入一条边
bool DeleteVer(const TypeOfVer &data); //往G中删除一个顶点
bool DeleteEdge( int u, int v ); //删除边 (外壳:有向(删除1条边), 无向(删除2条边))
void DFS_Traverse(int u); //DFS遍历(外壳部分)
void BFS_Traverse(int u); //BFS遍历
~adjlist_graph(); //析构函数
};
输入说明 :
建图的输入数据格式参见建图的算法说明。
第一行:图的类型
第二行:结点数
第三行:结点集
第四行:边数
第五行:边集
第六行:待插入的顶点的元素值
输出说明 :
第一行:图的类型
第二行:插入前的顶点集
第三行:插入前的邻接表
空行
第四行:插入后的顶点集
第五行:插入后的邻接表
输入范例 :
DG
6
A B C D E F
6
0 1
0 2
0 3
1 4
2 4
3 5
G
--------
DG
A B C D E F
A->3->2->1->nullptr
B->4->nullptr
C->4->nullptr
D->5->nullptr
E->nullptr
F->nullptr
A B C D E F G
A->3->2->1->nullptr
B->4->nullptr
C->4->nullptr
D->5->nullptr
E->nullptr
F->nullptr
G->nullptr
------------------------
我只是从输出角度实现了添加节点,其实内部并没有真正添加结点 不过答案是对的 这样也更简单一点 我最近实在没时间了 各位将就一下把
#include<iostream>
#include<queue>
using namespace std;
int m = 0;
int arr[100][2] = { 0 };
struct bian
{
int num;
int weight;
bian* next = NULL;
};
struct point
{
string letter;
bian* next = NULL;
};
struct graph
{
point m[100];
int number_of_point;
int number_of_bian;
};
void creat_no_directioin(graph& g)
{
int e;
int f;
cin >> e;
g.number_of_point = e;
for (int i = 0; i < e; i++)
{
cin >> g.m[i].letter;
}
//上面是输入的顶点的字母
cin >> f;
g.number_of_bian = f;
for (int i = 0; i < f; i++)
{
int v1;
int v2;
cin >> v1 >> v2;
bian* p1 = new bian;
p1->num = v2;
p1->next = g.m[v1].next;
g.m[v1].next = p1;
bian* p2 = new bian;
p2->num = v1;
p2->next = g.m[v2].next;
g.m[v2].next = p2;
}
}
void creat_direction(graph& g)
{
int e;
int f;
cin >> e;
g.number_of_point = e;
for (int i = 0; i < e; i++)
{
cin >> g.m[i].letter;
}
//上面是输入的顶点的字母
cin >> f;
g.number_of_bian = f;
for (int i = 0; i < f; i++)
{
int v1;
int v2;
cin >> v1 >> v2;
arr[i][0] = v1;
arr[i][1] = v2;
bian* p1 = new bian;
p1->num = v2;
p1->next = g.m[v1].next;
g.m[v1].next = p1;
}
}
int main()
{
string kind;
cin >> kind;
if (kind == "DN")//
{
graph g;
int m = 0;
creat_direction(g);
char am;
cin >> am;
cout << "DG" << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
m = 0;
cout << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
cout << " " << am << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << am << "->" << "nullptr" << endl;
}
else if (kind == "DG")
{
graph g;
int m = 0;
creat_direction(g);
char am;
cin >> am;
cout << "DG" << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
m = 0;
cout << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
cout << " " << am << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout <<am << "->" << "nullptr" << endl;
}
else if (kind == "UDN")//
{
graph g;
int m = 0;
creat_no_directioin(g);
char am;
cin >> am;
cout << "UDN" << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
m = 0;
cout << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
cout << " " << am << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << am << "->" << "nullptr" << endl;
}
else if (kind == "UDG")
{
graph g;
int m = 0;
creat_no_directioin(g);
char am;
cin >> am;
cout << "UDG" << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
m = 0;
cout << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << endl;
for (int h = 0; h < g.number_of_point; h++)
{
if (m == 1)
{
cout << " ";
}
m = 1;
cout << g.m[h].letter;
}
cout << " " << am << endl;
for (int u = 0; u < g.number_of_point; u++)
{
cout << g.m[u].letter;
bian* p = g.m[u].next;
while (p)
{
cout << "->";
cout << p->num;
p = p->next;
}
if (p == NULL)
{
cout << "->" << "nullptr" << endl;
}
}
cout << am << "->" << "nullptr" << endl;
}
return 0;
}